Merge EditableText component
parent
8bde761a8a
commit
17d789eb97
|
@ -18,7 +18,8 @@ limitations under the License.
|
||||||
|
|
||||||
var React = require('react');
|
var React = require('react');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = React.createClass({
|
||||||
|
displayName: 'EditableText',
|
||||||
propTypes: {
|
propTypes: {
|
||||||
onValueChanged: React.PropTypes.func,
|
onValueChanged: React.PropTypes.func,
|
||||||
initialValue: React.PropTypes.string,
|
initialValue: React.PropTypes.string,
|
||||||
|
@ -85,4 +86,54 @@ module.exports = {
|
||||||
onValueChanged: function(shouldSubmit) {
|
onValueChanged: function(shouldSubmit) {
|
||||||
this.props.onValueChanged(this.state.value, shouldSubmit);
|
this.props.onValueChanged(this.state.value, shouldSubmit);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
onKeyUp: function(ev) {
|
||||||
|
if (ev.key == "Enter") {
|
||||||
|
this.onFinish(ev);
|
||||||
|
} else if (ev.key == "Escape") {
|
||||||
|
this.cancelEdit();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
onClickDiv: function() {
|
||||||
|
this.setState({
|
||||||
|
phase: this.Phases.Edit,
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
onFocus: function(ev) {
|
||||||
|
ev.target.setSelectionRange(0, ev.target.value.length);
|
||||||
|
},
|
||||||
|
|
||||||
|
onFinish: function(ev) {
|
||||||
|
if (ev.target.value) {
|
||||||
|
this.setValue(ev.target.value, ev.key === "Enter");
|
||||||
|
} else {
|
||||||
|
this.cancelEdit();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
render: function() {
|
||||||
|
var editable_el;
|
||||||
|
|
||||||
|
if (this.state.phase == this.Phases.Display) {
|
||||||
|
if (this.state.value) {
|
||||||
|
editable_el = <div ref="display_div" onClick={this.onClickDiv}>{this.state.value}</div>;
|
||||||
|
} else {
|
||||||
|
editable_el = <div ref="display_div" onClick={this.onClickDiv}>{this.props.label}</div>;
|
||||||
|
}
|
||||||
|
} else if (this.state.phase == this.Phases.Edit) {
|
||||||
|
editable_el = (
|
||||||
|
<div>
|
||||||
|
<input type="text" defaultValue={this.state.value} onKeyUp={this.onKeyUp} onFocus={this.onFocus} onBlur={this.onFinish} placeholder={this.props.placeHolder} autoFocus/>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mx_EditableText">
|
||||||
|
{editable_el}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
Loading…
Reference in New Issue