2016-07-08 06:02:40 +00:00
|
|
|
import React, {Component, PropTypes} from 'react';
|
|
|
|
|
|
|
|
class Editable extends Component {
|
2016-11-26 08:29:16 +00:00
|
|
|
state = {editing: false};
|
2016-07-08 06:02:40 +00:00
|
|
|
|
2016-11-29 21:28:22 +00:00
|
|
|
_handleSetInputRef = n => this._input = n;
|
|
|
|
|
|
|
|
_handleSingleClickEditStart = () => {
|
|
|
|
if (this.props.singleClick) {
|
|
|
|
this._handleEditStart();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_handleEditStart = () => {
|
2016-07-08 06:02:40 +00:00
|
|
|
this.setState({editing: true});
|
|
|
|
|
|
|
|
setTimeout(() => {
|
2016-08-15 17:04:36 +00:00
|
|
|
this._input && this._input.focus();
|
|
|
|
this._input && this._input.select();
|
2016-07-08 06:02:40 +00:00
|
|
|
});
|
2016-11-23 19:33:24 +00:00
|
|
|
|
|
|
|
if (this.props.onEditStart) {
|
|
|
|
this.props.onEditStart();
|
|
|
|
}
|
2016-11-29 21:28:22 +00:00
|
|
|
};
|
2016-07-08 06:02:40 +00:00
|
|
|
|
2016-12-21 23:37:48 +00:00
|
|
|
_handleEditEnd = () => {
|
2016-08-15 17:04:36 +00:00
|
|
|
const value = this._input.value.trim();
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
// Don't do anything if it's empty
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-10 21:03:12 +00:00
|
|
|
this.props.onSubmit(value);
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
// This timeout prevents the UI from showing the old value after submit.
|
|
|
|
// It should give the UI enough time to redraw the new value.
|
2016-12-21 23:37:48 +00:00
|
|
|
setTimeout(async () => this.setState({editing: false}), 100);
|
2016-11-29 21:28:22 +00:00
|
|
|
};
|
2016-07-08 06:02:40 +00:00
|
|
|
|
2016-11-29 21:28:22 +00:00
|
|
|
_handleEditKeyDown = e => {
|
2016-07-08 06:02:40 +00:00
|
|
|
if (e.keyCode === 13) {
|
|
|
|
// Pressed Enter
|
|
|
|
this._handleEditEnd();
|
|
|
|
} else if (e.keyCode === 27) {
|
|
|
|
// Pressed Escape
|
2016-11-10 21:03:12 +00:00
|
|
|
// NOTE: This blur causes a save because we save on blur
|
|
|
|
// TODO: Make escape blur without saving
|
2016-08-15 17:04:36 +00:00
|
|
|
this._input && this._input.blur();
|
2016-07-08 06:02:40 +00:00
|
|
|
}
|
2016-11-29 21:28:22 +00:00
|
|
|
};
|
2016-07-08 06:02:40 +00:00
|
|
|
|
|
|
|
render () {
|
2016-11-23 19:33:24 +00:00
|
|
|
const {value, singleClick, onEditStart, ...extra} = this.props;
|
2016-07-08 06:02:40 +00:00
|
|
|
const {editing} = this.state;
|
|
|
|
|
|
|
|
if (editing) {
|
|
|
|
return (
|
|
|
|
<input
|
2016-08-15 17:04:36 +00:00
|
|
|
className="editable"
|
2016-07-08 06:02:40 +00:00
|
|
|
type="text"
|
2016-11-29 21:28:22 +00:00
|
|
|
ref={this._handleSetInputRef}
|
2016-07-08 06:02:40 +00:00
|
|
|
defaultValue={value}
|
2016-11-29 21:28:22 +00:00
|
|
|
onKeyDown={this._handleEditKeyDown}
|
|
|
|
onBlur={this._handleEditEnd}
|
2016-07-08 06:02:40 +00:00
|
|
|
{...extra}
|
|
|
|
/>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
2016-08-15 17:04:36 +00:00
|
|
|
<div className="editable"
|
2016-11-29 21:28:22 +00:00
|
|
|
onClick={this._handleSingleClickEditStart}
|
|
|
|
onDoubleClick={this._handleEditStart}
|
|
|
|
{...extra}>
|
2016-08-15 17:04:36 +00:00
|
|
|
{value}
|
|
|
|
</div>
|
2016-07-08 06:02:40 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Editable.propTypes = {
|
|
|
|
onSubmit: PropTypes.func.isRequired,
|
2016-08-15 17:04:36 +00:00
|
|
|
value: PropTypes.string.isRequired,
|
|
|
|
|
|
|
|
// Optional
|
2016-11-23 19:33:24 +00:00
|
|
|
singleClick: PropTypes.bool,
|
|
|
|
onEditStart: PropTypes.func,
|
2016-08-15 17:04:36 +00:00
|
|
|
};
|
2016-07-08 06:02:40 +00:00
|
|
|
|
|
|
|
export default Editable;
|