Files
ThreadNet-Web/src/components/views/dialogs/TextInputDialog.js
T

144 lines
4.6 KiB
JavaScript
Raw Normal View History

2016-01-13 16:32:41 +01:00
/*
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.
*/
2019-12-08 12:16:17 +00:00
import React, {createRef} from 'react';
2017-12-26 14:03:18 +13:00
import PropTypes from 'prop-types';
2019-12-19 18:19:56 -07:00
import * as sdk from '../../../index';
2020-03-13 17:32:02 +00:00
import Field from "../elements/Field";
import { _t } from '../../../languageHandler';
2016-01-13 16:32:41 +01:00
2020-08-29 12:14:16 +01:00
export default class TextInputDialog extends React.Component {
static propTypes = {
2017-12-26 14:03:18 +13:00
title: PropTypes.string,
description: PropTypes.oneOfType([
PropTypes.element,
PropTypes.string,
2016-03-18 11:20:00 +00:00
]),
2017-12-26 14:03:18 +13:00
value: PropTypes.string,
placeholder: PropTypes.string,
2017-12-26 14:03:18 +13:00
button: PropTypes.string,
busyMessage: PropTypes.string,
2017-12-26 14:03:18 +13:00
focus: PropTypes.bool,
onFinished: PropTypes.func.isRequired,
hasCancel: PropTypes.bool,
2020-03-13 17:32:02 +00:00
validator: PropTypes.func, // result of withValidation
2020-03-16 12:00:56 +00:00
fixedWidth: PropTypes.bool,
2020-08-29 12:14:16 +01:00
};
2016-01-13 16:32:41 +01:00
2020-08-29 12:14:16 +01:00
static defaultProps = {
title: "",
value: "",
description: "",
busyMessage: "Loading...",
2020-08-29 12:14:16 +01:00
focus: true,
hasCancel: true,
};
2020-08-29 12:14:16 +01:00
constructor(props) {
super(props);
this._field = createRef();
this.state = {
2020-03-13 17:32:02 +00:00
value: this.props.value,
busy: false,
2020-03-13 17:32:02 +00:00
valid: false,
};
2020-08-29 12:14:16 +01:00
}
2020-03-13 17:32:02 +00:00
2020-08-29 12:14:16 +01:00
componentDidMount() {
2016-01-13 16:32:41 +01:00
if (this.props.focus) {
// Set the cursor at the end of the text input
2020-03-13 17:32:02 +00:00
// this._field.current.value = this.props.value;
this._field.current.focus();
2016-01-13 16:32:41 +01:00
}
2020-08-29 12:14:16 +01:00
}
2016-01-13 16:32:41 +01:00
2020-08-29 12:14:16 +01:00
onOk = async ev => {
2020-03-13 17:32:02 +00:00
ev.preventDefault();
if (this.props.validator) {
this.setState({ busy: true });
2020-03-13 17:32:02 +00:00
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 });
2020-03-13 17:32:02 +00:00
return;
}
}
this.props.onFinished(true, this.state.value);
2020-08-29 12:14:16 +01:00
};
2016-01-13 16:32:41 +01:00
2020-08-29 12:14:16 +01:00
onCancel = () => {
2016-01-13 16:32:41 +01:00
this.props.onFinished(false);
2020-08-29 12:14:16 +01:00
};
2016-01-13 16:32:41 +01:00
2020-08-29 12:14:16 +01:00
onChange = ev => {
2020-03-13 17:32:02 +00:00
this.setState({
value: ev.target.value,
});
2020-08-29 12:14:16 +01:00
};
2020-03-13 17:32:02 +00:00
2020-08-29 12:14:16 +01:00
onValidate = async fieldState => {
2020-03-13 17:32:02 +00:00
const result = await this.props.validator(fieldState);
this.setState({
valid: result.valid,
});
return result;
2020-08-29 12:14:16 +01:00
};
2020-03-13 17:32:02 +00:00
2020-08-29 12:14:16 +01:00
render() {
2017-01-24 15:54:05 +00:00
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
2017-12-23 16:50:35 +13:00
const DialogButtons = sdk.getComponent('views.elements.DialogButtons');
2016-01-13 16:32:41 +01:00
return (
2020-03-16 12:00:56 +00:00
<BaseDialog
className="mx_TextInputDialog"
onFinished={this.props.onFinished}
2017-01-24 15:54:05 +00:00
title={this.props.title}
2020-03-16 12:00:56 +00:00
fixedWidth={this.props.fixedWidth}
2017-01-24 15:54:05 +00:00
>
<form onSubmit={this.onOk}>
<div className="mx_Dialog_content">
<div className="mx_TextInputDialog_label">
<label htmlFor="textinput"> { this.props.description } </label>
</div>
<div>
2020-03-13 17:32:02 +00:00
<Field
2019-12-08 12:16:17 +00:00
className="mx_TextInputDialog_input"
2020-03-13 17:32:02 +00:00
ref={this._field}
type="text"
label={this.props.placeholder}
value={this.state.value}
onChange={this.onChange}
onValidate={this.props.validator ? this.onValidate : undefined}
size="64"
/>
</div>
2016-01-13 16:32:41 +01:00
</div>
</form>
<DialogButtons
primaryButton={this.state.busy ? _t(this.props.busyMessage) : this.props.button}
disabled={this.state.busy}
2017-12-23 16:50:35 +13:00
onPrimaryButtonClick={this.onOk}
onCancel={this.onCancel}
hasCancel={this.props.hasCancel}
/>
2017-01-24 15:54:05 +00:00
</BaseDialog>
2016-01-13 16:32:41 +01:00
);
2020-08-29 12:14:16 +01:00
}
}