Skip to documentation

Design system / Sources & standards

Current repository source

inline-editable-field.tsx

src/components/ui/inline-editable-field.tsx

This view contains the escaped source read during the deployment build. Code and policy text are unchanged. Markdown is shown as source, not rendered as a document. A new deployment refreshes the content.

"use client";

import { Pencil } from "lucide-react";
import { cloneElement, isValidElement, useId, type KeyboardEvent, type ReactNode } from "react";

type InlineEditableFieldProps = {
  label: string;
  displayValue: ReactNode;
  isEditing: boolean;
  saving: boolean;
  error?: string | null;
  children?: ReactNode;
  onStartEdit: () => void;
  onSave: () => void;
  onCancel: () => void;
  onKeyDown: (event: KeyboardEvent) => void;
};

export function InlineEditableField({
  label,
  displayValue,
  isEditing,
  saving,
  error,
  children,
  onStartEdit,
  onSave,
  onCancel,
  onKeyDown,
}: InlineEditableFieldProps) {
  const labelId = `${useId()}-label`;

  function handleActivationKeyDown(event: KeyboardEvent<HTMLDivElement>) {
    if (event.key === "Enter" || event.key === " ") {
      event.preventDefault();
      onStartEdit();
    }
  }

  if (!isEditing) {
    return (
      <div
        role="button"
        tabIndex={0}
        onClick={onStartEdit}
        onKeyDown={handleActivationKeyDown}
        aria-label={`Edit ${label}`}
        className="group cursor-pointer rounded-md border p-4 transition-colors hover:border-muted-foreground/50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary"
      >
        <div className="mb-1 flex items-center gap-2">
          <span id={labelId} className="text-xs font-medium text-muted-foreground uppercase tracking-wider">
            {label}
          </span>
          <Pencil aria-hidden="true" className="ml-auto size-4 opacity-0 transition-opacity group-hover:opacity-100 group-focus-visible:opacity-100" />
        </div>
        <div>{displayValue}</div>
      </div>
    );
  }

  return (
    <div className="rounded-md border p-4">
      <div className="mb-1 flex items-center gap-2">
        <span id={labelId} className="text-xs font-medium text-muted-foreground uppercase tracking-wider">
          {label}
        </span>
      </div>
      <div className="space-y-2" onKeyDown={onKeyDown}>
        {isValidElement<{ "aria-labelledby"?: string }>(children)
          ? cloneElement(children, { "aria-labelledby": labelId })
          : children}
        {error && (
          <p role="alert" className="text-supporting text-destructive">
            {error}
          </p>
        )}
        <div className="flex gap-2">
          <button
            type="button"
            onClick={onSave}
            disabled={saving}
            className="rounded-md bg-primary px-3 py-1 text-xs font-medium text-primary-foreground hover:bg-primary/90 disabled:opacity-50"
          >
            {saving ? "Saving..." : "Save"}
          </button>
          <button
            type="button"
            onClick={onCancel}
            disabled={saving}
            className="rounded-md border px-3 py-1 text-xs font-medium hover:bg-muted disabled:opacity-50"
          >
            Cancel
          </button>
        </div>
      </div>
    </div>
  );
}
All sources and standards
Current components and theme tokens. Fictional example data. Source views refresh on deployment; authors maintain contract notes.