/** * Color picker admin component. * * Exports a `fields` map with a "picker" widget that renders a color * input with hex value display and preview swatch. */ import * as React from "react"; interface FieldWidgetProps { value: unknown; onChange: (value: unknown) => void; label: string; id: string; required?: boolean; options?: Record; minimal?: boolean; } /** Named CSS colors for the preset palette */ const PRESETS = [ "#ef4444", "#f97316", "#eab308", "#22c55e", "#06b6d4", "#3b82f6", "#8b5cf6", "#ec4899", "#000000", "#ffffff", ]; const VALID_HEX_PATTERN = /^#[\da-f]{6}$/i; function ColorPicker({ value, onChange, label, id, required, minimal }: FieldWidgetProps) { const rawColor = typeof value === "string" && value ? value : "#000000"; // Only pass valid 6-digit hex to the native color input and preview; // partial input while typing would produce invalid color values. const color = VALID_HEX_PATTERN.test(rawColor) ? rawColor : "#000000"; const handleHexChange = (e: React.ChangeEvent) => { const v = e.target.value; // Allow partial input while typing onChange(v); }; return (
{!minimal && ( )}
onChange(e.target.value)} className="h-10 w-10 cursor-pointer rounded border border-input p-0.5" data-testid="color-input" />
{PRESETS.map((preset) => (
); } export const fields = { picker: ColorPicker, };