1 В избранное 0 Ответвления 0

OSCHINA-MIRROR/open-hand-choerodon-ui

Присоединиться к Gitlife
Откройте для себя и примите участие в публичных проектах с открытым исходным кодом с участием более 10 миллионов разработчиков. Приватные репозитории также полностью бесплатны :)
Присоединиться бесплатно
Это зеркальный репозиторий, синхронизируется ежедневно с исходного репозитория.
Клонировать/Скачать
ActionButton.tsx 2.5 КБ
Копировать Редактировать Web IDE Исходные данные Просмотреть построчно История
HughHzWu Отправлено 3 лет назад a07a29b
import React, { Component, ReactNode } from 'react';
import { findDOMNode } from 'react-dom';
import Button from '../button';
import { ButtonType, ButtonProps } from '../button/Button';
export interface CommonButtonProps {
type?: ButtonType;
actionFn?: (...args: any[]) => any | PromiseLike<any>;
closeModal: Function;
text?: ReactNode;
buttonProps?: ButtonProps;
}
export interface ActionButtonProps {
okProps: CommonButtonProps;
cancelProps?: CommonButtonProps;
autoFocus?: boolean;
}
export interface ActionButtonState {
loading: boolean;
}
export default class ActionButton extends Component<ActionButtonProps, ActionButtonState> {
timeoutId: number;
constructor(props: ActionButtonProps) {
super(props);
this.state = {
loading: false,
};
}
componentDidMount() {
const { autoFocus } = this.props;
if (autoFocus) {
const $this = findDOMNode(this) as HTMLInputElement;
this.timeoutId = window.setTimeout(() => $this.focus());
}
}
componentWillUnmount() {
clearTimeout(this.timeoutId);
}
onClick = (props: CommonButtonProps) => {
const { actionFn, closeModal } = 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);
},
() => {
this.setState({ loading: false });
},
);
}
} else {
closeModal();
}
};
render() {
const { okProps, cancelProps } = this.props;
const { loading } = this.state;
const cancelButton = cancelProps && (
<Button
{...cancelProps.buttonProps}
type={cancelProps.type}
disabled={loading}
onClick={() => {
this.onClick(cancelProps);
}}
>
{cancelProps.text}
</Button>
);
return (
<>
{cancelButton}
<Button
{...okProps.buttonProps}
loading={loading}
type={okProps.type}
onClick={() => {
this.onClick(okProps);
}}
>
{okProps.text}
</Button>
</>
);
}
}

Комментарий ( 0 )

Вы можете оставить комментарий после Вход в систему

1
https://gitlife.ru/oschina-mirror/open-hand-choerodon-ui.git
git@gitlife.ru:oschina-mirror/open-hand-choerodon-ui.git
oschina-mirror
open-hand-choerodon-ui
open-hand-choerodon-ui
master