Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(modal): support alertdialog aria role #5955

Merged
merged 5 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/react/__tests__/__snapshots__/PublicAPI-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3072,6 +3072,9 @@ Map {
"selectorPrimaryFocus": "[data-modal-primary-focus]",
},
"propTypes": Object {
"alert": Object {
"type": "bool",
},
"aria-label": [Function],
"children": Object {
"type": "node",
Expand Down
2 changes: 2 additions & 0 deletions packages/react/src/components/Modal/Modal-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const props = () => ({
open: boolean('Open (open)', true),
passiveModal: boolean('Without footer (passiveModal)', false),
danger: boolean('Danger mode (danger)', false),
alert: boolean('Alert mode (alert)', false),
shouldSubmitOnEnter: boolean(
'Enter key to submit (shouldSubmitOnEnter)',
false
Expand Down Expand Up @@ -71,6 +72,7 @@ const titleOnlyProps = () => {
open: boolean('Open (open)', true),
passiveModal,
danger: !passiveModal && boolean('Danger mode (danger)', false),
alert: !passiveModal && boolean('Alert mode (alert)', false),
modalHeading: text(
'Modal heading (modalHeading)',
`
Expand Down
24 changes: 24 additions & 0 deletions packages/react/src/components/Modal/Modal-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const { prefix } = settings;

// The modal is the 0th child inside the wrapper on account of focus-trap-react
const getModal = (wrapper) => wrapper.find('.bx--modal');
const getModalBody = (wrapper) => wrapper.find('.bx--modal-container');

describe('Modal', () => {
describe('Renders as expected', () => {
Expand Down Expand Up @@ -287,3 +288,26 @@ describe('Danger Modal', () => {
});
});
});
describe('Alert Modal', () => {
describe('Renders as expected', () => {
const wrapper = shallow(<Modal aria-label="test" alert />);

it('has the expected attributes', () => {
expect(getModalBody(wrapper).props()).toEqual(
expect.objectContaining({
role: 'alertdialog',
'aria-describedby': expect.any(String),
})
);
});

it('should be a passive modal when passiveModal is passed', () => {
wrapper.setProps({ passiveModal: true });
expect(getModalBody(wrapper).props()).toEqual(
expect.objectContaining({
role: 'alert',
})
);
});
});
});
19 changes: 19 additions & 0 deletions packages/react/src/components/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ export default class Modal extends Component {
*/
danger: PropTypes.bool,

/**
* Specify whether the Modal is displaying an alert, error or warning
* Should go hand in hand with the danger prop.
*/
alert: PropTypes.bool,

/**
* Specify if Enter key should be used as "submit" action
*/
Expand Down Expand Up @@ -184,6 +190,7 @@ export default class Modal extends Component {
modalInstanceId = `modal-${getInstanceId()}`;
modalLabelId = `${prefix}--modal-header__label--${this.modalInstanceId}`;
modalHeadingId = `${prefix}--modal-header__heading--${this.modalInstanceId}`;
modalBodyId = `${prefix}--modal-body--${this.modalInstanceId}`;

handleKeyDown = (evt) => {
if (this.props.open) {
Expand Down Expand Up @@ -307,6 +314,7 @@ export default class Modal extends Component {
iconDescription,
primaryButtonDisabled,
danger,
alert,
selectorPrimaryFocus, // eslint-disable-line
selectorsFloatingMenus, // eslint-disable-line
shouldSubmitOnEnter, // eslint-disable-line
Expand Down Expand Up @@ -366,10 +374,20 @@ export default class Modal extends Component {
}
: {};

const alertDialogProps = {};
if (alert && passiveModal) {
alertDialogProps.role = 'alert';
}
if (alert && !passiveModal) {
alertDialogProps.role = 'alertdialog';
alertDialogProps['aria-describedby'] = this.modalBodyId;
}

const modalBody = (
<div
ref={this.innerModal}
role="dialog"
{...alertDialogProps}
className={containerClasses}
aria-label={ariaLabel}
aria-modal="true"
Expand All @@ -391,6 +409,7 @@ export default class Modal extends Component {
{!passiveModal && modalButton}
</div>
<div
id={this.modalBodyId}
className={contentClasses}
{...hasScrollingContentProps}
aria-labelledby={getAriaLabelledBy}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ exports[`ModalWrapper should render 1`] = `
<div
aria-labelledby="bx--modal-header__label--modal-1"
className="bx--modal-content"
id="bx--modal-body--modal-1"
>
<p
className="bx--modal-content__text"
Expand Down