ProjectName.tsx 1.5 KB

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