import { Editor, type Row } from '@components/common/form/Editor.js'; import { InputField } from '@components/common/form/InputField.js'; import { NumberField } from '@components/common/form/NumberField.js'; import { ReactSelectField } from '@components/common/form/ReactSelectField.js'; import { TextareaField } from '@components/common/form/TextareaField.js'; import { ToggleField } from '@components/common/form/ToggleField.js'; import { LinkPicker } from '@components/common/page-builder/pickers/LinkPicker.js'; import { Button } from '@components/common/ui/Button.js'; import { Input } from '@components/common/ui/Input.js'; import { Textarea } from '@components/common/ui/Textarea.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { ChevronDown, ChevronUp, Plus, Trash2, X } from 'lucide-react'; import React from 'react'; import { Controller, type RegisterOptions, useFieldArray, useFormContext } from 'react-hook-form'; export interface Validation { type: string; values?: Array; min?: number; max?: number; pattern?: string; } export interface FieldDescriptor { key: string; name: string; description?: string; type: string; isList?: boolean; required?: boolean; validations?: Validation[]; subFields?: FieldDescriptor[]; appearance?: Record; } function choicesOf(field: FieldDescriptor): Array | undefined { return (field.validations || []).find((v) => v.type === 'choices')?.values; } const isEmptyInput = (v: unknown): boolean => v === undefined || v === null || v === ''; /** * Map a definition's `required` flag + `validations` to react-hook-form * RegisterOptions so the value inputs enforce the same rules client-side (inline) * that the server enforces via AJV. Mirrors lib/metafield/compileField.ts: * size → min/maxLength, range → min/max, regexp → pattern. * * `size` and `regexp` are expressed as `validate` callbacks that pass on empty * input, because RHF's native `minLength`/`pattern` rules fire on `''` and would * wrongly block an untouched optional field (the server normalizes empties away, * so the client must too). `range`'s native min/max are safe — they ignore empty. * * `required` is mapped only when `includeRequired` is true (top-level fields). * It is skipped for GROUP sub-fields: the server normalizes an all-empty group to * "unset" before checking its sub-fields, so a required sub-field of an untouched * group must not block the save — the client mirrors that by not enforcing it. */ function toValidationRules( field: FieldDescriptor, includeRequired = true ): RegisterOptions | undefined { const rules: RegisterOptions = {}; const validate: Record boolean | string> = {}; if (includeRequired && field.required) { rules.required = _('${field} is required', { field: field.name }); } for (const v of field.validations || []) { if (v.type === 'size') { if (typeof v.min === 'number') { const min = v.min; validate.minLength = (val) => isEmptyInput(val) || String(val).length >= min || _('Must be at least ${n} characters', { n: String(min) }); } if (typeof v.max === 'number') { const max = v.max; validate.maxLength = (val) => isEmptyInput(val) || String(val).length <= max || _('Must be at most ${n} characters', { n: String(max) }); } } else if (v.type === 'range') { if (typeof v.min === 'number') rules.min = { value: v.min, message: _('Must be at least ${n}', { n: String(v.min) }) }; if (typeof v.max === 'number') rules.max = { value: v.max, message: _('Must be at most ${n}', { n: String(v.max) }) }; } else if (v.type === 'regexp' && v.pattern) { let re: RegExp | null = null; try { re = new RegExp(v.pattern); } catch { re = null; // ignore an invalid stored pattern; the server still enforces it } if (re) { const rx = re; validate.pattern = (val) => isEmptyInput(val) || rx.test(String(val)) || _('Invalid format'); } } } if (Object.keys(validate).length) rules.validate = validate; return Object.keys(rules).length ? rules : undefined; } /** URL: uses the LinkPicker (page / category / product / custom URL). */ function UrlInput({ name }: { name: string }) { const { control } = useFormContext(); return ( ( field.onChange(url)} /> )} /> ); } /** A single non-list, non-group input (bound to the form by `name`). */ function ScalarInput({ field, name, isSubField }: { field: FieldDescriptor; name: string; isSubField?: boolean; }) { const validation = toValidationRules(field, !isSubField); // The theme's declared default (appearance.placeholder) renders as the // input hint so the merchant sees in the form exactly what the storefront // shows while the field is unset (theme-metafields design, Decision 5). const placeholder = typeof field.appearance?.placeholder === 'string' ? field.appearance.placeholder : undefined; switch (field.type) { case 'long_text': return ( ); case 'integer': return ( ); case 'number': return ( ); case 'boolean': return ; case 'date': return ; case 'color': return ; case 'url': return ; case 'short_text': default: return ( ); } } /** A single controlled input for one item of a scalar list. */ function ListItemInput({ type, value, onChange, placeholder }: { type: string; value: unknown; onChange: (value: unknown) => void; placeholder?: string; }) { const str = value === undefined || value === null ? '' : String(value); if (type === 'long_text') { return (