2018-11-04 06:22:46 +00:00
|
|
|
import React, { PureComponent, createRef } from 'react';
|
2019-01-25 02:57:58 +00:00
|
|
|
import cn from 'classnames';
|
2018-04-05 19:13:32 +00:00
|
|
|
import { stringWidth } from 'utils';
|
2017-06-12 04:18:32 +00:00
|
|
|
|
|
|
|
export default class Editable extends PureComponent {
|
2017-06-21 05:23:07 +00:00
|
|
|
static defaultProps = {
|
|
|
|
editable: true
|
|
|
|
};
|
|
|
|
|
2018-11-04 06:22:46 +00:00
|
|
|
inputEl = createRef();
|
|
|
|
|
2017-06-21 05:23:07 +00:00
|
|
|
state = {
|
|
|
|
editing: false
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps, prevState) {
|
|
|
|
if (!prevState.editing && this.state.editing) {
|
|
|
|
// eslint-disable-next-line react/no-did-update-set-state
|
2018-05-25 02:12:02 +00:00
|
|
|
this.updateInputWidth(this.props.value);
|
2018-11-04 06:22:46 +00:00
|
|
|
this.inputEl.current.focus();
|
2018-11-29 12:06:37 +00:00
|
|
|
} else if (this.state.editing && prevProps.value !== this.props.value) {
|
2018-11-04 06:22:46 +00:00
|
|
|
this.updateInputWidth(this.props.value);
|
2017-06-21 05:23:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 02:12:02 +00:00
|
|
|
updateInputWidth = value => {
|
2018-11-04 06:22:46 +00:00
|
|
|
if (this.inputEl.current) {
|
|
|
|
const style = window.getComputedStyle(this.inputEl.current);
|
2018-05-25 02:12:02 +00:00
|
|
|
const padding = parseInt(style.paddingRight, 10);
|
2018-06-18 13:47:51 +00:00
|
|
|
// Make sure the width is at least 1px so the caret always shows
|
2018-05-06 19:36:05 +00:00
|
|
|
const width =
|
|
|
|
stringWidth(value, `${style.fontSize} ${style.fontFamily}`) || 1;
|
2018-05-25 02:12:02 +00:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
width: width + padding * 2,
|
|
|
|
indent: padding
|
|
|
|
});
|
2017-06-21 05:23:07 +00:00
|
|
|
}
|
2018-05-25 02:12:02 +00:00
|
|
|
};
|
2017-06-12 04:18:32 +00:00
|
|
|
|
|
|
|
startEditing = () => {
|
|
|
|
if (this.props.editable) {
|
|
|
|
this.initialValue = this.props.value;
|
|
|
|
this.setState({ editing: true });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
stopEditing = () => {
|
|
|
|
const { validate, value, onChange } = this.props;
|
|
|
|
if (validate && !validate(value)) {
|
|
|
|
onChange(this.initialValue);
|
|
|
|
}
|
|
|
|
this.setState({ editing: false });
|
|
|
|
};
|
|
|
|
|
2017-06-21 05:23:07 +00:00
|
|
|
handleBlur = e => {
|
|
|
|
const { onBlur } = this.props;
|
|
|
|
this.stopEditing();
|
|
|
|
if (onBlur) {
|
|
|
|
onBlur(e.target.value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
handleChange = e => this.props.onChange(e.target.value);
|
|
|
|
|
2017-06-12 04:18:32 +00:00
|
|
|
handleKey = e => {
|
|
|
|
if (e.key === 'Enter') {
|
2017-06-21 05:23:07 +00:00
|
|
|
this.handleBlur(e);
|
2017-06-12 04:18:32 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-06 19:36:05 +00:00
|
|
|
handleFocus = e => {
|
|
|
|
const val = e.target.value;
|
|
|
|
e.target.value = '';
|
|
|
|
e.target.value = val;
|
|
|
|
};
|
|
|
|
|
2017-06-12 04:18:32 +00:00
|
|
|
render() {
|
2019-01-25 02:57:58 +00:00
|
|
|
const { children, className, editable, value } = this.props;
|
2017-06-21 05:23:07 +00:00
|
|
|
|
|
|
|
const style = {
|
2018-05-25 02:12:02 +00:00
|
|
|
width: this.state.width,
|
|
|
|
textIndent: this.state.indent,
|
|
|
|
paddingLeft: 0
|
2017-06-21 05:23:07 +00:00
|
|
|
};
|
|
|
|
|
2018-04-05 23:46:22 +00:00
|
|
|
return this.state.editing ? (
|
|
|
|
<input
|
2018-11-04 06:22:46 +00:00
|
|
|
ref={this.inputEl}
|
2019-01-25 02:57:58 +00:00
|
|
|
className={`editable-wrap ${className}`}
|
2018-04-05 23:46:22 +00:00
|
|
|
type="text"
|
|
|
|
value={value}
|
|
|
|
onBlur={this.handleBlur}
|
|
|
|
onChange={this.handleChange}
|
|
|
|
onKeyDown={this.handleKey}
|
2018-05-06 19:36:05 +00:00
|
|
|
onFocus={this.handleFocus}
|
2018-04-05 23:46:22 +00:00
|
|
|
style={style}
|
|
|
|
spellCheck={false}
|
|
|
|
/>
|
|
|
|
) : (
|
2019-01-25 02:57:58 +00:00
|
|
|
<div
|
|
|
|
className={cn('editable-wrap', {
|
|
|
|
'editable-wrap-editable': editable
|
|
|
|
})}
|
|
|
|
onClick={this.startEditing}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</div>
|
2017-06-12 04:18:32 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|