2022-11-13 14:13:33 +02:00
|
|
|
import * as React from 'react';
|
|
|
|
import { _ } from '@joplin/lib/locale';
|
|
|
|
import { themeStyle } from '@joplin/lib/theme';
|
|
|
|
import time from '@joplin/lib/time';
|
|
|
|
const Datetime = require('react-datetime').default;
|
|
|
|
import CreatableSelect from 'react-select/creatable';
|
|
|
|
import Select from 'react-select';
|
|
|
|
import makeAnimated from 'react-select/animated';
|
|
|
|
interface Props {
|
|
|
|
themeId: number;
|
|
|
|
defaultValue: any;
|
|
|
|
visible: boolean;
|
|
|
|
style: any;
|
|
|
|
buttons: any[];
|
|
|
|
onClose: Function;
|
|
|
|
inputType: string;
|
|
|
|
description: string;
|
|
|
|
answer?: any;
|
|
|
|
autocomplete: any;
|
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class PromptDialog extends React.Component<Props, any> {
|
|
|
|
|
|
|
|
private answerInput_: any;
|
|
|
|
private focusInput_: boolean;
|
|
|
|
private styles_: any;
|
|
|
|
private styleKey_: string;
|
2023-06-30 10:07:03 +02:00
|
|
|
private menuIsOpened_ = false;
|
2022-11-13 14:13:33 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public constructor(props: Props) {
|
2022-11-13 14:13:33 +02:00
|
|
|
super(props);
|
2019-06-08 00:33:06 +02:00
|
|
|
|
|
|
|
this.answerInput_ = React.createRef();
|
2023-05-08 18:07:55 +02:00
|
|
|
|
|
|
|
this.select_menuOpen = this.select_menuOpen.bind(this);
|
|
|
|
this.select_menuClose = this.select_menuClose.bind(this);
|
2019-06-08 00:33:06 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public UNSAFE_componentWillMount() {
|
2017-11-08 19:51:55 +02:00
|
|
|
this.setState({
|
|
|
|
visible: false,
|
2019-02-24 20:04:25 +02:00
|
|
|
answer: this.props.defaultValue ? this.props.defaultValue : '',
|
2017-11-08 19:51:55 +02:00
|
|
|
});
|
|
|
|
this.focusInput_ = true;
|
2023-05-08 18:07:55 +02:00
|
|
|
this.menuIsOpened_ = false;
|
2017-11-08 19:51:55 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public UNSAFE_componentWillReceiveProps(newProps: Props) {
|
2017-11-30 01:03:10 +02:00
|
|
|
if ('visible' in newProps && newProps.visible !== this.props.visible) {
|
2017-11-08 19:51:55 +02:00
|
|
|
this.setState({ visible: newProps.visible });
|
|
|
|
if (newProps.visible) this.focusInput_ = true;
|
|
|
|
}
|
2017-11-12 01:13:14 +02:00
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
if ('defaultValue' in newProps && newProps.defaultValue !== this.props.defaultValue) {
|
2019-02-24 20:04:25 +02:00
|
|
|
this.setState({ answer: newProps.defaultValue });
|
2017-11-12 01:13:14 +02:00
|
|
|
}
|
2017-11-08 19:51:55 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 18:07:55 +02:00
|
|
|
private select_menuOpen() {
|
|
|
|
this.menuIsOpened_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private select_menuClose() {
|
|
|
|
this.menuIsOpened_ = false;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public componentDidUpdate() {
|
2019-06-08 00:33:06 +02:00
|
|
|
if (this.focusInput_ && this.answerInput_.current) this.answerInput_.current.focus();
|
2017-11-08 19:51:55 +02:00
|
|
|
this.focusInput_ = false;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public styles(themeId: number, width: number, height: number, visible: boolean) {
|
2019-09-19 23:51:18 +02:00
|
|
|
const styleKey = `${themeId}_${width}_${height}_${visible}`;
|
2017-11-30 01:03:10 +02:00
|
|
|
if (styleKey === this.styleKey_) return this.styles_;
|
|
|
|
|
|
|
|
const theme = themeStyle(themeId);
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styleKey_ = styleKey;
|
|
|
|
|
|
|
|
this.styles_ = {};
|
|
|
|
|
2018-01-09 22:06:47 +02:00
|
|
|
const paddingTop = 20;
|
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styles_.modalLayer = {
|
2017-11-08 19:51:55 +02:00
|
|
|
zIndex: 9999,
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
2017-11-30 01:03:10 +02:00
|
|
|
width: width,
|
2022-01-15 19:00:42 +02:00
|
|
|
height: height,
|
2017-11-08 19:51:55 +02:00
|
|
|
backgroundColor: 'rgba(0,0,0,0.6)',
|
2017-11-30 01:03:10 +02:00
|
|
|
display: visible ? 'flex' : 'none',
|
2019-06-08 00:33:06 +02:00
|
|
|
alignItems: 'flex-start',
|
|
|
|
justifyContent: 'center',
|
2019-09-19 23:51:18 +02:00
|
|
|
paddingTop: `${paddingTop}px`,
|
2017-11-08 19:51:55 +02:00
|
|
|
};
|
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styles_.promptDialog = {
|
2018-11-08 00:37:13 +02:00
|
|
|
backgroundColor: theme.backgroundColor,
|
2017-11-10 22:34:36 +02:00
|
|
|
padding: 16,
|
2017-11-08 19:51:55 +02:00
|
|
|
display: 'inline-block',
|
2019-06-08 00:33:06 +02:00
|
|
|
maxWidth: width * 0.5,
|
2017-11-08 19:51:55 +02:00
|
|
|
boxShadow: '6px 6px 20px rgba(0,0,0,0.5)',
|
|
|
|
};
|
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styles_.button = {
|
2017-11-08 19:51:55 +02:00
|
|
|
minWidth: theme.buttonMinWidth,
|
|
|
|
minHeight: theme.buttonMinHeight,
|
|
|
|
marginLeft: 5,
|
2018-11-08 00:37:13 +02:00
|
|
|
color: theme.color,
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
border: '1px solid',
|
|
|
|
borderColor: theme.dividerColor,
|
2017-11-08 19:51:55 +02:00
|
|
|
};
|
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styles_.label = {
|
2017-11-10 22:34:36 +02:00
|
|
|
marginRight: 5,
|
|
|
|
fontSize: theme.fontSize,
|
|
|
|
color: theme.color,
|
|
|
|
fontFamily: theme.fontFamily,
|
2019-06-08 00:33:06 +02:00
|
|
|
verticalAlign: 'middle',
|
2017-11-10 22:34:36 +02:00
|
|
|
};
|
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
this.styles_.input = {
|
|
|
|
width: 0.5 * width,
|
2017-11-08 19:51:55 +02:00
|
|
|
maxWidth: 400,
|
2018-11-08 00:37:13 +02:00
|
|
|
color: theme.color,
|
|
|
|
backgroundColor: theme.backgroundColor,
|
|
|
|
border: '1px solid',
|
|
|
|
borderColor: theme.dividerColor,
|
2017-11-08 19:51:55 +02:00
|
|
|
};
|
|
|
|
|
2019-07-20 23:13:10 +02:00
|
|
|
this.styles_.select = {
|
2023-06-01 13:02:36 +02:00
|
|
|
control: (provided: any) => {
|
|
|
|
return { ...provided,
|
2019-07-29 14:13:23 +02:00
|
|
|
minWidth: width * 0.2,
|
|
|
|
maxWidth: width * 0.5,
|
2019-08-29 18:34:54 +02:00
|
|
|
fontFamily: theme.fontFamily,
|
2023-06-01 13:02:36 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
input: (provided: any) => {
|
|
|
|
return { ...provided,
|
2019-07-29 14:13:23 +02:00
|
|
|
minWidth: '20px',
|
|
|
|
color: theme.color,
|
2023-06-01 13:02:36 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
menu: (provided: any) => {
|
|
|
|
return { ...provided,
|
2019-07-29 14:13:23 +02:00
|
|
|
color: theme.color,
|
|
|
|
fontFamily: theme.fontFamily,
|
|
|
|
backgroundColor: theme.backgroundColor,
|
2023-06-01 13:02:36 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
option: (provided: any, state: any) => {
|
|
|
|
return { ...provided,
|
2019-07-29 14:13:23 +02:00
|
|
|
color: theme.color,
|
|
|
|
fontFamily: theme.fontFamily,
|
2020-04-04 19:03:09 +02:00
|
|
|
paddingLeft: `${10 + (state.data.indentDepth || 0) * 20}px`,
|
2023-06-01 13:02:36 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
multiValueLabel: (provided: any) => {
|
|
|
|
return { ...provided,
|
2019-07-29 14:13:23 +02:00
|
|
|
fontFamily: theme.fontFamily,
|
2023-06-01 13:02:36 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
multiValueRemove: (provided: any) => {
|
|
|
|
return { ...provided,
|
2019-07-29 14:13:23 +02:00
|
|
|
color: theme.color,
|
2023-06-01 13:02:36 +02:00
|
|
|
};
|
|
|
|
},
|
2019-06-08 00:33:06 +02:00
|
|
|
};
|
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
this.styles_.selectTheme = (tagTheme: any) => {
|
|
|
|
return { ...tagTheme,
|
2019-07-29 14:13:23 +02:00
|
|
|
borderRadius: 2,
|
2023-06-01 13:02:36 +02:00
|
|
|
colors: { ...tagTheme.colors,
|
2019-07-29 14:13:23 +02:00
|
|
|
primary: theme.raisedBackgroundColor,
|
|
|
|
primary25: theme.raisedBackgroundColor,
|
|
|
|
neutral0: theme.backgroundColor,
|
|
|
|
neutral5: theme.backgroundColor,
|
|
|
|
neutral10: theme.raisedBackgroundColor,
|
|
|
|
neutral20: theme.raisedBackgroundColor,
|
|
|
|
neutral30: theme.raisedBackgroundColor,
|
|
|
|
neutral40: theme.color,
|
|
|
|
neutral50: theme.color,
|
|
|
|
neutral60: theme.color,
|
|
|
|
neutral70: theme.color,
|
|
|
|
neutral80: theme.color,
|
|
|
|
neutral90: theme.color,
|
|
|
|
danger: theme.backgroundColor,
|
|
|
|
dangerLight: theme.colorError2,
|
2023-06-01 13:02:36 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
2019-06-08 00:33:06 +02:00
|
|
|
|
2023-06-01 13:02:36 +02:00
|
|
|
this.styles_.desc = { ...theme.textStyle, marginTop: 10 };
|
2017-11-12 01:13:14 +02:00
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
return this.styles_;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public render() {
|
2017-11-30 01:03:10 +02:00
|
|
|
const style = this.props.style;
|
2020-09-15 15:01:07 +02:00
|
|
|
const theme = themeStyle(this.props.themeId);
|
2017-11-30 01:03:10 +02:00
|
|
|
const buttonTypes = this.props.buttons ? this.props.buttons : ['ok', 'cancel'];
|
|
|
|
|
2020-09-15 15:01:07 +02:00
|
|
|
const styles = this.styles(this.props.themeId, style.width, style.height, this.state.visible);
|
2017-11-30 01:03:10 +02:00
|
|
|
|
2022-11-13 14:13:33 +02:00
|
|
|
const onClose = (accept: boolean, buttonType: string = null) => {
|
2017-12-01 23:55:02 +02:00
|
|
|
if (this.props.onClose) {
|
|
|
|
let outputAnswer = this.state.answer;
|
|
|
|
if (this.props.inputType === 'datetime') {
|
2018-09-16 20:37:31 +02:00
|
|
|
// outputAnswer = anythingToDate(outputAnswer);
|
|
|
|
outputAnswer = time.anythingToDateTime(outputAnswer);
|
2017-12-01 23:55:02 +02:00
|
|
|
}
|
|
|
|
this.props.onClose(accept ? outputAnswer : null, buttonType);
|
|
|
|
}
|
2017-11-08 19:51:55 +02:00
|
|
|
this.setState({ visible: false, answer: '' });
|
2019-07-29 14:13:23 +02:00
|
|
|
};
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2022-11-13 14:13:33 +02:00
|
|
|
const onChange = (event: any) => {
|
2017-11-08 19:51:55 +02:00
|
|
|
this.setState({ answer: event.target.value });
|
2019-07-29 14:13:23 +02:00
|
|
|
};
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2018-09-16 20:37:31 +02:00
|
|
|
// const anythingToDate = (o) => {
|
|
|
|
// if (o && o.toDate) return o.toDate();
|
|
|
|
// if (!o) return null;
|
|
|
|
// let m = moment(o, time.dateTimeFormat());
|
|
|
|
// if (m.isValid()) return m.toDate();
|
|
|
|
// m = moment(o, time.dateFormat());
|
|
|
|
// return m.isValid() ? m.toDate() : null;
|
|
|
|
// }
|
2017-12-01 23:55:02 +02:00
|
|
|
|
2022-11-13 14:13:33 +02:00
|
|
|
const onDateTimeChange = (momentObject: any) => {
|
2017-12-01 23:55:02 +02:00
|
|
|
this.setState({ answer: momentObject });
|
2019-07-29 14:13:23 +02:00
|
|
|
};
|
2017-11-28 00:50:46 +02:00
|
|
|
|
2022-11-13 14:13:33 +02:00
|
|
|
const onSelectChange = (newValue: any) => {
|
2019-07-20 23:13:10 +02:00
|
|
|
this.setState({ answer: newValue });
|
2019-06-08 00:33:06 +02:00
|
|
|
this.focusInput_ = true;
|
2019-07-29 14:13:23 +02:00
|
|
|
};
|
2019-06-08 00:33:06 +02:00
|
|
|
|
2022-11-13 14:13:33 +02:00
|
|
|
const onKeyDown = (event: any) => {
|
2019-07-20 23:13:10 +02:00
|
|
|
if (event.key === 'Enter') {
|
2023-05-08 18:07:55 +02:00
|
|
|
// If the dropdown is open, we don't close the dialog - instead
|
|
|
|
// the currently item will be selcted. If it is closed however
|
|
|
|
// we confirm the dialog.
|
|
|
|
if ((this.props.inputType === 'tags' || this.props.inputType === 'dropdown') && this.menuIsOpened_) {
|
2022-12-31 01:39:17 +02:00
|
|
|
// Do nothing
|
|
|
|
} else {
|
2019-07-20 23:13:10 +02:00
|
|
|
onClose(true);
|
|
|
|
}
|
2017-11-08 19:51:55 +02:00
|
|
|
} else if (event.key === 'Escape') {
|
2017-11-08 23:22:24 +02:00
|
|
|
onClose(false);
|
2017-11-08 19:51:55 +02:00
|
|
|
}
|
2019-07-29 14:13:23 +02:00
|
|
|
};
|
2017-11-08 19:51:55 +02:00
|
|
|
|
2017-11-30 01:03:10 +02:00
|
|
|
const descComp = this.props.description ? <div style={styles.desc}>{this.props.description}</div> : null;
|
2017-11-12 01:13:14 +02:00
|
|
|
|
2017-11-28 00:50:46 +02:00
|
|
|
let inputComp = null;
|
|
|
|
|
|
|
|
if (this.props.inputType === 'datetime') {
|
2022-11-13 14:13:33 +02:00
|
|
|
inputComp = <Datetime className="datetime-picker" value={this.state.answer} inputProps={{ style: styles.input }} dateFormat={time.dateFormat()} timeFormat={time.timeFormat()} onChange={(momentObject: any) => onDateTimeChange(momentObject)} />;
|
2019-06-08 00:33:06 +02:00
|
|
|
} else if (this.props.inputType === 'tags') {
|
2023-05-08 18:07:55 +02:00
|
|
|
inputComp = <CreatableSelect className="tag-selector" onMenuOpen={this.select_menuOpen} onMenuClose={this.select_menuClose} styles={styles.select} theme={styles.selectTheme} ref={this.answerInput_} value={this.state.answer} placeholder="" components={makeAnimated()} isMulti={true} isClearable={false} backspaceRemovesValue={true} options={this.props.autocomplete} onChange={onSelectChange} onKeyDown={(event: any) => onKeyDown(event)} />;
|
2019-07-20 23:13:10 +02:00
|
|
|
} else if (this.props.inputType === 'dropdown') {
|
2023-05-08 18:07:55 +02:00
|
|
|
inputComp = <Select className="item-selector" onMenuOpen={this.select_menuOpen} onMenuClose={this.select_menuClose} styles={styles.select} theme={styles.selectTheme} ref={this.answerInput_} components={makeAnimated()} value={this.props.answer} defaultValue={this.props.defaultValue} isClearable={false} options={this.props.autocomplete} onChange={onSelectChange} onKeyDown={(event: any) => onKeyDown(event)} />;
|
2017-11-28 00:50:46 +02:00
|
|
|
} else {
|
2020-05-21 10:14:33 +02:00
|
|
|
inputComp = <input style={styles.input} ref={this.answerInput_} value={this.state.answer} type="text" onChange={event => onChange(event)} onKeyDown={event => onKeyDown(event)} />;
|
2017-11-28 00:50:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const buttonComps = [];
|
2022-04-11 17:49:32 +02:00
|
|
|
if (buttonTypes.indexOf('create') >= 0) {
|
|
|
|
buttonComps.push(
|
|
|
|
<button key="create" disabled={!this.state.answer} style={styles.button} onClick={() => onClose(true, 'create')}>
|
|
|
|
{_('Create')}
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
2020-03-14 01:57:34 +02:00
|
|
|
if (buttonTypes.indexOf('ok') >= 0) {
|
2019-07-29 14:13:23 +02:00
|
|
|
buttonComps.push(
|
2020-04-14 13:20:05 +02:00
|
|
|
<button key="ok" disabled={!this.state.answer} style={styles.button} onClick={() => onClose(true, 'ok')}>
|
2019-07-29 14:13:23 +02:00
|
|
|
{_('OK')}
|
|
|
|
</button>
|
|
|
|
);
|
2020-03-14 01:57:34 +02:00
|
|
|
}
|
|
|
|
if (buttonTypes.indexOf('cancel') >= 0) {
|
2019-07-29 14:13:23 +02:00
|
|
|
buttonComps.push(
|
|
|
|
<button key="cancel" style={styles.button} onClick={() => onClose(false, 'cancel')}>
|
|
|
|
{_('Cancel')}
|
|
|
|
</button>
|
|
|
|
);
|
2020-03-14 01:57:34 +02:00
|
|
|
}
|
|
|
|
if (buttonTypes.indexOf('clear') >= 0) {
|
2019-07-29 14:13:23 +02:00
|
|
|
buttonComps.push(
|
|
|
|
<button key="clear" style={styles.button} onClick={() => onClose(false, 'clear')}>
|
|
|
|
{_('Clear')}
|
|
|
|
</button>
|
|
|
|
);
|
2020-03-14 01:57:34 +02:00
|
|
|
}
|
2017-11-28 00:50:46 +02:00
|
|
|
|
2017-11-08 19:51:55 +02:00
|
|
|
return (
|
2021-05-17 20:33:44 +02:00
|
|
|
<div className="modal-layer" style={styles.modalLayer}>
|
|
|
|
<div className="modal-dialog" style={styles.promptDialog}>
|
2017-11-30 01:03:10 +02:00
|
|
|
<label style={styles.label}>{this.props.label ? this.props.label : ''}</label>
|
2019-07-29 14:13:23 +02:00
|
|
|
<div style={{ display: 'inline-block', color: 'black', backgroundColor: theme.backgroundColor }}>
|
2017-11-28 00:50:46 +02:00
|
|
|
{inputComp}
|
2017-11-12 01:13:14 +02:00
|
|
|
{descComp}
|
|
|
|
</div>
|
2019-07-29 14:13:23 +02:00
|
|
|
<div style={{ textAlign: 'right', marginTop: 10 }}>{buttonComps}</div>
|
2017-11-08 19:51:55 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|