2020-01-05 22:26:00 +00:00
|
|
|
import React, { Fragment, Component } from "react";
|
|
|
|
|
|
|
|
type InputState = {
|
|
|
|
value: string;
|
|
|
|
edit: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
value: string;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
};
|
|
|
|
|
2020-01-06 21:29:44 +04:00
|
|
|
export class EditableText extends Component<Props, InputState> {
|
2020-01-05 22:26:00 +00:00
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
value: props.value,
|
|
|
|
edit: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-01-05 14:45:16 -08:00
|
|
|
UNSAFE_componentWillReceiveProps(props: Props) {
|
2020-01-05 22:26:00 +00:00
|
|
|
this.setState({ value: props.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleEdit(e: React.ChangeEvent<HTMLInputElement>) {
|
|
|
|
this.setState({ value: e.target.value });
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleBlur() {
|
|
|
|
const { value } = this.state;
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
this.setState({ value: this.props.value, edit: false });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.props.onChange(value);
|
|
|
|
this.setState({ edit: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
const { value, edit } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
{edit ? (
|
|
|
|
<input
|
|
|
|
className="project-name-input"
|
|
|
|
name="name"
|
|
|
|
maxLength={25}
|
|
|
|
value={value}
|
|
|
|
onChange={e => this.handleEdit(e)}
|
|
|
|
onBlur={() => this.handleBlur()}
|
2020-01-05 14:45:16 -08:00
|
|
|
onKeyDown={e => {
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
this.handleBlur();
|
|
|
|
}
|
|
|
|
}}
|
2020-01-05 22:26:00 +00:00
|
|
|
autoFocus
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<span
|
|
|
|
onClick={() => this.setState({ edit: true })}
|
|
|
|
className="project-name"
|
|
|
|
>
|
|
|
|
{value}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|