2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
|
|
|
// These should probably just be combined into one modal component
|
|
|
|
import * as React from 'react';
|
|
|
|
import ReactModal from 'react-modal';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
type ModalProps = {
|
|
|
|
type: string,
|
|
|
|
overlay: boolean,
|
|
|
|
confirmButtonLabel: string,
|
|
|
|
abortButtonLabel: string,
|
|
|
|
confirmButtonDisabled: boolean,
|
|
|
|
abortButtonDisabled: boolean,
|
|
|
|
onConfirmed?: any => any,
|
|
|
|
onAborted?: any => any,
|
|
|
|
className?: string,
|
|
|
|
children?: React.Node,
|
|
|
|
extraContent?: React.Node,
|
|
|
|
expandButtonLabel?: string,
|
|
|
|
hideButtonLabel?: string,
|
2019-01-10 02:38:26 +01:00
|
|
|
title?: string | React.Node,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export class Modal extends React.PureComponent<ModalProps> {
|
|
|
|
static defaultProps = {
|
|
|
|
type: 'alert',
|
|
|
|
overlay: true,
|
2019-03-05 05:46:57 +01:00
|
|
|
confirmButtonLabel: __('OK'),
|
|
|
|
abortButtonLabel: __('Cancel'),
|
2018-03-26 23:32:43 +02:00
|
|
|
confirmButtonDisabled: false,
|
|
|
|
abortButtonDisabled: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
children,
|
|
|
|
type,
|
|
|
|
confirmButtonLabel,
|
|
|
|
confirmButtonDisabled,
|
|
|
|
onConfirmed,
|
|
|
|
abortButtonLabel,
|
|
|
|
abortButtonDisabled,
|
|
|
|
onAborted,
|
|
|
|
className,
|
2018-09-26 19:48:07 +02:00
|
|
|
title,
|
2018-03-26 23:32:43 +02:00
|
|
|
...modalProps
|
|
|
|
} = this.props;
|
|
|
|
return (
|
|
|
|
<ReactModal
|
|
|
|
{...modalProps}
|
2018-05-31 06:32:31 +02:00
|
|
|
onRequestClose={onAborted || onConfirmed}
|
2019-09-27 22:03:05 +02:00
|
|
|
className={classnames('card modal', className)}
|
2019-03-25 07:18:22 +01:00
|
|
|
overlayClassName="modal-overlay"
|
2018-03-26 23:32:43 +02:00
|
|
|
>
|
2019-07-21 23:31:22 +02:00
|
|
|
{title && <h1 className="card__title">{title}</h1>}
|
2019-01-11 17:34:36 +01:00
|
|
|
{children}
|
2019-01-08 00:29:40 +01:00
|
|
|
{type === 'custom' ? null : ( // custom modals define their own buttons
|
2019-03-25 07:18:22 +01:00
|
|
|
<div className="card__actions">
|
2019-01-08 00:29:40 +01:00
|
|
|
<Button
|
2019-03-25 07:18:22 +01:00
|
|
|
button="primary"
|
2019-01-08 00:29:40 +01:00
|
|
|
label={confirmButtonLabel}
|
|
|
|
disabled={confirmButtonDisabled}
|
|
|
|
onClick={onConfirmed}
|
|
|
|
/>
|
|
|
|
{type === 'confirm' ? (
|
2019-05-07 23:38:29 +02:00
|
|
|
<Button button="link" label={abortButtonLabel} disabled={abortButtonDisabled} onClick={onAborted} />
|
2019-01-08 00:29:40 +01:00
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
)}
|
2018-03-26 23:32:43 +02:00
|
|
|
</ReactModal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
expanded: boolean,
|
|
|
|
};
|
|
|
|
|
|
|
|
export class ExpandableModal extends React.PureComponent<ModalProps, State> {
|
|
|
|
static defaultProps = {
|
2019-03-05 05:46:57 +01:00
|
|
|
confirmButtonLabel: __('OK'),
|
|
|
|
expandButtonLabel: __('Show More...'),
|
|
|
|
hideButtonLabel: __('Show Less'),
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: ModalProps) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
expanded: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
toggleExpanded() {
|
|
|
|
this.setState({
|
|
|
|
expanded: !this.state.expanded,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2019-03-25 07:18:22 +01:00
|
|
|
<Modal type="custom" {...this.props}>
|
2018-03-26 23:32:43 +02:00
|
|
|
{this.props.children}
|
2019-07-21 23:31:22 +02:00
|
|
|
{this.state.expanded ? <div>{this.props.extraContent}</div> : null}
|
2019-03-25 07:18:22 +01:00
|
|
|
<div className="card__actions">
|
2019-05-07 23:38:29 +02:00
|
|
|
<Button button="primary" label={this.props.confirmButtonLabel} onClick={this.props.onConfirmed} />
|
2018-03-26 23:32:43 +02:00
|
|
|
<Button
|
2019-03-25 07:18:22 +01:00
|
|
|
button="link"
|
2018-03-26 23:32:43 +02:00
|
|
|
label={!this.state.expanded ? this.props.expandButtonLabel : this.props.hideButtonLabel}
|
|
|
|
onClick={() => {
|
|
|
|
this.toggleExpanded();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Modal;
|