fix: confirm button should only trigger once (#22963)

close #22861
This commit is contained in:
偏右 2020-04-06 21:57:51 +08:00 committed by GitHub
parent f5d4bde04b
commit 8dbaf66c2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 8 deletions

View File

@ -12,18 +12,17 @@ export interface ActionButtonProps {
}
export interface ActionButtonState {
loading: boolean;
loading: ButtonProps['loading'];
}
export default class ActionButton extends React.Component<ActionButtonProps, ActionButtonState> {
timeoutId: number;
constructor(props: ActionButtonProps) {
super(props);
this.state = {
loading: false,
};
}
clicked: boolean;
state = {
loading: false,
};
componentDidMount() {
if (this.props.autoFocus) {
@ -38,6 +37,10 @@ export default class ActionButton extends React.Component<ActionButtonProps, Act
onClick = () => {
const { actionFn, closeModal } = this.props;
if (this.clicked) {
return;
}
this.clicked = true;
if (actionFn) {
let ret;
if (actionFn.length) {
@ -62,6 +65,7 @@ export default class ActionButton extends React.Component<ActionButtonProps, Act
console.error(e);
// See: https://github.com/ant-design/ant-design/issues/6183
this.setState({ loading: false });
this.clicked = false;
},
);
}
@ -74,7 +78,12 @@ export default class ActionButton extends React.Component<ActionButtonProps, Act
const { type, children, buttonProps } = this.props;
const { loading } = this.state;
return (
<Button type={type} onClick={this.onClick} loading={loading} {...buttonProps}>
<Button
type={type}
onClick={this.onClick}
loading={loading}
{...buttonProps}
>
{children}
</Button>
);

View File

@ -231,4 +231,13 @@ describe('Modal.confirm triggers callbacks correctly', () => {
);
warnSpy.mockRestore();
});
it('ok button should trigger onOk once when click it many times quickly', () => {
const onOk = jest.fn();
open({ onOk });
// Fifth Modal
$$('.ant-btn-primary')[0].click();
$$('.ant-btn-primary')[0].click();
expect(onOk).toHaveBeenCalledTimes(1);
});
});