2020-12-01 14:00:13 +01:00
|
|
|
import "./TextInput.scss";
|
|
|
|
|
|
|
|
import React, { Component } from "react";
|
|
|
|
import { selectNode, removeSelection } from "../utils";
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
value: string;
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
label: string;
|
2021-03-20 16:08:03 +05:30
|
|
|
isNameEditable: boolean;
|
2020-12-01 14:00:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export class ProjectName extends Component<Props> {
|
|
|
|
private handleFocus = (event: React.FocusEvent<HTMLElement>) => {
|
|
|
|
selectNode(event.currentTarget);
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleBlur = (event: React.FocusEvent<HTMLElement>) => {
|
|
|
|
const value = event.currentTarget.innerText.trim();
|
|
|
|
if (value !== this.props.value) {
|
|
|
|
this.props.onChange(value);
|
|
|
|
}
|
|
|
|
removeSelection();
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
event.preventDefault();
|
|
|
|
if (event.nativeEvent.isComposing || event.keyCode === 229) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.currentTarget.blur();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private makeEditable = (editable: HTMLSpanElement | null) => {
|
|
|
|
if (!editable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
editable.contentEditable = "plaintext-only";
|
|
|
|
} catch {
|
|
|
|
editable.contentEditable = "true";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public render() {
|
2021-03-20 16:08:03 +05:30
|
|
|
return this.props.isNameEditable ? (
|
2020-12-01 14:00:13 +01:00
|
|
|
<span
|
|
|
|
suppressContentEditableWarning
|
|
|
|
ref={this.makeEditable}
|
|
|
|
data-type="wysiwyg"
|
|
|
|
className="TextInput"
|
|
|
|
role="textbox"
|
|
|
|
aria-label={this.props.label}
|
|
|
|
onBlur={this.handleBlur}
|
|
|
|
onKeyDown={this.handleKeyDown}
|
|
|
|
onFocus={this.handleFocus}
|
|
|
|
>
|
|
|
|
{this.props.value}
|
|
|
|
</span>
|
2021-03-20 16:08:03 +05:30
|
|
|
) : (
|
|
|
|
<span
|
|
|
|
className="TextInput TextInput--readonly"
|
|
|
|
aria-label={this.props.label}
|
|
|
|
>
|
|
|
|
{this.props.value}
|
|
|
|
</span>
|
2020-12-01 14:00:13 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|