2020-04-10 18:09:29 -04:00
|
|
|
import "./TextInput.scss";
|
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-25 19:37:58 -03:00
|
|
|
label: string;
|
2020-01-05 22:26:00 +00:00
|
|
|
};
|
|
|
|
|
2020-01-29 02:25:47 +02:00
|
|
|
export class ProjectName extends Component<Props> {
|
2020-02-28 23:03:53 +01:00
|
|
|
private handleFocus = (event: React.FocusEvent<HTMLElement>) => {
|
|
|
|
selectNode(event.currentTarget);
|
2020-01-15 20:42:02 +05:00
|
|
|
};
|
|
|
|
|
2020-02-28 23:03:53 +01:00
|
|
|
private handleBlur = (event: React.FocusEvent<HTMLElement>) => {
|
|
|
|
const value = event.currentTarget.innerText.trim();
|
2020-02-02 20:04:35 +02:00
|
|
|
if (value !== this.props.value) {
|
|
|
|
this.props.onChange(value);
|
|
|
|
}
|
2020-01-15 20:42:02 +05:00
|
|
|
removeSelection();
|
|
|
|
};
|
|
|
|
|
2020-02-28 23:03:53 +01:00
|
|
|
private handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
event.preventDefault();
|
|
|
|
if (event.nativeEvent.isComposing || event.keyCode === 229) {
|
2020-02-17 17:44:08 +09:00
|
|
|
return;
|
|
|
|
}
|
2020-02-28 23:03:53 +01:00
|
|
|
event.currentTarget.blur();
|
2020-01-05 22:26:00 +00:00
|
|
|
}
|
2020-01-15 20:42:02 +05:00
|
|
|
};
|
2020-02-22 10:46:12 -05:00
|
|
|
private makeEditable = (editable: HTMLSpanElement | null) => {
|
|
|
|
if (!editable) {
|
|
|
|
return;
|
|
|
|
}
|
2020-02-21 22:51:34 -05:00
|
|
|
try {
|
|
|
|
editable.contentEditable = "plaintext-only";
|
|
|
|
} catch {
|
|
|
|
editable.contentEditable = "true";
|
|
|
|
}
|
|
|
|
};
|
2020-01-05 22:26:00 +00:00
|
|
|
|
|
|
|
public render() {
|
|
|
|
return (
|
2020-01-15 20:42:02 +05:00
|
|
|
<span
|
|
|
|
suppressContentEditableWarning
|
2020-02-21 22:51:34 -05:00
|
|
|
ref={this.makeEditable}
|
2020-01-15 20:42:02 +05:00
|
|
|
data-type="wysiwyg"
|
2020-04-10 18:09:29 -04:00
|
|
|
className="TextInput"
|
2020-01-25 19:37:58 -03:00
|
|
|
role="textbox"
|
|
|
|
aria-label={this.props.label}
|
2020-01-15 20:42:02 +05:00
|
|
|
onBlur={this.handleBlur}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
onFocus={this.handleFocus}
|
|
|
|
>
|
|
|
|
{this.props.value}
|
|
|
|
</span>
|
2020-01-05 22:26:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|