DynamicExtFields.tsx
4.71 KB
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
// Dynamic form fields for Tier 1 custom fields (the JSONB `ext` column).
//
// Fetches the custom field declarations for an entity from
// /api/v1/_meta/metadata/custom-fields/{entityName} and renders
// one input per declared field. The SPA doesn't need to know the
// entity's custom fields at compile time — a business analyst
// declares a field in YAML metadata, the framework validates it
// on every save, and this component renders it automatically.
//
// This is P3.1 — the runtime form renderer for Tier 1 customization.
import { useEffect, useState } from 'react'
import { useLocale } from '@/i18n/LocaleContext'
interface CustomFieldDecl {
key: string
targetEntity: string
type: { kind: string; maxLength?: number; precision?: number; scale?: number; allowedValues?: string[] }
required: boolean
labelTranslations: Record<string, string>
}
interface Props {
entityName: string
values: Record<string, unknown>
onChange: (key: string, value: unknown) => void
}
export function DynamicExtFields({ entityName, values, onChange }: Props) {
const [fields, setFields] = useState<CustomFieldDecl[]>([])
const { locale } = useLocale()
useEffect(() => {
fetch(`/api/v1/_meta/metadata/custom-fields/${entityName}`)
.then((r) => r.json())
.then((data: CustomFieldDecl[]) => setFields(data))
.catch(() => setFields([]))
}, [entityName])
if (fields.length === 0) return null
const label = (f: CustomFieldDecl): string => {
const langKey = locale.replace('-', '_').toLowerCase()
return (
f.labelTranslations[locale] ??
f.labelTranslations[langKey] ??
f.labelTranslations['en'] ??
f.labelTranslations['en-US'] ??
f.key
)
}
return (
<div className="border-t border-slate-200 pt-4 mt-4">
<div className="text-xs font-semibold uppercase tracking-wide text-slate-400 mb-3">
Custom fields
</div>
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2">
{fields.map((f) => {
const val = values[f.key]
return (
<div key={f.key}>
<label className="block text-sm font-medium text-slate-700">
{label(f)}
{f.required && <span className="text-rose-500 ml-0.5">*</span>}
</label>
{renderInput(f, val, (v) => onChange(f.key, v))}
</div>
)
})}
</div>
</div>
)
}
function renderInput(
f: CustomFieldDecl,
value: unknown,
onChange: (v: unknown) => void,
) {
const cls = 'mt-1 w-full rounded-md border border-slate-300 px-3 py-2 text-sm'
switch (f.type.kind) {
case 'boolean':
return (
<input
type="checkbox"
checked={Boolean(value)}
onChange={(e) => onChange(e.target.checked)}
className="mt-2 rounded border-slate-300"
/>
)
case 'integer':
return (
<input
type="number"
step="1"
value={value != null ? String(value) : ''}
onChange={(e) => onChange(e.target.value ? Number(e.target.value) : null)}
required={f.required}
className={cls}
/>
)
case 'decimal':
case 'money':
case 'quantity':
return (
<input
type="number"
step="0.01"
value={value != null ? String(value) : ''}
onChange={(e) => onChange(e.target.value ? Number(e.target.value) : null)}
required={f.required}
className={cls}
/>
)
case 'date':
return (
<input
type="date"
value={value != null ? String(value) : ''}
onChange={(e) => onChange(e.target.value || null)}
required={f.required}
className={cls}
/>
)
case 'dateTime':
return (
<input
type="datetime-local"
value={value != null ? String(value) : ''}
onChange={(e) => onChange(e.target.value || null)}
required={f.required}
className={cls}
/>
)
case 'enum':
return (
<select
value={value != null ? String(value) : ''}
onChange={(e) => onChange(e.target.value || null)}
required={f.required}
className={cls}
>
<option value="">—</option>
{f.type.allowedValues?.map((v) => (
<option key={v} value={v}>{v}</option>
))}
</select>
)
case 'string':
case 'uuid':
default:
return (
<input
type="text"
maxLength={f.type.maxLength}
value={value != null ? String(value) : ''}
onChange={(e) => onChange(e.target.value || null)}
required={f.required}
className={cls}
/>
)
}
}