From 17d2998ec1855611d5eed661482d47709ad59c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Wed, 15 Sep 2021 20:22:09 +0200 Subject: [PATCH] Convert EditableTextContainer to TS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- ...Container.js => EditableTextContainer.tsx} | 121 +++++++++--------- 1 file changed, 60 insertions(+), 61 deletions(-) rename src/components/views/elements/{EditableTextContainer.js => EditableTextContainer.tsx} (65%) diff --git a/src/components/views/elements/EditableTextContainer.js b/src/components/views/elements/EditableTextContainer.tsx similarity index 65% rename from src/components/views/elements/EditableTextContainer.js rename to src/components/views/elements/EditableTextContainer.tsx index 5778446355..9610b188fb 100644 --- a/src/components/views/elements/EditableTextContainer.js +++ b/src/components/views/elements/EditableTextContainer.tsx @@ -15,9 +15,34 @@ limitations under the License. */ import React from 'react'; -import PropTypes from 'prop-types'; -import * as sdk from '../../../index'; import { replaceableComponent } from "../../../utils/replaceableComponent"; +import Spinner from "./Spinner"; +import EditableText from "./EditableText"; + +interface IProps { + /* callback to retrieve the initial value. */ + getInitialValue?: () => Promise; + + /* initial value; used if getInitialValue is not given */ + initialValue?: string; + + /* placeholder text to use when the value is empty (and not being + * edited) */ + placeholder?: string; + + /* callback to update the value. Called with a single argument: the new + * value. */ + onSubmit?: (value: string) => Promise<{} | void>; + + /* should the input submit when focus is lost? */ + blurToSubmit?: boolean; +} + +interface IState { + busy: boolean; + errorString: string; + value: string; +} /** * A component which wraps an EditableText, with a spinner while updates take @@ -31,50 +56,51 @@ import { replaceableComponent } from "../../../utils/replaceableComponent"; * taken from the 'initialValue' property. */ @replaceableComponent("views.elements.EditableTextContainer") -export default class EditableTextContainer extends React.Component { - constructor(props) { +export default class EditableTextContainer extends React.Component { + private _unmounted = false; + public static defaultProps: Partial = { + initialValue: "", + placeholder: "", + blurToSubmit: false, + onSubmit: () => { return Promise.resolve(); }, + }; + + constructor(props: IProps) { super(props); - this._unmounted = false; this.state = { busy: false, errorString: null, value: props.initialValue, }; - this._onValueChanged = this._onValueChanged.bind(this); } - componentDidMount() { - if (this.props.getInitialValue === undefined) { - // use whatever was given in the initialValue property. - return; - } + public async componentDidMount(): Promise { + // use whatever was given in the initialValue property. + if (this.props.getInitialValue === undefined) return; this.setState({ busy: true }); - - this.props.getInitialValue().then( - (result) => { - if (this._unmounted) { return; } - this.setState({ - busy: false, - value: result, - }); - }, - (error) => { - if (this._unmounted) { return; } - this.setState({ - errorString: error.toString(), - busy: false, - }); - }, - ); + try { + const initialValue = await this.props.getInitialValue(); + if (this._unmounted) return; + this.setState({ + busy: false, + value: initialValue, + }); + } catch (error) { + if (this._unmounted) return; + this.setState({ + errorString: error.toString(), + busy: false, + }); + } } - componentWillUnmount() { + public componentWillUnmount(): void { this._unmounted = true; } - _onValueChanged(value, shouldSubmit) { + private onValueChanged = (value: string, shouldSubmit: boolean): void => { if (!shouldSubmit) { return; } @@ -100,24 +126,22 @@ export default class EditableTextContainer extends React.Component { }); }, ); - } + }; - render() { + public render(): JSX.Element { if (this.state.busy) { - const Loader = sdk.getComponent("elements.Spinner"); return ( - + ); } else if (this.state.errorString) { return (
{ this.state.errorString }
); } else { - const EditableText = sdk.getComponent('elements.EditableText'); return ( ); @@ -125,28 +149,3 @@ export default class EditableTextContainer extends React.Component { } } -EditableTextContainer.propTypes = { - /* callback to retrieve the initial value. */ - getInitialValue: PropTypes.func, - - /* initial value; used if getInitialValue is not given */ - initialValue: PropTypes.string, - - /* placeholder text to use when the value is empty (and not being - * edited) */ - placeholder: PropTypes.string, - - /* callback to update the value. Called with a single argument: the new - * value. */ - onSubmit: PropTypes.func, - - /* should the input submit when focus is lost? */ - blurToSubmit: PropTypes.bool, -}; - -EditableTextContainer.defaultProps = { - initialValue: "", - placeholder: "", - blurToSubmit: false, - onSubmit: function(v) {return Promise.resolve(); }, -};