You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
4 years ago
|
import "./TextInput.scss";
|
||
|
|
||
4 years ago
|
import React, { useState } from "react";
|
||
4 years ago
|
import { focusNearestParent } from "../utils";
|
||
4 years ago
|
|
||
4 years ago
|
import "./ProjectName.scss";
|
||
4 years ago
|
import { useExcalidrawContainer } from "./App";
|
||
2 years ago
|
import { KEYS } from "../keys";
|
||
4 years ago
|
|
||
4 years ago
|
type Props = {
|
||
|
value: string;
|
||
|
onChange: (value: string) => void;
|
||
|
label: string;
|
||
4 years ago
|
isNameEditable: boolean;
|
||
2 years ago
|
ignoreFocus?: boolean;
|
||
4 years ago
|
};
|
||
|
|
||
4 years ago
|
export const ProjectName = (props: Props) => {
|
||
4 years ago
|
const { id } = useExcalidrawContainer();
|
||
4 years ago
|
const [fileName, setFileName] = useState<string>(props.value);
|
||
|
|
||
|
const handleBlur = (event: any) => {
|
||
2 years ago
|
if (!props.ignoreFocus) {
|
||
|
focusNearestParent(event.target);
|
||
|
}
|
||
4 years ago
|
const value = event.target.value;
|
||
4 years ago
|
if (value !== props.value) {
|
||
|
props.onChange(value);
|
||
4 years ago
|
}
|
||
|
};
|
||
|
|
||
4 years ago
|
const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
|
||
2 years ago
|
if (event.key === KEYS.ENTER) {
|
||
4 years ago
|
event.preventDefault();
|
||
|
if (event.nativeEvent.isComposing || event.keyCode === 229) {
|
||
|
return;
|
||
|
}
|
||
|
event.currentTarget.blur();
|
||
|
}
|
||
|
};
|
||
|
|
||
4 years ago
|
return (
|
||
|
<div className="ProjectName">
|
||
|
<label className="ProjectName-label" htmlFor="filename">
|
||
|
{`${props.label}${props.isNameEditable ? "" : ":"}`}
|
||
|
</label>
|
||
|
{props.isNameEditable ? (
|
||
|
<input
|
||
3 years ago
|
type="text"
|
||
4 years ago
|
className="TextInput"
|
||
|
onBlur={handleBlur}
|
||
|
onKeyDown={handleKeyDown}
|
||
4 years ago
|
id={`${id}-filename`}
|
||
4 years ago
|
value={fileName}
|
||
|
onChange={(event) => setFileName(event.target.value)}
|
||
|
/>
|
||
|
) : (
|
||
4 years ago
|
<span className="TextInput TextInput--readonly" id={`${id}-filename`}>
|
||
4 years ago
|
{props.value}
|
||
|
</span>
|
||
|
)}
|
||
|
</div>
|
||
|
);
|
||
|
};
|