excalidraw/src/components/ProjectName.tsx

60 lines
1.4 KiB
TypeScript
Raw Normal View History

2020-01-29 02:25:47 +02:00
import "./ProjectName.css";
import React, { Component } from "react";
import { selectNode, removeSelection } from "../utils";
type Props = {
value: string;
onChange: (value: string) => void;
label: string;
};
2020-01-29 02:25:47 +02:00
export class ProjectName 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();
if (e.nativeEvent.isComposing || e.keyCode === 229) {
return;
}
e.currentTarget.blur();
}
};
2020-02-21 22:51:34 -05:00
private makeEditable = (editable: HTMLSpanElement) => {
try {
editable.contentEditable = "plaintext-only";
} catch {
editable.contentEditable = "true";
}
};
public render() {
return (
<span
suppressContentEditableWarning
2020-02-21 22:51:34 -05:00
ref={this.makeEditable}
data-type="wysiwyg"
2020-01-29 02:25:47 +02:00
className="ProjectName"
role="textbox"
aria-label={this.props.label}
onBlur={this.handleBlur}
onKeyDown={this.handleKeyDown}
onFocus={this.handleFocus}
>
{this.props.value}
</span>
);
}
}