/* Copyright 2015, 2016 OpenMarket Ltd Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React, { ChangeEvent, createRef } from 'react'; import Field from "../elements/Field"; import { _t, _td } from '../../../languageHandler'; import { replaceableComponent } from "../../../utils/replaceableComponent"; import { IFieldState, IValidationResult } from "../elements/Validation"; import BaseDialog from "./BaseDialog"; import DialogButtons from "../elements/DialogButtons"; import { IDialogProps } from "./IDialogProps"; interface IProps extends IDialogProps { title?: string; description?: React.ReactNode; value?: string; placeholder?: string; button?: string; busyMessage?: string; // pass _td string focus?: boolean; hasCancel?: boolean; validator?: (fieldState: IFieldState) => IValidationResult; // result of withValidation fixedWidth?: boolean; } interface IState { value: string; busy: boolean; valid: boolean; } @replaceableComponent("views.dialogs.TextInputDialog") export default class TextInputDialog extends React.Component { private field = createRef(); public static defaultProps = { title: "", value: "", description: "", busyMessage: _td("Loading..."), focus: true, hasCancel: true, }; constructor(props: IProps) { super(props); this.state = { value: this.props.value, busy: false, valid: false, }; } public componentDidMount(): void { if (this.props.focus) { // Set the cursor at the end of the text input // this._field.current.value = this.props.value; this.field.current.focus(); } } private onOk = async (ev: React.FormEvent): Promise => { ev.preventDefault(); if (this.props.validator) { this.setState({ busy: true }); await this.field.current.validate({ allowEmpty: false }); if (!this.field.current.state.valid) { this.field.current.focus(); this.field.current.validate({ allowEmpty: false, focused: true }); this.setState({ busy: false }); return; } } this.props.onFinished(true, this.state.value); }; private onCancel = (): void => { this.props.onFinished(false); }; private onChange = (ev: ChangeEvent): void => { this.setState({ value: ev.target.value, }); }; private onValidate = async (fieldState: IFieldState): Promise => { const result = await this.props.validator(fieldState); this.setState({ valid: result.valid, }); return result; }; public render(): JSX.Element { return (
); } }