@asteby/metacore-runtime-react 49.13.0 → 50.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/dist/dialogs/dynamic-record.d.ts +41 -0
- package/dist/dialogs/dynamic-record.d.ts.map +1 -1
- package/dist/dialogs/dynamic-record.js +134 -11
- package/dist/dialogs/types.d.ts +5 -0
- package/dist/dialogs/types.d.ts.map +1 -1
- package/dist/icon-picker-field.d.ts +8 -1
- package/dist/icon-picker-field.d.ts.map +1 -1
- package/dist/icon-picker-field.js +8 -4
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/upload-field.d.ts +21 -0
- package/dist/upload-field.d.ts.map +1 -1
- package/dist/upload-field.js +51 -3
- package/package.json +3 -3
- package/src/__tests__/dynamic-record-edit-payload-upload.test.tsx +231 -0
- package/src/__tests__/dynamic-record-texts.test.tsx +75 -0
- package/src/__tests__/icon-picker-icon-only.test.tsx +49 -0
- package/src/dialogs/dynamic-record.tsx +224 -35
- package/src/dialogs/types.ts +5 -0
- package/src/icon-picker-field.tsx +27 -4
- package/src/types.ts +6 -0
- package/src/upload-field.tsx +57 -3
|
@@ -55,12 +55,12 @@ import { toastServerError, extractFieldErrors, localizeFieldIssue, localizeField
|
|
|
55
55
|
import { DynamicSelectField, OptionLead, OptionThumb } from '../dynamic-select-field'
|
|
56
56
|
import { RecordPicker } from '../record-picker'
|
|
57
57
|
import { DynamicMultiSelectField } from '../dynamic-multi-select-field'
|
|
58
|
-
import { UploadField } from '../upload-field'
|
|
58
|
+
import { UploadField, validateUploadFile } from '../upload-field'
|
|
59
59
|
import { DynamicRelations } from '../dynamic-relations'
|
|
60
60
|
import { AuditInfo, readAuditMeta } from '../audit-info'
|
|
61
61
|
import type { AuditMeta } from '../types'
|
|
62
62
|
import { useOptionsResolver, type ResolvedOption } from '../use-options-resolver'
|
|
63
|
-
import { getFieldRef, getVisibleWhen, evaluateVisibleWhen, ATTRIBUTE_CLASSES_KEY } from '../dynamic-form-schema'
|
|
63
|
+
import { getFieldRef, getVisibleWhen, evaluateVisibleWhen, getUploadConfig, ATTRIBUTE_CLASSES_KEY } from '../dynamic-form-schema'
|
|
64
64
|
import { useAttributeClasses, type AttributeClass } from '../attribute-classes'
|
|
65
65
|
import type { VisibleWhen } from '../types'
|
|
66
66
|
import { groupFieldsBySection, type FormLayout } from '../form-layout'
|
|
@@ -181,6 +181,65 @@ export interface FieldDef {
|
|
|
181
181
|
* `groupFieldsBySection`.
|
|
182
182
|
*/
|
|
183
183
|
section?: string
|
|
184
|
+
/** `icon` widget: only lucide names (no "Imagen" tab). */
|
|
185
|
+
iconOnly?: boolean
|
|
186
|
+
/** snake_case alias for `iconOnly`. */
|
|
187
|
+
icon_only?: boolean
|
|
188
|
+
/**
|
|
189
|
+
* `file` / `image` upload: maximum size in bytes. A larger file is rejected
|
|
190
|
+
* client-side (error toast + inline message) and never POSTed to `/upload`.
|
|
191
|
+
* Absent → no limit (retrocompat).
|
|
192
|
+
*/
|
|
193
|
+
maxSize?: number
|
|
194
|
+
/**
|
|
195
|
+
* `file` / `image` upload: accept list (`"image/*"`, `"image/png,.webp"`).
|
|
196
|
+
* Forwarded to the file input and also enforced on pick and drag-and-drop.
|
|
197
|
+
* Absent → `image/*` for `image`, anything for `file` (retrocompat).
|
|
198
|
+
*/
|
|
199
|
+
accept?: string
|
|
200
|
+
/**
|
|
201
|
+
* Pre-fills this field from a sibling while the user has not edited it by
|
|
202
|
+
* hand: `{ field: 'name', transform: 'slug' }` keeps `slug` in sync with
|
|
203
|
+
* `name`. Once the user types in this field it is no longer derived.
|
|
204
|
+
*/
|
|
205
|
+
deriveFrom?: { field: string; transform: 'slug' }
|
|
206
|
+
/** snake_case alias for `deriveFrom`. */
|
|
207
|
+
derive_from?: { field: string; transform: 'slug' }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Lowercase, accent-free, hyphenated slug ("Asociaciones Médicas" →
|
|
212
|
+
* "asociaciones-medicas"). Pure — used by `FieldDef.deriveFrom`.
|
|
213
|
+
*/
|
|
214
|
+
export function slugify(input: unknown): string {
|
|
215
|
+
return String(input ?? '')
|
|
216
|
+
.normalize('NFD')
|
|
217
|
+
.replace(/[\u0300-\u036f]/g, '')
|
|
218
|
+
.toLowerCase()
|
|
219
|
+
.replace(/[^a-z0-9]+/g, '-')
|
|
220
|
+
.replace(/^-+|-+$/g, '')
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const getDeriveFrom = (f: FieldDef) => f.deriveFrom ?? f.derive_from
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* `editPayload: 'declared'` — keeps only the keys of the fields declared in the
|
|
227
|
+
* modal metadata. A declared field with no value is sent as '' (so a value can
|
|
228
|
+
* be cleared); one hidden by `visible_when` (already stripped) stays out.
|
|
229
|
+
*/
|
|
230
|
+
export function pickDeclaredFieldValues(
|
|
231
|
+
values: Record<string, any>,
|
|
232
|
+
fields: FieldDef[] | undefined,
|
|
233
|
+
): Record<string, any> {
|
|
234
|
+
const out: Record<string, any> = {}
|
|
235
|
+
for (const f of fields ?? []) {
|
|
236
|
+
if (f.key in values) {
|
|
237
|
+
out[f.key] = values[f.key] ?? ''
|
|
238
|
+
} else if (!getVisibleWhen(f) && !f.hidden && f.type !== 'hidden' && !f.readonly) {
|
|
239
|
+
out[f.key] = ''
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return out
|
|
184
243
|
}
|
|
185
244
|
|
|
186
245
|
// Permissive shape: the wire payload may omit some fields (e.g. `title` is
|
|
@@ -197,7 +256,30 @@ interface ModalMetadata {
|
|
|
197
256
|
titleKey?: string
|
|
198
257
|
createTitle?: string
|
|
199
258
|
editTitle?: string
|
|
259
|
+
/**
|
|
260
|
+
* Optional per-model dialog texts. When present they replace the generic
|
|
261
|
+
* description / submit label of the mode; absent → the built-in texts.
|
|
262
|
+
* snake_case aliases accepted (wire format).
|
|
263
|
+
*/
|
|
264
|
+
createDescription?: string
|
|
265
|
+
editDescription?: string
|
|
266
|
+
viewDescription?: string
|
|
267
|
+
createSubmitLabel?: string
|
|
268
|
+
editSubmitLabel?: string
|
|
269
|
+
create_description?: string
|
|
270
|
+
edit_description?: string
|
|
271
|
+
view_description?: string
|
|
272
|
+
create_submit_label?: string
|
|
273
|
+
edit_submit_label?: string
|
|
200
274
|
fields?: FieldDef[]
|
|
275
|
+
/**
|
|
276
|
+
* Shape of the PUT payload in `mode='edit'`. `'all'` (default) sends every
|
|
277
|
+
* form value as before; `'declared'` sends only the keys of the fields
|
|
278
|
+
* declared in this metadata (a declared empty field goes as ''). Create is
|
|
279
|
+
* unaffected. Tolerates the snake_case alias `edit_payload`.
|
|
280
|
+
*/
|
|
281
|
+
editPayload?: 'all' | 'declared'
|
|
282
|
+
edit_payload?: 'all' | 'declared'
|
|
201
283
|
/**
|
|
202
284
|
* Declarative form layout (kernel PR #230): groups the fields into named
|
|
203
285
|
* sections rendered stacked (`mode:"sections"`) or as a wizard
|
|
@@ -501,6 +583,33 @@ function formatDisplayValue(rawValue: any, field: FieldDef): string {
|
|
|
501
583
|
return String(value)
|
|
502
584
|
}
|
|
503
585
|
|
|
586
|
+
// modeTexts resolves the per-model description / submit label for a mode,
|
|
587
|
+
// falling back to the generic MODE_CONFIG strings when metadata omits them.
|
|
588
|
+
function modeTexts(
|
|
589
|
+
meta: ModalMetadata | null | undefined,
|
|
590
|
+
mode: 'create' | 'edit' | 'view',
|
|
591
|
+
base: { description: string; submitLabel: string },
|
|
592
|
+
): { description: string; submitLabel: string } {
|
|
593
|
+
const pick = (...v: Array<string | undefined>) => v.find((x) => typeof x === 'string' && x.trim() !== '')
|
|
594
|
+
if (!meta) return base
|
|
595
|
+
if (mode === 'create') {
|
|
596
|
+
return {
|
|
597
|
+
description: pick(meta.createDescription, meta.create_description) ?? base.description,
|
|
598
|
+
submitLabel: pick(meta.createSubmitLabel, meta.create_submit_label) ?? base.submitLabel,
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
if (mode === 'edit') {
|
|
602
|
+
return {
|
|
603
|
+
description: pick(meta.editDescription, meta.edit_description) ?? base.description,
|
|
604
|
+
submitLabel: pick(meta.editSubmitLabel, meta.edit_submit_label) ?? base.submitLabel,
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
return {
|
|
608
|
+
description: pick(meta.viewDescription, meta.view_description) ?? base.description,
|
|
609
|
+
submitLabel: base.submitLabel,
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
504
613
|
const MODE_CONFIG = {
|
|
505
614
|
create: {
|
|
506
615
|
getTitle: (meta: ModalMetadata, t: TFn) => {
|
|
@@ -690,6 +799,8 @@ export function DynamicRecordDialog({
|
|
|
690
799
|
const [audit, setAudit] = useState<AuditMeta | undefined>(undefined)
|
|
691
800
|
const [record, setRecord] = useState<any | null>(null)
|
|
692
801
|
const [formValues, setFormValues] = useState<Record<string, any>>({})
|
|
802
|
+
// Derived fields (`deriveFrom`) the user has typed in by hand: no longer derived.
|
|
803
|
+
const manualDerived = useRef<Set<string>>(new Set())
|
|
693
804
|
// Classes of the record's category (plus those its saved data carries), for
|
|
694
805
|
// `visible_when.class` fields.
|
|
695
806
|
const attributeClasses = useAttributeClasses(api, modalMeta, formValues, record)
|
|
@@ -731,6 +842,7 @@ export function DynamicRecordDialog({
|
|
|
731
842
|
// the wizard to its first step.
|
|
732
843
|
setFieldErrors({})
|
|
733
844
|
setStepIndex(0)
|
|
845
|
+
manualDerived.current.clear()
|
|
734
846
|
|
|
735
847
|
let cancelled = false
|
|
736
848
|
|
|
@@ -747,6 +859,14 @@ export function DynamicRecordDialog({
|
|
|
747
859
|
? ''
|
|
748
860
|
: resolvePath(rec, field.key) ?? field.defaultValue ?? ''
|
|
749
861
|
}
|
|
862
|
+
// En edición, un destino derivado ya guardado no se pisa: cuenta como manual.
|
|
863
|
+
if (!isCreate) {
|
|
864
|
+
for (const field of meta.fields ?? []) {
|
|
865
|
+
if (getDeriveFrom(field) && initial[field.key] !== '' && initial[field.key] != null) {
|
|
866
|
+
manualDerived.current.add(field.key)
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}
|
|
750
870
|
setFormValues(initial)
|
|
751
871
|
}
|
|
752
872
|
|
|
@@ -986,7 +1106,10 @@ export function DynamicRecordDialog({
|
|
|
986
1106
|
// filter, so a DiscountRule with scope=category never submits the
|
|
987
1107
|
// product_id / customer_id it isn't showing. Mirrors dynamic-form.tsx,
|
|
988
1108
|
// which builds its Zod only over visibleFields.
|
|
989
|
-
|
|
1109
|
+
let submittedValues = stripHiddenFieldValues(formValues, modalMeta.fields, mode, attributeClasses)
|
|
1110
|
+
if (mode === 'edit' && (modalMeta.editPayload ?? modalMeta.edit_payload) === 'declared') {
|
|
1111
|
+
submittedValues = pickDeclaredFieldValues(submittedValues, modalMeta.fields)
|
|
1112
|
+
}
|
|
990
1113
|
|
|
991
1114
|
// Empty reference pickers → null (not "" / nil-UUID) so nullable FK
|
|
992
1115
|
// columns accept them instead of raising a 23503 FK violation.
|
|
@@ -1086,6 +1209,8 @@ export function DynamicRecordDialog({
|
|
|
1086
1209
|
? 'Editar registro'
|
|
1087
1210
|
: 'Ver registro'
|
|
1088
1211
|
|
|
1212
|
+
const texts = modeTexts(modalMeta, mode, config)
|
|
1213
|
+
|
|
1089
1214
|
const visibleFields = filterVisibleFields(modalMeta?.fields, mode, formValues, attributeClasses)
|
|
1090
1215
|
|
|
1091
1216
|
// Declarative form layout: group the (already visibility-filtered) fields by
|
|
@@ -1115,7 +1240,23 @@ export function DynamicRecordDialog({
|
|
|
1115
1240
|
locked={isCreate && !!lockedFields?.includes(field.key)}
|
|
1116
1241
|
error={fieldErrors[field.key]}
|
|
1117
1242
|
onChange={val => {
|
|
1118
|
-
|
|
1243
|
+
if (getDeriveFrom(field)) {
|
|
1244
|
+
if (val === '' || val == null) manualDerived.current.delete(field.key)
|
|
1245
|
+
else manualDerived.current.add(field.key)
|
|
1246
|
+
}
|
|
1247
|
+
setFormValues((prev: Record<string, any>) => {
|
|
1248
|
+
const next = { ...prev, [field.key]: val }
|
|
1249
|
+
for (const target of modalMeta?.fields ?? []) {
|
|
1250
|
+
const d = getDeriveFrom(target)
|
|
1251
|
+
if (!d || d.field !== field.key || d.transform !== 'slug') continue
|
|
1252
|
+
if (manualDerived.current.has(target.key)) continue
|
|
1253
|
+
const current = prev[target.key] ?? ''
|
|
1254
|
+
if (current === '' || current === slugify(prev[field.key])) {
|
|
1255
|
+
next[target.key] = slugify(val)
|
|
1256
|
+
}
|
|
1257
|
+
}
|
|
1258
|
+
return next
|
|
1259
|
+
})
|
|
1119
1260
|
setFieldErrors(prev => {
|
|
1120
1261
|
if (!prev[field.key]) return prev
|
|
1121
1262
|
const next = { ...prev }
|
|
@@ -1152,7 +1293,7 @@ export function DynamicRecordDialog({
|
|
|
1152
1293
|
<DialogContent className="sm:max-w-2xl max-h-[90dvh] flex flex-col p-0 gap-0 overflow-hidden" style={{ maxHeight: '90dvh' }}>
|
|
1153
1294
|
<DialogHeader className="p-6 pb-4 border-b shrink-0">
|
|
1154
1295
|
<DialogTitle>{title}</DialogTitle>
|
|
1155
|
-
<DialogDescription>{
|
|
1296
|
+
<DialogDescription>{texts.description}</DialogDescription>
|
|
1156
1297
|
</DialogHeader>
|
|
1157
1298
|
|
|
1158
1299
|
<div className="flex-1 overflow-y-auto p-6">
|
|
@@ -1332,7 +1473,7 @@ export function DynamicRecordDialog({
|
|
|
1332
1473
|
disabled={saving || loading}
|
|
1333
1474
|
>
|
|
1334
1475
|
{saving && <Loader2 className="mr-2 h-4 w-4 animate-spin" />}
|
|
1335
|
-
{saving ? config.submittingLabel :
|
|
1476
|
+
{saving ? config.submittingLabel : texts.submitLabel}
|
|
1336
1477
|
</Button>
|
|
1337
1478
|
)}
|
|
1338
1479
|
</div>
|
|
@@ -2435,18 +2576,34 @@ function ScannableRecordInput({
|
|
|
2435
2576
|
)
|
|
2436
2577
|
}
|
|
2437
2578
|
|
|
2438
|
-
function ImageUploadField({ field
|
|
2579
|
+
function ImageUploadField({ field, value, onChange }: { field: FieldDef; value: any; onChange: (val: any) => void }) {
|
|
2439
2580
|
const { t } = useTranslation()
|
|
2440
2581
|
const api = useApi()
|
|
2441
2582
|
const model = useContext(ModelContext)
|
|
2442
2583
|
const getImageUrl = useContext(ImageUrlContext)
|
|
2443
2584
|
const [uploading, setUploading] = useState(false)
|
|
2585
|
+
const [error, setError] = useState<string | null>(null)
|
|
2586
|
+
const [dragging, setDragging] = useState(false)
|
|
2444
2587
|
const inputRef = useRef<HTMLInputElement>(null)
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2588
|
+
const { accept: declaredAccept, maxSize } = getUploadConfig(field as ActionFieldDef)
|
|
2589
|
+
const accept = declaredAccept || 'image/*'
|
|
2590
|
+
|
|
2591
|
+
async function upload(file: File) {
|
|
2592
|
+
const rejected = validateUploadFile(file, { accept: declaredAccept, maxSize })
|
|
2593
|
+
if (rejected) {
|
|
2594
|
+
const msg =
|
|
2595
|
+
rejected === 'type'
|
|
2596
|
+
? t('common.upload.invalid_type', { defaultValue: 'File type not allowed.' })
|
|
2597
|
+
: t('common.upload.too_large', {
|
|
2598
|
+
mb: ((maxSize ?? 0) / (1024 * 1024)).toFixed(1),
|
|
2599
|
+
defaultValue: 'File too large (max {{mb}} MB).',
|
|
2600
|
+
})
|
|
2601
|
+
setError(msg)
|
|
2602
|
+
toast.error(msg)
|
|
2603
|
+
if (inputRef.current) inputRef.current.value = ''
|
|
2604
|
+
return
|
|
2605
|
+
}
|
|
2606
|
+
setError(null)
|
|
2450
2607
|
setUploading(true)
|
|
2451
2608
|
try {
|
|
2452
2609
|
const formData = new FormData()
|
|
@@ -2463,35 +2620,67 @@ function ImageUploadField({ field: _field, value, onChange }: { field: FieldDef;
|
|
|
2463
2620
|
}
|
|
2464
2621
|
}
|
|
2465
2622
|
|
|
2623
|
+
function handleFile(e: React.ChangeEvent<HTMLInputElement>) {
|
|
2624
|
+
const file = e.target.files?.[0]
|
|
2625
|
+
if (!file) return
|
|
2626
|
+
void upload(file)
|
|
2627
|
+
}
|
|
2628
|
+
|
|
2466
2629
|
return (
|
|
2467
|
-
<div className="
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2630
|
+
<div className="grid gap-1.5">
|
|
2631
|
+
<div
|
|
2632
|
+
className="flex items-center gap-3"
|
|
2633
|
+
onDragOver={(e) => {
|
|
2634
|
+
// Siempre preventDefault: si no, el navegador abre el archivo soltado.
|
|
2635
|
+
e.preventDefault()
|
|
2636
|
+
if (value || uploading) return
|
|
2637
|
+
setDragging(true)
|
|
2638
|
+
}}
|
|
2639
|
+
onDragLeave={() => setDragging(false)}
|
|
2640
|
+
onDrop={(e) => {
|
|
2641
|
+
e.preventDefault()
|
|
2642
|
+
setDragging(false)
|
|
2643
|
+
const dropped = e.dataTransfer?.files?.[0]
|
|
2644
|
+
if (!dropped || value || uploading) return
|
|
2645
|
+
void upload(dropped)
|
|
2646
|
+
}}
|
|
2647
|
+
>
|
|
2648
|
+
{value ? (
|
|
2649
|
+
<div className="relative">
|
|
2650
|
+
<img src={getImageUrl(String(value))} alt="" className="h-16 w-16 rounded-lg object-cover border" />
|
|
2651
|
+
<button
|
|
2652
|
+
type="button"
|
|
2653
|
+
onClick={() => onChange('')}
|
|
2654
|
+
className="absolute -top-1.5 -right-1.5 size-5 bg-destructive text-white rounded-full flex items-center justify-center hover:bg-destructive/90"
|
|
2655
|
+
>
|
|
2656
|
+
<XIcon className="size-3" />
|
|
2657
|
+
</button>
|
|
2658
|
+
</div>
|
|
2659
|
+
) : (
|
|
2471
2660
|
<button
|
|
2472
2661
|
type="button"
|
|
2473
|
-
onClick={() =>
|
|
2474
|
-
|
|
2662
|
+
onClick={() => inputRef.current?.click()}
|
|
2663
|
+
disabled={uploading}
|
|
2664
|
+
className={cn(
|
|
2665
|
+
'h-16 w-16 rounded-lg border-2 border-dashed border-muted-foreground/30 flex flex-col items-center justify-center gap-1 hover:border-primary/50 hover:bg-muted/50 transition-colors disabled:opacity-50',
|
|
2666
|
+
dragging && 'border-primary/50 bg-muted/50',
|
|
2667
|
+
)}
|
|
2475
2668
|
>
|
|
2476
|
-
|
|
2669
|
+
{uploading ? (
|
|
2670
|
+
<Loader2 className="size-4 animate-spin text-muted-foreground" />
|
|
2671
|
+
) : (
|
|
2672
|
+
<Upload className="size-4 text-muted-foreground" />
|
|
2673
|
+
)}
|
|
2477
2674
|
</button>
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
<
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
>
|
|
2486
|
-
{uploading ? (
|
|
2487
|
-
<Loader2 className="size-4 animate-spin text-muted-foreground" />
|
|
2488
|
-
) : (
|
|
2489
|
-
<Upload className="size-4 text-muted-foreground" />
|
|
2490
|
-
)}
|
|
2491
|
-
</button>
|
|
2675
|
+
)}
|
|
2676
|
+
<input ref={inputRef} type="file" accept={accept} onChange={handleFile} className="hidden" />
|
|
2677
|
+
{!value && <span className="text-xs text-muted-foreground">PNG, JPG, WebP</span>}
|
|
2678
|
+
</div>
|
|
2679
|
+
{error && (
|
|
2680
|
+
<span className="text-sm text-destructive" role="alert">
|
|
2681
|
+
{error}
|
|
2682
|
+
</span>
|
|
2492
2683
|
)}
|
|
2493
|
-
<input ref={inputRef} type="file" accept="image/*" onChange={handleFile} className="hidden" />
|
|
2494
|
-
{!value && <span className="text-xs text-muted-foreground">PNG, JPG, WebP</span>}
|
|
2495
2684
|
</div>
|
|
2496
2685
|
)
|
|
2497
2686
|
}
|
package/src/dialogs/types.ts
CHANGED
|
@@ -30,6 +30,11 @@ export interface ModelSchema {
|
|
|
30
30
|
title?: string
|
|
31
31
|
createTitle?: string
|
|
32
32
|
editTitle?: string
|
|
33
|
+
createDescription?: string
|
|
34
|
+
editDescription?: string
|
|
35
|
+
viewDescription?: string
|
|
36
|
+
createSubmitLabel?: string
|
|
37
|
+
editSubmitLabel?: string
|
|
33
38
|
fields?: Array<{
|
|
34
39
|
key: string
|
|
35
40
|
label: string
|
|
@@ -29,6 +29,13 @@ export interface IconPickerFieldProps {
|
|
|
29
29
|
field: ActionFieldDef
|
|
30
30
|
value: any
|
|
31
31
|
onChange: (v: any) => void
|
|
32
|
+
/**
|
|
33
|
+
* Icon-only mode: hide the "Imagen" tab so the field can only store a
|
|
34
|
+
* lucide icon name. Also read from `field.iconOnly` / `field.icon_only`
|
|
35
|
+
* (metadata). An existing url/path value is preserved untouched until the
|
|
36
|
+
* user picks an icon or clears it (shown as a non-editable current value).
|
|
37
|
+
*/
|
|
38
|
+
iconOnly?: boolean
|
|
32
39
|
}
|
|
33
40
|
|
|
34
41
|
/** Icons rendered per "page" — grows as the user scrolls to the bottom. Keeps
|
|
@@ -42,15 +49,19 @@ export function looksLikeImageValue(value: unknown): boolean {
|
|
|
42
49
|
return typeof value === 'string' && value !== '' && /[/.]/.test(value)
|
|
43
50
|
}
|
|
44
51
|
|
|
45
|
-
export function IconPickerField({ field, value, onChange }: IconPickerFieldProps) {
|
|
52
|
+
export function IconPickerField({ field, value, onChange, iconOnly }: IconPickerFieldProps) {
|
|
53
|
+
const onlyIcon = iconOnly ?? field.iconOnly ?? field.icon_only ?? false
|
|
46
54
|
const [mode, setMode] = useState<'icon' | 'image'>(() =>
|
|
47
|
-
looksLikeImageValue(value) ? 'image' : 'icon',
|
|
55
|
+
!onlyIcon && looksLikeImageValue(value) ? 'image' : 'icon',
|
|
48
56
|
)
|
|
57
|
+
// iconOnly + a stored url/path (written before the restriction): keep it,
|
|
58
|
+
// render it as a read-only current value the user can clear or replace.
|
|
59
|
+
const legacyImageValue = onlyIcon && looksLikeImageValue(value)
|
|
49
60
|
const [open, setOpen] = useState(false)
|
|
50
61
|
const [query, setQuery] = useState('')
|
|
51
62
|
const [limit, setLimit] = useState(PAGE)
|
|
52
63
|
|
|
53
|
-
const selected = mode === 'icon' ? resolveLucideIconName(value) : null
|
|
64
|
+
const selected = mode === 'icon' && !legacyImageValue ? resolveLucideIconName(value) : null
|
|
54
65
|
|
|
55
66
|
// Full match list (names only) — filtered by query, not yet capped.
|
|
56
67
|
const matches = useMemo(() => {
|
|
@@ -70,6 +81,7 @@ export function IconPickerField({ field, value, onChange }: IconPickerFieldProps
|
|
|
70
81
|
|
|
71
82
|
return (
|
|
72
83
|
<div className="flex flex-col gap-2">
|
|
84
|
+
{!onlyIcon && (
|
|
73
85
|
<div className="flex gap-1" role="tablist" aria-label="Tipo de ícono">
|
|
74
86
|
<Button
|
|
75
87
|
type="button"
|
|
@@ -92,7 +104,18 @@ export function IconPickerField({ field, value, onChange }: IconPickerFieldProps
|
|
|
92
104
|
Imagen
|
|
93
105
|
</Button>
|
|
94
106
|
</div>
|
|
95
|
-
|
|
107
|
+
)}
|
|
108
|
+
{legacyImageValue && (
|
|
109
|
+
<div className="flex items-center justify-between gap-2 rounded-md border px-3 py-2 text-sm" data-testid="icon-legacy-value">
|
|
110
|
+
<span className="min-w-0 truncate text-muted-foreground" title={String(value)}>
|
|
111
|
+
Valor actual: {String(value)}
|
|
112
|
+
</span>
|
|
113
|
+
<Button type="button" variant="ghost" size="sm" onClick={() => onChange('')}>
|
|
114
|
+
Quitar
|
|
115
|
+
</Button>
|
|
116
|
+
</div>
|
|
117
|
+
)}
|
|
118
|
+
{mode === 'image' && !onlyIcon ? (
|
|
96
119
|
<UploadField field={field} value={value} onChange={onChange} />
|
|
97
120
|
) : (
|
|
98
121
|
<Popover
|
package/src/types.ts
CHANGED
|
@@ -675,6 +675,12 @@ export interface ActionFieldDef {
|
|
|
675
675
|
key: string
|
|
676
676
|
label: string
|
|
677
677
|
type: string
|
|
678
|
+
/**
|
|
679
|
+
* `icon` widget only: restrict to lucide icon names (hides the "Imagen"
|
|
680
|
+
* tab). snake_case alias `icon_only` accepted from metadata JSON.
|
|
681
|
+
*/
|
|
682
|
+
iconOnly?: boolean
|
|
683
|
+
icon_only?: boolean
|
|
678
684
|
required?: boolean
|
|
679
685
|
/**
|
|
680
686
|
* Campo de solo lectura (escrito por el servidor/sistema): `DynamicForm` lo
|
package/src/upload-field.tsx
CHANGED
|
@@ -51,6 +51,39 @@ export function uploadedDisplayName(value: unknown): string {
|
|
|
51
51
|
return parts[parts.length - 1] || cleaned
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
+
/**
|
|
55
|
+
* True when `file` satisfies an HTML-style accept list (`"image/*,.pdf,image/png"`).
|
|
56
|
+
* Tokens: `type/*` (prefix), `type/sub` (exact MIME) and `.ext` (filename suffix),
|
|
57
|
+
* case-insensitive. An empty/absent list accepts everything. Pure — exported for tests.
|
|
58
|
+
*/
|
|
59
|
+
export function fileMatchesAccept(file: { name: string; type: string }, accept?: string): boolean {
|
|
60
|
+
const tokens = (accept ?? '')
|
|
61
|
+
.split(',')
|
|
62
|
+
.map((x) => x.trim().toLowerCase())
|
|
63
|
+
.filter(Boolean)
|
|
64
|
+
if (tokens.length === 0) return true
|
|
65
|
+
const name = (file.name || '').toLowerCase()
|
|
66
|
+
const type = (file.type || '').toLowerCase()
|
|
67
|
+
return tokens.some((tok) => {
|
|
68
|
+
if (tok.startsWith('.')) return name.endsWith(tok)
|
|
69
|
+
if (tok.endsWith('/*')) return type.startsWith(tok.slice(0, -1))
|
|
70
|
+
return type === tok
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Client-side gate shared by the upload widgets: `'type'` when the file does not
|
|
76
|
+
* match `accept`, `'size'` when it exceeds `maxSize` (bytes), else null. Pure.
|
|
77
|
+
*/
|
|
78
|
+
export function validateUploadFile(
|
|
79
|
+
file: { name: string; type: string; size: number },
|
|
80
|
+
opts: { accept?: string; maxSize?: number },
|
|
81
|
+
): 'type' | 'size' | null {
|
|
82
|
+
if (!fileMatchesAccept(file, opts.accept)) return 'type'
|
|
83
|
+
if (opts.maxSize && file.size > opts.maxSize) return 'size'
|
|
84
|
+
return null
|
|
85
|
+
}
|
|
86
|
+
|
|
54
87
|
export function UploadField({ field, value, onChange }: UploadFieldProps) {
|
|
55
88
|
const { t } = useTranslation()
|
|
56
89
|
const api = useApi()
|
|
@@ -69,7 +102,16 @@ export function UploadField({ field, value, onChange }: UploadFieldProps) {
|
|
|
69
102
|
return
|
|
70
103
|
}
|
|
71
104
|
setError(null)
|
|
72
|
-
|
|
105
|
+
const rejected = validateUploadFile(file, { accept, maxSize })
|
|
106
|
+
if (rejected === 'type') {
|
|
107
|
+
setError(
|
|
108
|
+
t('common.upload.invalid_type', {
|
|
109
|
+
defaultValue: 'File type not allowed.',
|
|
110
|
+
}),
|
|
111
|
+
)
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
if (rejected === 'size' && maxSize) {
|
|
73
115
|
const mb = (maxSize / (1024 * 1024)).toFixed(1)
|
|
74
116
|
setError(
|
|
75
117
|
t('common.upload.too_large', {
|
|
@@ -121,11 +163,23 @@ export function UploadField({ field, value, onChange }: UploadFieldProps) {
|
|
|
121
163
|
setUploading(false)
|
|
122
164
|
}
|
|
123
165
|
},
|
|
124
|
-
[api, endpoint, maxSize, storagePath, onChange, t],
|
|
166
|
+
[api, endpoint, accept, maxSize, storagePath, onChange, t],
|
|
125
167
|
)
|
|
126
168
|
|
|
127
169
|
return (
|
|
128
|
-
<div
|
|
170
|
+
<div
|
|
171
|
+
className="grid gap-1.5"
|
|
172
|
+
data-widget="upload"
|
|
173
|
+
onDragOver={(e) => {
|
|
174
|
+
e.preventDefault()
|
|
175
|
+
}}
|
|
176
|
+
onDrop={(e) => {
|
|
177
|
+
e.preventDefault()
|
|
178
|
+
const dropped = e.dataTransfer?.files?.[0]
|
|
179
|
+
if (!dropped || uploading) return
|
|
180
|
+
void handleFile(dropped)
|
|
181
|
+
}}
|
|
182
|
+
>
|
|
129
183
|
<FilePickButton
|
|
130
184
|
id={field.key}
|
|
131
185
|
accept={accept}
|