Convert TextInputDialog to TS

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
pull/21833/head
Šimon Brandner 2021-09-05 08:35:24 +02:00
parent 0b9255f5ee
commit 37099fd188
No known key found for this signature in database
GPG Key ID: 55C211A1226CB17D
1 changed files with 40 additions and 39 deletions

View File

@ -14,33 +14,39 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import React, { createRef } from 'react'; import React, { ChangeEvent, createRef } from 'react';
import PropTypes from 'prop-types';
import * as sdk from '../../../index';
import Field from "../elements/Field"; import Field from "../elements/Field";
import { _t, _td } from '../../../languageHandler'; import { _t, _td } from '../../../languageHandler';
import { replaceableComponent } from "../../../utils/replaceableComponent"; import { replaceableComponent } from "../../../utils/replaceableComponent";
import { IFieldState, IValidationResult } from "../elements/Validation";
import BaseDialog from "./BaseDialog";
import DialogButtons from "../elements/DialogButtons";
interface IProps {
title?: string;
description?: string | JSX.Element;
value?: string;
placeholder?: string;
button?: string;
busyMessage?: string; // pass _td string
focus?: boolean;
onFinished: (success: boolean, value?: string) => void;
hasCancel?: boolean;
validator?: (fieldState: IFieldState) => IValidationResult; // result of withValidation
fixedWidth?: boolean;
}
interface IState {
value: string;
busy: boolean;
valid: boolean;
}
@replaceableComponent("views.dialogs.TextInputDialog") @replaceableComponent("views.dialogs.TextInputDialog")
export default class TextInputDialog extends React.Component { export default class TextInputDialog extends React.Component<IProps, IState> {
static propTypes = { private field = createRef<Field>();
title: PropTypes.string,
description: PropTypes.oneOfType([
PropTypes.element,
PropTypes.string,
]),
value: PropTypes.string,
placeholder: PropTypes.string,
button: PropTypes.string,
busyMessage: PropTypes.string, // pass _td string
focus: PropTypes.bool,
onFinished: PropTypes.func.isRequired,
hasCancel: PropTypes.bool,
validator: PropTypes.func, // result of withValidation
fixedWidth: PropTypes.bool,
};
static defaultProps = { public static defaultProps = {
title: "", title: "",
value: "", value: "",
description: "", description: "",
@ -49,11 +55,9 @@ export default class TextInputDialog extends React.Component {
hasCancel: true, hasCancel: true,
}; };
constructor(props) { constructor(props: IProps) {
super(props); super(props);
this._field = createRef();
this.state = { this.state = {
value: this.props.value, value: this.props.value,
busy: false, busy: false,
@ -61,23 +65,23 @@ export default class TextInputDialog extends React.Component {
}; };
} }
componentDidMount() { public componentDidMount(): void {
if (this.props.focus) { if (this.props.focus) {
// Set the cursor at the end of the text input // Set the cursor at the end of the text input
// this._field.current.value = this.props.value; // this._field.current.value = this.props.value;
this._field.current.focus(); this.field.current.focus();
} }
} }
onOk = async ev => { private onOk = async (ev: React.FormEvent): Promise<void> => {
ev.preventDefault(); ev.preventDefault();
if (this.props.validator) { if (this.props.validator) {
this.setState({ busy: true }); this.setState({ busy: true });
await this._field.current.validate({ allowEmpty: false }); await this.field.current.validate({ allowEmpty: false });
if (!this._field.current.state.valid) { if (!this.field.current.state.valid) {
this._field.current.focus(); this.field.current.focus();
this._field.current.validate({ allowEmpty: false, focused: true }); this.field.current.validate({ allowEmpty: false, focused: true });
this.setState({ busy: false }); this.setState({ busy: false });
return; return;
} }
@ -85,17 +89,17 @@ export default class TextInputDialog extends React.Component {
this.props.onFinished(true, this.state.value); this.props.onFinished(true, this.state.value);
}; };
onCancel = () => { private onCancel = (): void => {
this.props.onFinished(false); this.props.onFinished(false);
}; };
onChange = ev => { private onChange = (ev: ChangeEvent<HTMLInputElement>): void => {
this.setState({ this.setState({
value: ev.target.value, value: ev.target.value,
}); });
}; };
onValidate = async fieldState => { private onValidate = async (fieldState: IFieldState): Promise<IValidationResult> => {
const result = await this.props.validator(fieldState); const result = await this.props.validator(fieldState);
this.setState({ this.setState({
valid: result.valid, valid: result.valid,
@ -103,9 +107,7 @@ export default class TextInputDialog extends React.Component {
return result; return result;
}; };
render() { public render(): JSX.Element {
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
const DialogButtons = sdk.getComponent('views.elements.DialogButtons');
return ( return (
<BaseDialog <BaseDialog
className="mx_TextInputDialog" className="mx_TextInputDialog"
@ -121,13 +123,12 @@ export default class TextInputDialog extends React.Component {
<div> <div>
<Field <Field
className="mx_TextInputDialog_input" className="mx_TextInputDialog_input"
ref={this._field} ref={this.field}
type="text" type="text"
label={this.props.placeholder} label={this.props.placeholder}
value={this.state.value} value={this.state.value}
onChange={this.onChange} onChange={this.onChange}
onValidate={this.props.validator ? this.onValidate : undefined} onValidate={this.props.validator ? this.onValidate : undefined}
size="64"
/> />
</div> </div>
</div> </div>