2020-01-15 20:42:02 +05:00
|
|
|
import "./EditableText.css";
|
2020-01-05 22:26:00 +00:00
|
|
|
|
2020-01-15 20:42:02 +05:00
|
|
|
import React, { Component } from "react";
|
|
|
|
import { selectNode, removeSelection } from "../utils";
|
2020-01-05 22:26:00 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
value: string;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
};
|
|
|
|
|
2020-01-15 20:42:02 +05:00
|
|
|
export class EditableText extends Component<Props> {
|
|
|
|
private handleFocus = (e: React.FocusEvent<HTMLElement>) => {
|
|
|
|
selectNode(e.currentTarget);
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleBlur = (e: React.FocusEvent<HTMLElement>) => {
|
|
|
|
const value = e.currentTarget.innerText.trim();
|
|
|
|
if (value !== this.props.value) this.props.onChange(value);
|
|
|
|
removeSelection();
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleKeyDown = (e: React.KeyboardEvent<HTMLElement>) => {
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
e.preventDefault();
|
|
|
|
e.currentTarget.blur();
|
2020-01-05 22:26:00 +00:00
|
|
|
}
|
2020-01-15 20:42:02 +05:00
|
|
|
};
|
2020-01-05 22:26:00 +00:00
|
|
|
|
|
|
|
public render() {
|
|
|
|
return (
|
2020-01-15 20:42:02 +05:00
|
|
|
<span
|
|
|
|
suppressContentEditableWarning
|
|
|
|
contentEditable="true"
|
|
|
|
data-type="wysiwyg"
|
|
|
|
className="project-name"
|
|
|
|
onBlur={this.handleBlur}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
onFocus={this.handleFocus}
|
|
|
|
>
|
|
|
|
{this.props.value}
|
|
|
|
</span>
|
2020-01-05 22:26:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|