Submit a new display name on blur of input field

pull/21833/head
Luke Barnard 2017-03-02 17:29:06 +00:00
parent f85c452e35
commit 7a092e4ac7
3 changed files with 13 additions and 3 deletions

View File

@ -33,7 +33,10 @@ module.exports = React.createClass({
className: React.PropTypes.string, className: React.PropTypes.string,
labelClassName: React.PropTypes.string, labelClassName: React.PropTypes.string,
placeholderClassName: React.PropTypes.string, placeholderClassName: React.PropTypes.string,
// Overrides blurToSubmit if true
blurToCancel: React.PropTypes.bool, blurToCancel: React.PropTypes.bool,
// Will cause onValueChanged(value, true) to fire on blur
blurToSubmit: React.PropTypes.bool,
editable: React.PropTypes.bool, editable: React.PropTypes.bool,
}, },
@ -51,6 +54,7 @@ module.exports = React.createClass({
editable: true, editable: true,
className: "mx_EditableText", className: "mx_EditableText",
placeholderClassName: "mx_EditableText_placeholder", placeholderClassName: "mx_EditableText_placeholder",
blurToSubmit: false,
}; };
}, },
@ -182,9 +186,9 @@ module.exports = React.createClass({
} }
}, },
onFinish: function(ev) { onFinish: function(ev, shouldSubmit) {
var self = this; var self = this;
var submit = (ev.key === "Enter"); var submit = (ev.key === "Enter") || shouldSubmit;
this.setState({ this.setState({
phase: this.Phases.Display, phase: this.Phases.Display,
}, function() { }, function() {
@ -199,7 +203,7 @@ module.exports = React.createClass({
if (this.props.blurToCancel) if (this.props.blurToCancel)
{this.cancelEdit();} {this.cancelEdit();}
else else
{this.onFinish(ev);} {this.onFinish(ev, this.props.blurToSubmit);}
this.showPlaceholder(!this.value); this.showPlaceholder(!this.value);
}, },

View File

@ -116,6 +116,7 @@ export default class EditableTextContainer extends React.Component {
<EditableText initialValue={this.state.value} <EditableText initialValue={this.state.value}
placeholder={this.props.placeholder} placeholder={this.props.placeholder}
onValueChanged={this._onValueChanged} onValueChanged={this._onValueChanged}
blurToSubmit={this.props.blurToSubmit}
/> />
); );
} }
@ -137,11 +138,15 @@ EditableTextContainer.propTypes = {
/* callback to update the value. Called with a single argument: the new /* callback to update the value. Called with a single argument: the new
* value. */ * value. */
onSubmit: React.PropTypes.func, onSubmit: React.PropTypes.func,
/* should the input submit when focus is lost? */
blurToSubmit: React.PropTypes.bool,
}; };
EditableTextContainer.defaultProps = { EditableTextContainer.defaultProps = {
initialValue: "", initialValue: "",
placeholder: "", placeholder: "",
blurToSubmit: false,
onSubmit: function(v) {return q(); }, onSubmit: function(v) {return q(); },
}; };

View File

@ -53,6 +53,7 @@ module.exports = React.createClass({
<EditableTextContainer <EditableTextContainer
getInitialValue={this._getDisplayName} getInitialValue={this._getDisplayName}
placeholder="No display name" placeholder="No display name"
blurToSubmit={true}
onSubmit={this._changeDisplayName} /> onSubmit={this._changeDisplayName} />
); );
} }