Skip to content

Commit f57c03e

Browse files
Ke1syAlina Andrieieva
andauthored
feat: Add prop to disable close button (#416)
* Add prop to disable close button * renamed prop to `disableCloseBtn` * removed empty line * added 'disable' to closable * readme update --------- Co-authored-by: Alina Andrieieva <[email protected]>
1 parent 51b3f67 commit f57c03e

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ ReactDOM.render(
6464
| maskTransitionName | String | | mask animation css class name | |
6565
| title | String\|React.Element | | Title of the dialog | |
6666
| footer | React.Element | | footer of the dialog | |
67-
| closable | Boolean | true | whether show close button | |
67+
| closable | Boolean \| ({ closeIcon?: React.ReactNode; disabled?: boolean } & React.AriaAttributes | true | whether show close button | |
6868
| mask | Boolean | true | whether show mask | |
6969
| maskClosable | Boolean | true | whether click mask to close | |
7070
| keyboard | Boolean | true | whether support press esc to close | |

assets/index/Dialog.less

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
opacity: .2;
4747
text-decoration: none;
4848

49+
&:disabled {
50+
pointer-events: none;
51+
}
52+
4953
&-x:after {
5054
content: '×'
5155
}

src/Dialog/Content/Panel.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ const Panel = React.forwardRef<ContentRef, PanelProps>((props, ref) => {
112112
}, [closable, closeIcon, prefixCls]);
113113

114114
const ariaProps = pickAttrs(closableObj, true);
115+
const closeBtnIsDisabled = typeof(closable) === 'object' && closable.disabled;
115116

116117
const closerNode = closable ? (
117118
<button
@@ -120,6 +121,7 @@ const Panel = React.forwardRef<ContentRef, PanelProps>((props, ref) => {
120121
aria-label="Close"
121122
{...ariaProps}
122123
className={`${prefixCls}-close`}
124+
disabled={closeBtnIsDisabled}
123125
>
124126
{closableObj.closeIcon}
125127
</button>

src/IDialogPropTypes.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export type IDialogPropTypes = {
2828
afterClose?: () => any;
2929
afterOpenChange?: (open: boolean) => void;
3030
onClose?: (e: SyntheticEvent) => any;
31-
closable?: boolean | ({ closeIcon?: React.ReactNode } & React.AriaAttributes);
31+
closable?: boolean | ({ closeIcon?: React.ReactNode; disabled?: boolean } & React.AriaAttributes);
3232
maskClosable?: boolean;
3333
visible?: boolean;
3434
destroyOnClose?: boolean;

tests/index.spec.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,36 @@ describe('dialog', () => {
665665
wrapper.update();
666666
expect(onClose).toHaveBeenCalledTimes(1);
667667
});
668+
669+
it('support disable button in closable', () => {
670+
const onClose = jest.fn();
671+
const wrapper = mount(
672+
<Dialog
673+
closable={{
674+
closeIcon:"test",
675+
disabled: true,
676+
}}
677+
visible
678+
onClose={onClose}
679+
/>
680+
);
681+
jest.runAllTimers();
682+
wrapper.update();
683+
684+
const btn = wrapper.find('.rc-dialog-close');
685+
expect(btn.prop('disabled')).toBe(true);
686+
btn.simulate('click');
687+
688+
jest.runAllTimers();
689+
wrapper.update();
690+
expect(onClose).not.toHaveBeenCalled();
691+
692+
btn.simulate('keydown', { key: 'Enter' });
693+
jest.runAllTimers();
694+
wrapper.update();
695+
expect(onClose).not.toHaveBeenCalled();
696+
});
697+
668698
it('should not display closeIcon when closable is false', () => {
669699
const onClose = jest.fn();
670700
const wrapper = mount(

0 commit comments

Comments
 (0)