ProjectName.tsx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import "./TextInput.scss";
  2. import React, { useState } from "react";
  3. import { focusNearestParent } from "../utils";
  4. import "./ProjectName.scss";
  5. import { useExcalidrawContainer } from "./App";
  6. type Props = {
  7. value: string;
  8. onChange: (value: string) => void;
  9. label: string;
  10. isNameEditable: boolean;
  11. };
  12. export const ProjectName = (props: Props) => {
  13. const { id } = useExcalidrawContainer();
  14. const [fileName, setFileName] = useState<string>(props.value);
  15. const handleBlur = (event: any) => {
  16. focusNearestParent(event.target);
  17. const value = event.target.value;
  18. if (value !== props.value) {
  19. props.onChange(value);
  20. }
  21. };
  22. const handleKeyDown = (event: React.KeyboardEvent<HTMLElement>) => {
  23. if (event.key === "Enter") {
  24. event.preventDefault();
  25. if (event.nativeEvent.isComposing || event.keyCode === 229) {
  26. return;
  27. }
  28. event.currentTarget.blur();
  29. }
  30. };
  31. return (
  32. <div className="ProjectName">
  33. <label className="ProjectName-label" htmlFor="filename">
  34. {`${props.label}${props.isNameEditable ? "" : ":"}`}
  35. </label>
  36. {props.isNameEditable ? (
  37. <input
  38. type="text"
  39. className="TextInput"
  40. onBlur={handleBlur}
  41. onKeyDown={handleKeyDown}
  42. id={`${id}-filename`}
  43. value={fileName}
  44. onChange={(event) => setFileName(event.target.value)}
  45. />
  46. ) : (
  47. <span className="TextInput TextInput--readonly" id={`${id}-filename`}>
  48. {props.value}
  49. </span>
  50. )}
  51. </div>
  52. );
  53. };