mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-24 07:28:28 +08:00
b9a86b2ea1
This PR adds the `okButtonProps` and `cancelButtonProps` API to the `Modal.confirm` and associated methods. * [x] Make sure that you propose PR to right branch: bugfix for `master`, feature for branch `feature`. * [x] Make sure that you follow antd's [code convention](https://github.com/ant-design/ant-design/wiki/Code-convention-for-antd). * [x] Run `npm run lint` and fix those errors before submitting in order to keep consistent code style. * [x] Rebase before creating a PR to keep commit history clear. * [x] Add some descriptions and refer relative issues for you PR. Extra checklist: *isNewFeature* **:** * [x] Update API docs for the component. * [x] Update/Add demo to demonstrate new feature. * [x] Update TypeScript definition for the component. * [x] Add unit tests for the feature.
74 lines
1.9 KiB
TypeScript
74 lines
1.9 KiB
TypeScript
import * as React from 'react';
|
|
import * as ReactDOM from 'react-dom';
|
|
import Button from '../button';
|
|
import { ButtonType, NativeButtonProps } from '../button/button';
|
|
|
|
export interface ActionButtonProps {
|
|
type?: ButtonType;
|
|
actionFn?: (...args: any[]) => any | PromiseLike<any>;
|
|
closeModal: Function;
|
|
autoFocus?: boolean;
|
|
buttonProps?: NativeButtonProps;
|
|
}
|
|
|
|
export interface ActionButtonState {
|
|
loading: boolean;
|
|
}
|
|
|
|
export default class ActionButton extends React.Component<ActionButtonProps, ActionButtonState> {
|
|
timeoutId: number;
|
|
|
|
constructor(props: ActionButtonProps) {
|
|
super(props);
|
|
this.state = {
|
|
loading: false,
|
|
};
|
|
}
|
|
componentDidMount() {
|
|
if (this.props.autoFocus) {
|
|
const $this = ReactDOM.findDOMNode(this) as HTMLInputElement;
|
|
this.timeoutId = setTimeout(() => $this.focus());
|
|
}
|
|
}
|
|
componentWillUnmount() {
|
|
clearTimeout(this.timeoutId);
|
|
}
|
|
onClick = () => {
|
|
const { actionFn, closeModal } = this.props;
|
|
if (actionFn) {
|
|
let ret;
|
|
if (actionFn.length) {
|
|
ret = actionFn(closeModal);
|
|
} else {
|
|
ret = actionFn();
|
|
if (!ret) {
|
|
closeModal();
|
|
}
|
|
}
|
|
if (ret && ret.then) {
|
|
this.setState({ loading: true });
|
|
ret.then((...args: any[]) => {
|
|
// It's unnecessary to set loading=false, for the Modal will be unmounted after close.
|
|
// this.setState({ loading: false });
|
|
closeModal(...args);
|
|
}, () => {
|
|
// See: https://github.com/ant-design/ant-design/issues/6183
|
|
this.setState({ loading: false });
|
|
});
|
|
}
|
|
} else {
|
|
closeModal();
|
|
}
|
|
}
|
|
|
|
render() {
|
|
const { type, children, buttonProps } = this.props;
|
|
const loading = this.state.loading;
|
|
return (
|
|
<Button type={type} onClick={this.onClick} loading={loading} {...buttonProps}>
|
|
{children}
|
|
</Button>
|
|
);
|
|
}
|
|
}
|