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: Dialog support aria-* in closable #403

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Dialog/Content/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useRef } from 'react';
import { RefContext } from '../../context';
import type { IDialogPropTypes } from '../../IDialogPropTypes';
import MemoChildren from './MemoChildren';
import pickAttrs from 'rc-util/lib/pickAttrs';

const sentinelStyle = { width: 0, height: 0, overflow: 'hidden', outline: 'none' };
const entityStyle = { outline: 'none' };
Expand Down Expand Up @@ -96,11 +97,21 @@ const Panel = React.forwardRef<ContentRef, PanelProps>((props, ref) => {
);
}


const closableObj =
typeof closable === 'object'
kiner-tang marked this conversation as resolved.
Show resolved Hide resolved
? closable
: closable
? { closeIcon: closeIcon ?? <span className={`${prefixCls}-close-x`} /> }
: {};

const ariaProps = pickAttrs(closableObj, true);

let closer: React.ReactNode;
if (closable) {
closer = (
<button type="button" onClick={onClose} aria-label="Close" className={`${prefixCls}-close`}>
{closeIcon || <span className={`${prefixCls}-close-x`} />}
<button type="button" onClick={onClose} aria-label="Close" {...ariaProps} className={`${prefixCls}-close`}>
{closableObj.closeIcon}
</button>
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/IDialogPropTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type IDialogPropTypes = {
afterClose?: () => any;
afterOpenChange?: (open: boolean) => void;
onClose?: (e: SyntheticEvent) => any;
closable?: boolean;
closable?: boolean | ({ closeIcon?: React.ReactNode } & React.AriaAttributes);
maskClosable?: boolean;
visible?: boolean;
destroyOnClose?: boolean;
Expand Down
40 changes: 40 additions & 0 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -640,4 +640,44 @@ describe('dialog', () => {
);
spy.mockRestore();
});

it('support aria-* in closable', () => {
const onClose = jest.fn();
const wrapper = mount(
<Dialog
closable={{
closeIcon:"test",
'aria-label': 'test aria-label',
}}
visible
onClose={onClose}
/>
);
jest.runAllTimers();
wrapper.update();

const btn = wrapper.find('.rc-dialog-close');
expect(btn.text()).toBe('test');
expect(btn.getDOMNode().getAttribute('aria-label')).toBe('test aria-label');
btn.simulate('click');

jest.runAllTimers();
wrapper.update();
expect(onClose).toHaveBeenCalledTimes(1);
});
it('should not display closeIcon when closable is false', () => {
const onClose = jest.fn();
const wrapper = mount(
<Dialog
closable={false}
visible
onClose={onClose}
/>
);
jest.runAllTimers();
wrapper.update();

const btn = wrapper.find('.rc-dialog-close');
expect(btn.find('.rc-dialog-close-x')).not.toBeNull();
});
});