File tree 6 files changed +92
-0
lines changed
6 files changed +92
-0
lines changed Original file line number Diff line number Diff line change 2
2
import * as React from 'react' ;
3
3
import PropTypes from 'prop-types' ;
4
4
import { useAlertDialogRootContext } from '../root/AlertDialogRootContext' ;
5
+ import { mergeReactProps } from '../../utils/mergeReactProps' ;
5
6
import { useComponentRenderer } from '../../utils/useComponentRenderer' ;
6
7
import { useEnhancedEffect } from '../../utils/useEnhancedEffect' ;
7
8
import { useBaseUiId } from '../../utils/useBaseUiId' ;
@@ -31,7 +32,16 @@ const AlertDialogDescription = React.forwardRef(function AlertDialogDescription(
31
32
} ;
32
33
} , [ id , setDescriptionElementId ] ) ;
33
34
35
+ const getProps = React . useCallback (
36
+ ( externalProps = { } ) =>
37
+ mergeReactProps ( externalProps , {
38
+ id,
39
+ } ) ,
40
+ [ id ] ,
41
+ ) ;
42
+
34
43
const { renderElement } = useComponentRenderer ( {
44
+ propGetter : getProps ,
35
45
render : render ?? 'p' ,
36
46
className,
37
47
state,
Original file line number Diff line number Diff line change @@ -8,6 +8,32 @@ import { spy } from 'sinon';
8
8
describe ( '<AlertDialog.Root />' , ( ) => {
9
9
const { render } = createRenderer ( ) ;
10
10
11
+ it ( 'ARIA attributes' , async ( ) => {
12
+ const { queryByRole, getByText } = await render (
13
+ < AlertDialog . Root open >
14
+ < AlertDialog . Trigger />
15
+ < AlertDialog . Portal >
16
+ < AlertDialog . Backdrop />
17
+ < AlertDialog . Popup >
18
+ < AlertDialog . Title > title text</ AlertDialog . Title >
19
+ < AlertDialog . Description > description text</ AlertDialog . Description >
20
+ </ AlertDialog . Popup >
21
+ </ AlertDialog . Portal >
22
+ </ AlertDialog . Root > ,
23
+ ) ;
24
+
25
+ const popup = queryByRole ( 'alertdialog' ) ;
26
+ expect ( popup ) . not . to . equal ( null ) ;
27
+ expect ( popup ) . to . have . attribute ( 'aria-modal' , 'true' ) ;
28
+
29
+ expect ( getByText ( 'title text' ) . getAttribute ( 'id' ) ) . to . equal (
30
+ popup ?. getAttribute ( 'aria-labelledby' ) ,
31
+ ) ;
32
+ expect ( getByText ( 'description text' ) . getAttribute ( 'id' ) ) . to . equal (
33
+ popup ?. getAttribute ( 'aria-describedby' ) ,
34
+ ) ;
35
+ } ) ;
36
+
11
37
describe ( 'prop: onOpenChange' , ( ) => {
12
38
it ( 'calls onOpenChange with the new open state' , async ( ) => {
13
39
const handleOpenChange = spy ( ) ;
Original file line number Diff line number Diff line change 2
2
import * as React from 'react' ;
3
3
import PropTypes from 'prop-types' ;
4
4
import { useAlertDialogRootContext } from '../root/AlertDialogRootContext' ;
5
+ import { mergeReactProps } from '../../utils/mergeReactProps' ;
5
6
import { useComponentRenderer } from '../../utils/useComponentRenderer' ;
6
7
import { useEnhancedEffect } from '../../utils/useEnhancedEffect' ;
7
8
import { useBaseUiId } from '../../utils/useBaseUiId' ;
@@ -31,7 +32,16 @@ const AlertDialogTitle = React.forwardRef(function AlertDialogTitle(
31
32
} ;
32
33
} , [ id , setTitleElementId ] ) ;
33
34
35
+ const getProps = React . useCallback (
36
+ ( externalProps = { } ) =>
37
+ mergeReactProps ( externalProps , {
38
+ id,
39
+ } ) ,
40
+ [ id ] ,
41
+ ) ;
42
+
34
43
const { renderElement } = useComponentRenderer ( {
44
+ propGetter : getProps ,
35
45
render : render ?? 'h2' ,
36
46
className,
37
47
state,
Original file line number Diff line number Diff line change 2
2
import * as React from 'react' ;
3
3
import PropTypes from 'prop-types' ;
4
4
import { useDialogRootContext } from '../root/DialogRootContext' ;
5
+ import { mergeReactProps } from '../../utils/mergeReactProps' ;
5
6
import { useComponentRenderer } from '../../utils/useComponentRenderer' ;
6
7
import { useEnhancedEffect } from '../../utils/useEnhancedEffect' ;
7
8
import { useBaseUiId } from '../../utils/useBaseUiId' ;
@@ -31,7 +32,16 @@ const DialogDescription = React.forwardRef(function DialogDescription(
31
32
} ;
32
33
} , [ id , setDescriptionElementId ] ) ;
33
34
35
+ const getProps = React . useCallback (
36
+ ( externalProps = { } ) =>
37
+ mergeReactProps ( externalProps , {
38
+ id,
39
+ } ) ,
40
+ [ id ] ,
41
+ ) ;
42
+
34
43
const { renderElement } = useComponentRenderer ( {
44
+ propGetter : getProps ,
35
45
render : render ?? 'p' ,
36
46
className,
37
47
state,
Original file line number Diff line number Diff line change @@ -14,6 +14,32 @@ describe('<Dialog.Root />', () => {
14
14
15
15
const { render } = createRenderer ( ) ;
16
16
17
+ it ( 'ARIA attributes' , async ( ) => {
18
+ const { queryByRole, getByText } = await render (
19
+ < Dialog . Root modal = { false } open >
20
+ < Dialog . Trigger />
21
+ < Dialog . Portal >
22
+ < Dialog . Backdrop />
23
+ < Dialog . Popup >
24
+ < Dialog . Title > title text</ Dialog . Title >
25
+ < Dialog . Description > description text</ Dialog . Description >
26
+ </ Dialog . Popup >
27
+ </ Dialog . Portal >
28
+ </ Dialog . Root > ,
29
+ ) ;
30
+
31
+ const popup = queryByRole ( 'dialog' ) ;
32
+ expect ( popup ) . not . to . equal ( null ) ;
33
+ expect ( popup ) . to . not . have . attribute ( 'aria-modal' ) ;
34
+
35
+ expect ( getByText ( 'title text' ) . getAttribute ( 'id' ) ) . to . equal (
36
+ popup ?. getAttribute ( 'aria-labelledby' ) ,
37
+ ) ;
38
+ expect ( getByText ( 'description text' ) . getAttribute ( 'id' ) ) . to . equal (
39
+ popup ?. getAttribute ( 'aria-describedby' ) ,
40
+ ) ;
41
+ } ) ;
42
+
17
43
describe ( 'uncontrolled mode' , ( ) => {
18
44
it ( 'should open the dialog with the trigger' , async ( ) => {
19
45
const { queryByRole, getByRole } = await render (
Original file line number Diff line number Diff line change 2
2
import * as React from 'react' ;
3
3
import PropTypes from 'prop-types' ;
4
4
import { useDialogRootContext } from '../root/DialogRootContext' ;
5
+ import { mergeReactProps } from '../../utils/mergeReactProps' ;
5
6
import { useComponentRenderer } from '../../utils/useComponentRenderer' ;
6
7
import { useEnhancedEffect } from '../../utils/useEnhancedEffect' ;
7
8
import { useBaseUiId } from '../../utils/useBaseUiId' ;
@@ -31,7 +32,16 @@ const DialogTitle = React.forwardRef(function DialogTitle(
31
32
} ;
32
33
} , [ id , setTitleElementId ] ) ;
33
34
35
+ const getProps = React . useCallback (
36
+ ( externalProps = { } ) =>
37
+ mergeReactProps ( externalProps , {
38
+ id,
39
+ } ) ,
40
+ [ id ] ,
41
+ ) ;
42
+
34
43
const { renderElement } = useComponentRenderer ( {
44
+ propGetter : getProps ,
35
45
render : render ?? 'h2' ,
36
46
className,
37
47
state,
You can’t perform that action at this time.
0 commit comments