2020-12-01 14:00:13 +01:00
|
|
|
import "./TextInput.scss";
|
|
|
|
|
2021-06-04 21:22:09 +05:30
|
|
|
import React, { useState } from "react";
|
2021-04-13 01:29:25 +05:30
|
|
|
import { focusNearestParent } from "../utils";
|
2020-12-01 14:00:13 +01:00
|
|
|
|
2021-05-25 21:37:14 +02:00
|
|
|
import "./ProjectName.scss";
|
2021-06-10 02:46:56 +05:30
|
|
|
import { useExcalidrawContainer } from "./App";
|
2021-05-25 21:37:14 +02:00
|
|
|
|
2020-12-01 14:00:13 +01:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2021-06-04 21:22:09 +05:30
|
|
|
export const ProjectName = (props: Props) => {
|
2021-06-10 02:46:56 +05:30
|
|
|
const { id } = useExcalidrawContainer();
|
2021-06-04 21:22:09 +05:30
|
|
|
const [fileName, setFileName] = useState<string>(props.value);
|
|
|
|
|
|
|
|
const handleBlur = (event: any) => {
|
2021-04-13 01:29:25 +05:30
|
|
|
focusNearestParent(event.target);
|
2021-03-20 21:57:58 +05:30
|
|
|
const value = event.target.value;
|
2021-06-04 21:22:09 +05:30
|
|
|
if (value !== props.value) {
|
|
|
|
props.onChange(value);
|
2020-12-01 14:00:13 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-04 21:22:09 +05:30
|
|
|
const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
|
2020-12-01 14:00:13 +01:00
|
|
|
if (event.key === "Enter") {
|
|
|
|
event.preventDefault();
|
|
|
|
if (event.nativeEvent.isComposing || event.keyCode === 229) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
event.currentTarget.blur();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-04 21:22:09 +05:30
|
|
|
return (
|
|
|
|
<div className="ProjectName">
|
|
|
|
<label className="ProjectName-label" htmlFor="filename">
|
|
|
|
{`${props.label}${props.isNameEditable ? "" : ":"}`}
|
|
|
|
</label>
|
|
|
|
{props.isNameEditable ? (
|
|
|
|
<input
|
|
|
|
className="TextInput"
|
|
|
|
onBlur={handleBlur}
|
|
|
|
onKeyDown={handleKeyDown}
|
2021-06-10 02:46:56 +05:30
|
|
|
id={`${id}-filename`}
|
2021-06-04 21:22:09 +05:30
|
|
|
value={fileName}
|
|
|
|
onChange={(event) => setFileName(event.target.value)}
|
|
|
|
/>
|
|
|
|
) : (
|
2021-06-10 02:46:56 +05:30
|
|
|
<span className="TextInput TextInput--readonly" id={`${id}-filename`}>
|
2021-06-04 21:22:09 +05:30
|
|
|
{props.value}
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|