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

38.23% Statements 13/34
13.63% Branches 3/22
27.27% Functions 3/11
37.5% Lines 12/32

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
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175

    3x

    3x

    3x

    3x
    3x

    3x

    3x

    3x

    3x

    3x
    4x
    3x

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

    /** A single option in the select dropdown. */
    export interface SelectOption {
      value: string;
      label: string;
    }

    /** Props for EditableSelect. */
    export interface EditableSelectProps {
      /** Current persisted value. */
      value: string;
      /** Called when the user selects a new value. Required in edit mode. */
      onSave: (value: string) => void;
      /** "edit" (default) for click-to-edit, "create" for always-visible. */
      mode?: "edit" | "create";
      /** Available options for the dropdown. */
      options: SelectOption[];
      /** 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 change in create mode. */
      onChange?: (value: string) => void;
      /** Custom display renderer for the selected value. */
      renderDisplay?: (value: string) => ReactNode | undefined;
      /** Placeholder text when no value is selected. */
      placeholder?: string;
      /** Accessible label for the select. */
      ariaLabel?: string;
      /** Base test ID — gets `-select` / `-button` suffixes appended. */
      "data-testid"?: string;
    }

    /** Reusable click-to-edit select dropdown. */
    export function EditableSelect(props: EditableSelectProps): JSX.Element {
      const {
        value,
        onSave,
        mode = "edit",
        options,
        fieldId = "select",
        activeFieldId,
        onActivate,
        onChange,
        renderDisplay,
        placeholder,
        ariaLabel,
        "data-testid": testId,
      } = props;

      const selectRef = useRef<HTMLSelectElement>(null);

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

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

      /** Select saves immediately on change and exits edit mode. */
      const handleSelectChange = useCallback(
        (e: React.ChangeEvent<HTMLSelectElement>) => {
          const newValue = e.target.value;
          field.ignoreInitialBlurRef.current = false;
          Iif (newValue !== value) {
            onSave(newValue);
          }
          field.cancelEdit();
        },
        [value, onSave, field],
      );

      /** Blur just cancels (no auto-save for selects). */
      const handleSelectBlur = useCallback(
        (event: React.FocusEvent) => {
          Iif (field.ignoreInitialBlurRef.current) {
            field.ignoreInitialBlurRef.current = false;
            return;
          }
          Iif (
            event.relatedTarget instanceof HTMLElement &&
            event.relatedTarget.dataset.editAction === fieldId
          ) {
            return;
          }
          field.cancelEdit();
        },
        [fieldId, field],
      );

      // Create mode: always show select
      Iif (mode === "create") {
        return (
          <select
            className={styles.editSelect}
            value={value}
            onChange={(e) => onChange?.(e.target.value)}
            aria-label={ariaLabel}
            data-testid={testId ? `${testId}-select` : undefined}
          >
            {options.map((opt) => (
              <option key={opt.value} value={opt.value}>
                {opt.label}
              </option>
            ))}
          </select>
        );
      }

      // Edit mode: show select dropdown
      Iif (field.isEditing) {
        return (
          <select
            ref={selectRef}
            className={styles.editSelect}
            value={field.draft}
            onChange={handleSelectChange}
            onBlur={handleSelectBlur}
            title={ariaLabel}
            aria-label={ariaLabel}
            data-testid={testId ? `${testId}-select` : undefined}
          >
            {options.map((opt) => (
              <option key={opt.value} value={opt.value}>
                {opt.label}
              </option>
            ))}
          </select>
        );
      }

      // Display mode
      const displayContent = renderDisplay?.(value);
      const selectedLabel = options.find((o) => o.value === value)?.label;
      return (
        <button
          type="button"
          className={styles.metaValueClickable}
          onClick={() => field.startEdit()}
          title="Click to change"
          aria-label={ariaLabel}
          data-testid={testId ? `${testId}-button` : undefined}
        >
          {displayContent !== undefined ? (
            displayContent
          ) : selectedLabel ? (
            <span>{selectedLabel}</span>
          ) : (
            <span className={styles.metaPlaceholder}>{placeholder || "None"}</span>
          )}
          <span className={styles.editButton} aria-hidden="true">
            &#x270F;&#xFE0F;
          </span>
        </button>
      );
    }