# All files / src/components/editable EditableTextField.tsx

52.17% Statements 12/23
36.66% Branches 11/30
25% Functions 2/8
54.54% Lines 12/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163

    13x

    13x

    13x

    13x
    13x

    13x
    1x

    1x

    1x

    12x

    12x
    12x

     import { useEffect, useRef, type JSX, type ReactNode } from "react";
    import { useEditableField } from "./useEditableField.js";
    import styles from "./EditableField.module.scss";

    /** Props for EditableTextField. */
    export interface EditableTextFieldProps {
      /** Current persisted value. */
      value: string;
      /** Called when the user saves. Required in edit mode. */
      onSave: (value: string) => void;
      /** Optional validation — return an error string, or undefined if valid. */
      validate?: (value: string) => string | undefined;
      /** "edit" (default) for click-to-edit, "create" for always-editable. */
      mode?: "edit" | "create";
      /** Unique field identifier for coordination. */
      fieldId?: string;
      /** Which field is currently being edited (parent coordination). */
      activeFieldId?: string | null; // eslint-disable-line @rushstack/no-new-null
      /** Callback to tell the parent which field is active. */
      onActivate?: (fieldId: string | null) => void; // eslint-disable-line @rushstack/no-new-null
      /** Called on every keystroke in create mode. */
      onChange?: (value: string) => void;
      /** Custom display renderer (e.g., link for repoUrl). */
      renderDisplay?: (value: string) => ReactNode | undefined;
      /** Placeholder text shown when empty. */
      placeholder?: string;
      /** Max character length for the input. */
      maxLength?: number;
      /** Accessible label for the input. */
      ariaLabel?: string;
      /** Base test ID — gets `-input` / `-button` suffixes appended. */
      "data-testid"?: string;
    }

    /** Reusable click-to-edit text input field. */
    export function EditableTextField(props: EditableTextFieldProps): JSX.Element {
      const {
        value,
        onSave,
        validate,
        mode = "edit",
        fieldId = "text",
        activeFieldId,
        onActivate,
        onChange,
        renderDisplay,
        placeholder,
        maxLength,
        ariaLabel,
        "data-testid": testId,
      } = props;

      const inputRef = useRef<HTMLInputElement>(null);

      const field = useEditableField({
        value,
        onSave,
        validate,
        fieldId,
        activeFieldId,
        onActivate,
        enterToSave: true,
        trimOnSave: true,
      });

      // Auto-focus when entering edit mode
      useEffect(() => {
        Iif (field.isEditing) {
          const timer = window.setTimeout(() => {
            inputRef.current?.focus();
          }, 0);
          return () => window.clearTimeout(timer);
        }
      }, [field.isEditing]);

      // Create mode: always show input, no blur-to-save
      if (mode === "create") {
        const handleChange = (e: React.ChangeEvent<HTMLInputElement>): void => {
          onChange?.(e.target.value);
        };

        const validationError = validate?.(value);

        return (
          <div className={styles.editFieldWrapper}>
            <input
              className={`${styles.editInput} ${validationError ? styles.editInputInvalid : ""}`}
              value={value}
              onChange={handleChange}
              maxLength={maxLength}
              placeholder={placeholder}
              aria-label={ariaLabel}
              data-testid={testId ? `${testId}-input` : undefined}
            />
            {validationError && (
              <span className={styles.editError} data-testid="edit-error">
                {validationError}
              </span>
            )}
          </div>
        );
      }

      // Edit mode: toggle between display and input
      Iif (field.isEditing) {
        return (
          <div className={styles.editFieldWrapper}>
            <input
              ref={inputRef}
              className={`${styles.editInput} ${field.error ? styles.editInputInvalid : ""}`}
              value={field.draft}
              onChange={(e) => field.setDraft(e.target.value)}
              onBlur={field.handleBlur}
              onKeyDown={field.handleKeyDown}
              maxLength={maxLength}
              placeholder={placeholder}
              aria-label={ariaLabel}
              data-testid={testId ? `${testId}-input` : undefined}
            />
            {field.isDirty && <span className={styles.unsavedDot} title="Unsaved changes" />}
            {field.error && (
              <span className={styles.editError} data-testid="edit-error">
                {field.error}
              </span>
            )}
            <span className={styles.editHint}>Enter to save &middot; Esc to cancel</span>
          </div>
        );
      }

      // Display mode — uses <span role="button"> to avoid nested interactive elements
      // when renderDisplay returns links or other interactive content
      const displayContent = renderDisplay?.(value);
      return (
        <span
          role="button"
          tabIndex={0}
          className={styles.metaValueClickable}
          onClick={() => field.startEdit()}
          onKeyDown={(e) => {
            Iif (e.key === "Enter" || e.key === " ") {
              e.preventDefault();
              field.startEdit();
            }
          }}
          title="Click to edit"
          aria-label={ariaLabel}
          data-testid={testId ? `${testId}-button` : undefined}
        >
          {displayContent !== undefined ? (
            displayContent
          ) : value ? (
            <span>{value}</span>
          ) : (
            <span className={styles.metaPlaceholder}>{placeholder || "None"}</span>
          )}
          <span className={styles.editButton} aria-hidden="true">
            &#x270F;&#xFE0F;
          </span>
        </span>
      );
    }