@shikiform/react 0.2.0 → 0.3.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/dist/{chunk-DQHRLKQS.js → chunk-A54EUYGM.js} +303 -137
- package/dist/chunk-A54EUYGM.js.map +1 -0
- package/dist/index.d.ts +17 -28
- package/dist/index.js +6 -6
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +71 -4
- package/dist/internal.js +1 -1
- package/dist/{use-watch-9vSz8P44.d.ts → use-watch-BJDsp7Zo.d.ts} +182 -164
- package/package.json +2 -2
- package/dist/chunk-DQHRLKQS.js.map +0 -1
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { createContext, memo, useContext, useMemo, useRef, useSyncExternalStore, useState, useEffect, useCallback, useId, forwardRef, useLayoutEffect } from 'react';
|
|
2
|
-
import { formatMessage, joinPath, getAt, ShikiUsageError, showDraftPath, uiId, normalizeUiSpec, engineOf, emptySlot, createForm, deepEqual,
|
|
2
|
+
import { pathOf, formatMessage, joinPath, getAt, ShikiUsageError, showDraftPath, uiId, normalizeUiSpec, engineOf, emptySlot, createForm, deepEqual, pathDomId, nearestName } from '@shikiform/core/internal';
|
|
3
3
|
import { jsx, Fragment } from 'react/jsx-runtime';
|
|
4
4
|
import { useVirtualizer } from '@tanstack/react-virtual';
|
|
5
|
+
import { isFieldErrors, SubmitSupersededError } from '@shikiform/core';
|
|
5
6
|
|
|
6
7
|
// src/context.tsx
|
|
7
8
|
var FormContext = createContext(null);
|
|
8
9
|
var ScopeContext = createContext({ path: "", index: -1 });
|
|
9
10
|
var ArrayContext = createContext(null);
|
|
11
|
+
var TreeContext = createContext(null);
|
|
10
12
|
var RowWrapContext = createContext(null);
|
|
11
13
|
function useFormContext() {
|
|
12
14
|
return useRequiredForm("useFormContext()");
|
|
@@ -126,7 +128,7 @@ function FieldGroup({ value, children }) {
|
|
|
126
128
|
}
|
|
127
129
|
function useFieldGroupRegistration(path) {
|
|
128
130
|
const registrar = useContext(FieldGroupContext);
|
|
129
|
-
useEffect(() => registrar ? registrar.add(path) : void 0, [registrar, path]);
|
|
131
|
+
useEffect(() => path && registrar ? registrar.add(path) : void 0, [registrar, path]);
|
|
130
132
|
}
|
|
131
133
|
var ids = /* @__PURE__ */ new WeakMap();
|
|
132
134
|
var nextId = 0;
|
|
@@ -225,22 +227,24 @@ function useDragProps(key, move, encode, decode) {
|
|
|
225
227
|
|
|
226
228
|
// src/hooks/use-array.ts
|
|
227
229
|
function useArrayField(name, opts) {
|
|
228
|
-
const scope = useScope();
|
|
229
|
-
const form = useRequiredForm(`useArrayField('${name}') / <ArrayField name="${name}">`);
|
|
230
|
-
const path = resolveName(form, scope, name);
|
|
231
|
-
if (process.env.NODE_ENV !== "production") assertArrayValue(path, form.getValue(path));
|
|
232
|
-
useFieldGroupRegistration(path);
|
|
233
|
-
return useArrayAt(path, opts?.rowKey);
|
|
234
|
-
}
|
|
235
|
-
function useArray() {
|
|
236
230
|
const ctx = useContext(ArrayContext);
|
|
237
|
-
|
|
231
|
+
const scope = useScope();
|
|
232
|
+
if (name == null && !ctx) {
|
|
238
233
|
if (process.env.NODE_ENV !== "production") {
|
|
239
|
-
throw new ShikiUsageError("
|
|
234
|
+
throw new ShikiUsageError("useArrayField() must render inside <ArrayField> (a row or its container component: ArrayItems / ArrayCards / ArrayTable).");
|
|
240
235
|
}
|
|
241
|
-
throw new Error("
|
|
236
|
+
throw new Error("useArrayField() must render inside <ArrayField>");
|
|
237
|
+
}
|
|
238
|
+
const form = useRequiredForm(name != null ? `useArrayField('${name}') / <ArrayField name="${name}">` : "useArrayField()");
|
|
239
|
+
let path;
|
|
240
|
+
if (name != null) {
|
|
241
|
+
path = resolveName(form, scope, name);
|
|
242
|
+
if (process.env.NODE_ENV !== "production") assertArrayValue(path, form.getValue(path));
|
|
243
|
+
} else {
|
|
244
|
+
path = ctx?.path;
|
|
242
245
|
}
|
|
243
|
-
|
|
246
|
+
useFieldGroupRegistration(name != null ? path : void 0);
|
|
247
|
+
return useArrayAt(path ?? null, name != null ? opts?.rowKey : ctx?.rowKey);
|
|
244
248
|
}
|
|
245
249
|
function assertArrayValue(path, value) {
|
|
246
250
|
if (value == null || Array.isArray(value)) return;
|
|
@@ -250,44 +254,51 @@ function assertArrayValue(path, value) {
|
|
|
250
254
|
);
|
|
251
255
|
}
|
|
252
256
|
function useArrayAt(path, rowKey) {
|
|
253
|
-
const form = useRequiredForm(`the array '${path}'`);
|
|
257
|
+
const form = useRequiredForm(path ? `the array '${path}'` : "useArrayField()");
|
|
254
258
|
useSyncExternalStore(
|
|
255
|
-
(onStoreChange) => form.subscribeStructure(path, onStoreChange)
|
|
256
|
-
|
|
257
|
-
() => form.getStructureVersion(path)
|
|
259
|
+
(onStoreChange) => path ? form.subscribeStructure(path, onStoreChange) : () => {
|
|
260
|
+
},
|
|
261
|
+
() => path ? form.getStructureVersion(path) : 0,
|
|
262
|
+
() => path ? form.getStructureVersion(path) : 0
|
|
258
263
|
);
|
|
259
|
-
form.registerArray(path, rowKey);
|
|
260
|
-
const dragProps = useDragProps(path, (from, to) =>
|
|
264
|
+
if (path) form.registerArray(path, rowKey);
|
|
265
|
+
const dragProps = useDragProps(path ?? "", (from, to) => {
|
|
266
|
+
if (path) form.moveArray(path, from, to);
|
|
267
|
+
}, String, toIndex);
|
|
268
|
+
const messages = useArrayMessages(path);
|
|
269
|
+
const at = path ?? "";
|
|
261
270
|
return {
|
|
262
|
-
path,
|
|
263
|
-
keys: form.structureKeys(path, rowKey),
|
|
264
|
-
push: (row) =>
|
|
265
|
-
|
|
266
|
-
|
|
271
|
+
path: at,
|
|
272
|
+
keys: path ? form.structureKeys(path, rowKey) : [],
|
|
273
|
+
push: (row) => {
|
|
274
|
+
if (path) form.pushArray(path, row);
|
|
275
|
+
},
|
|
276
|
+
insert: (index, row) => {
|
|
277
|
+
if (path) form.insertArray(path, index, row);
|
|
278
|
+
},
|
|
279
|
+
remove: (index) => {
|
|
280
|
+
if (path) form.removeArray(path, index);
|
|
281
|
+
},
|
|
267
282
|
/** `to` is the final index. */
|
|
268
|
-
move: (from, to) =>
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
283
|
+
move: (from, to) => {
|
|
284
|
+
if (path) form.moveArray(path, from, to);
|
|
285
|
+
},
|
|
286
|
+
swap: (indexA, indexB) => {
|
|
287
|
+
if (path) form.swapArray(path, indexA, indexB);
|
|
288
|
+
},
|
|
289
|
+
replace: (index, row) => {
|
|
290
|
+
if (path) form.replaceArray(path, index, row);
|
|
291
|
+
},
|
|
292
|
+
dragProps,
|
|
293
|
+
errors: messages.errors,
|
|
294
|
+
warnings: messages.warnings
|
|
272
295
|
};
|
|
273
296
|
}
|
|
274
|
-
function
|
|
275
|
-
const
|
|
276
|
-
return Number.isInteger(index) ? index : void 0;
|
|
277
|
-
}
|
|
278
|
-
var NO_MESSAGES = { errors: [], warnings: [] };
|
|
279
|
-
function useArrayErrors() {
|
|
280
|
-
const ctx = useContext(ArrayContext);
|
|
281
|
-
if (!ctx) {
|
|
282
|
-
if (process.env.NODE_ENV !== "production") {
|
|
283
|
-
throw new ShikiUsageError("useArrayErrors() must render inside an <ArrayField> (its container, or a row).");
|
|
284
|
-
}
|
|
285
|
-
throw new Error("useArrayErrors() must render inside an <ArrayField>");
|
|
286
|
-
}
|
|
287
|
-
const form = useRequiredForm("useArrayErrors()");
|
|
288
|
-
const path = ctx.path;
|
|
297
|
+
function useArrayMessages(path) {
|
|
298
|
+
const form = useRequiredForm(path ? `the array '${path}'` : "useArrayField()");
|
|
289
299
|
const cache = useRef(NO_MESSAGES);
|
|
290
300
|
const read = () => {
|
|
301
|
+
if (!path) return NO_MESSAGES;
|
|
291
302
|
const binding = form.binding(path);
|
|
292
303
|
const prev = cache.current;
|
|
293
304
|
if (deepEqual(prev.errors, binding.errors) && deepEqual(prev.warnings, binding.warnings)) return prev;
|
|
@@ -296,11 +307,17 @@ function useArrayErrors() {
|
|
|
296
307
|
return next;
|
|
297
308
|
};
|
|
298
309
|
return useSyncExternalStore(
|
|
299
|
-
(onStoreChange) => form.subscribePath(path, onStoreChange)
|
|
310
|
+
(onStoreChange) => path ? form.subscribePath(path, onStoreChange) : () => {
|
|
311
|
+
},
|
|
300
312
|
read,
|
|
301
313
|
read
|
|
302
314
|
);
|
|
303
315
|
}
|
|
316
|
+
function toIndex(raw) {
|
|
317
|
+
const index = Number(raw);
|
|
318
|
+
return Number.isInteger(index) ? index : void 0;
|
|
319
|
+
}
|
|
320
|
+
var NO_MESSAGES = { errors: [], warnings: [] };
|
|
304
321
|
var IdPrefixContext = createContext("");
|
|
305
322
|
function IdPrefixProvider({ prefix, children }) {
|
|
306
323
|
return /* @__PURE__ */ jsx(IdPrefixContext.Provider, { value: prefix, children });
|
|
@@ -390,11 +407,13 @@ function withResolvedSlots(field, kit, defaults) {
|
|
|
390
407
|
}
|
|
391
408
|
|
|
392
409
|
// src/hooks/field-lifecycle.ts
|
|
410
|
+
var FLAG_KEYS = ["visible", "disabled", "readOnly", "omit"];
|
|
393
411
|
function applyJsxField(form, path, spec) {
|
|
412
|
+
const flags = {};
|
|
413
|
+
for (const key of FLAG_KEYS) if (spec[key] !== void 0) flags[key] = spec[key];
|
|
414
|
+
const hasFlags = FLAG_KEYS.some((key) => spec[key] !== void 0);
|
|
394
415
|
if (spec.rules === void 0 && spec.required === void 0) {
|
|
395
|
-
if (
|
|
396
|
-
form.registerField(path, { display: spec.display, pattern: spec.pattern, jsx: true });
|
|
397
|
-
}
|
|
416
|
+
if (hasFlags) form.registerField(path, { ...flags, jsx: true });
|
|
398
417
|
return void 0;
|
|
399
418
|
}
|
|
400
419
|
const lease = form.mountJsxField(path, spec);
|
|
@@ -416,7 +435,14 @@ function useField(name, options) {
|
|
|
416
435
|
const path = resolveName(form, scope, String(name));
|
|
417
436
|
const ui = options?.ui;
|
|
418
437
|
if (ui && process.env.NODE_ENV !== "production") warnInstanceOptions(path, ui, options);
|
|
419
|
-
const spec = options && !ui ? {
|
|
438
|
+
const spec = options && !ui ? {
|
|
439
|
+
rules: options.rules,
|
|
440
|
+
required: options.required,
|
|
441
|
+
visible: options.visible,
|
|
442
|
+
disabled: options.disabled,
|
|
443
|
+
readOnly: options.readOnly,
|
|
444
|
+
omit: options.omit
|
|
445
|
+
} : void 0;
|
|
420
446
|
const ownsValidation = !!spec && (spec.rules !== void 0 || spec.required !== void 0);
|
|
421
447
|
const latestSpec = useRef(spec);
|
|
422
448
|
latestSpec.current = spec;
|
|
@@ -456,7 +482,7 @@ function useUiNode(form, path, ui, kind) {
|
|
|
456
482
|
}
|
|
457
483
|
var warnedInstance = /* @__PURE__ */ new Set();
|
|
458
484
|
function warnInstanceOptions(path, ui, options) {
|
|
459
|
-
const owned = ["rules", "required", "enum"].filter((key2) => options[key2] !== void 0);
|
|
485
|
+
const owned = ["rules", "required", "enum", "omit"].filter((key2) => options[key2] !== void 0);
|
|
460
486
|
const key = `${path}#${ui}`;
|
|
461
487
|
if (!owned.length || warnedInstance.has(key)) return;
|
|
462
488
|
warnedInstance.add(key);
|
|
@@ -480,8 +506,12 @@ function useJsxEnum(form, path, input) {
|
|
|
480
506
|
}
|
|
481
507
|
var NO_FIELD = `[shikiform] useField() without a name reads the surrounding <Field>, but this component is not inside one. Render it inside <Field name="\u2026"> (or as its component / decorator / children), or pass a name: useField('email').`;
|
|
482
508
|
function uiJsx(options, path) {
|
|
483
|
-
|
|
484
|
-
|
|
509
|
+
return {
|
|
510
|
+
...uiLayer(options, path),
|
|
511
|
+
visible: options.visible,
|
|
512
|
+
disabled: options.disabled,
|
|
513
|
+
readOnly: options.readOnly
|
|
514
|
+
};
|
|
485
515
|
}
|
|
486
516
|
function uiLayer(options, path) {
|
|
487
517
|
const { title, description, component, decorator } = options;
|
|
@@ -492,25 +522,94 @@ function useFieldContextOrNull() {
|
|
|
492
522
|
return useContext(FieldContext);
|
|
493
523
|
}
|
|
494
524
|
function useTreeField(name, opts) {
|
|
495
|
-
const
|
|
525
|
+
const ctx = useContext(TreeContext);
|
|
496
526
|
const scope = useScope();
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
527
|
+
if (name == null && !scope.pos) {
|
|
528
|
+
if (process.env.NODE_ENV !== "production") {
|
|
529
|
+
throw new ShikiUsageError("useTreeField() must render inside a tree node (<TreeField>).");
|
|
530
|
+
}
|
|
531
|
+
throw new Error("useTreeField() must render inside a tree node (<TreeField>)");
|
|
532
|
+
}
|
|
533
|
+
const form = useRequiredForm(name != null ? `useTreeField('${name}') / <TreeField name="${name}">` : "useTreeField()");
|
|
534
|
+
const treePath = name != null ? resolveName(form, scope, name) : ctx?.path ?? form.treeRoot(scope.path) ?? scope.path;
|
|
535
|
+
const options = name != null ? opts : { childrenKey: ctx?.childrenKey, rowKey: ctx?.rowKey };
|
|
536
|
+
useFieldGroupRegistration(name != null ? treePath : void 0);
|
|
537
|
+
const tree = useTreeAt(treePath, options, name != null);
|
|
538
|
+
const nodePath = name == null ? scope.path : "";
|
|
539
|
+
const own = useOwnMessages(nodePath || null);
|
|
540
|
+
const record = useWatch(useCallback((d) => nodePath ? form.query(nodePath).$value : void 0, [form, nodePath]));
|
|
541
|
+
const pos = scope.pos;
|
|
542
|
+
const nodeDrag = useCallback((at) => tree.dragProps(at ?? pos ?? []), [tree.dragProps, pos]);
|
|
543
|
+
if (name != null) return tree;
|
|
544
|
+
return {
|
|
545
|
+
...tree,
|
|
546
|
+
path: scope.path,
|
|
547
|
+
treePath: tree.treePath,
|
|
548
|
+
errors: own.errors,
|
|
549
|
+
warnings: own.warnings,
|
|
550
|
+
pos: pos ?? [],
|
|
551
|
+
index: pos ? pos[pos.length - 1] ?? scope.index : scope.index,
|
|
552
|
+
record,
|
|
553
|
+
dragProps: nodeDrag
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
function useTreeAt(path, opts, subscribe = true) {
|
|
557
|
+
const form = useRequiredForm(path ? `the tree '${path}'` : "useTreeField()");
|
|
558
|
+
if (path) form.registerTree(path, opts);
|
|
500
559
|
useSyncExternalStore(
|
|
501
|
-
(onStoreChange) => form.subscribeStructure(path, onStoreChange)
|
|
502
|
-
|
|
503
|
-
() => form.getStructureVersion(path)
|
|
560
|
+
(onStoreChange) => path && subscribe ? form.subscribeStructure(path, onStoreChange) : () => {
|
|
561
|
+
},
|
|
562
|
+
() => path && subscribe ? form.getStructureVersion(path) : 0,
|
|
563
|
+
() => path && subscribe ? form.getStructureVersion(path) : 0
|
|
504
564
|
);
|
|
505
|
-
const value = form.getValue(path);
|
|
506
|
-
const dragProps = useDragProps(path
|
|
565
|
+
const value = path ? form.getValue(path) : void 0;
|
|
566
|
+
const dragProps = useDragProps(path ?? "", (from, to) => {
|
|
567
|
+
if (path) form.moveTree(path, from, to);
|
|
568
|
+
}, (pos) => pos.join("."), toPos);
|
|
569
|
+
const messages = useOwnMessages(subscribe ? path : null);
|
|
570
|
+
const at = path ?? "";
|
|
507
571
|
return {
|
|
508
|
-
path,
|
|
572
|
+
path: at,
|
|
573
|
+
treePath: at,
|
|
574
|
+
errors: messages.errors,
|
|
575
|
+
warnings: messages.warnings,
|
|
509
576
|
value: Array.isArray(value) ? value : [],
|
|
510
|
-
|
|
577
|
+
push: (node, parentPos) => {
|
|
578
|
+
if (path) form.pushTree(path, node, parentPos);
|
|
579
|
+
},
|
|
580
|
+
insert: (pos, node) => {
|
|
581
|
+
if (path) form.insertTree(path, pos, node);
|
|
582
|
+
},
|
|
583
|
+
remove: (pos) => {
|
|
584
|
+
if (path) form.removeTree(path, pos);
|
|
585
|
+
},
|
|
586
|
+
replace: (pos, node) => {
|
|
587
|
+
if (path) form.replaceTree(path, pos, node);
|
|
588
|
+
},
|
|
589
|
+
move: (from, to) => path ? form.moveTree(path, from, to) : { ok: false },
|
|
511
590
|
dragProps
|
|
512
591
|
};
|
|
513
592
|
}
|
|
593
|
+
function useOwnMessages(path) {
|
|
594
|
+
const form = useRequiredForm(path ? `the tree '${path}'` : "useTreeField()");
|
|
595
|
+
const cache = useRef(NO_MESSAGES2);
|
|
596
|
+
const read = () => {
|
|
597
|
+
if (!path) return NO_MESSAGES2;
|
|
598
|
+
const binding = form.binding(path);
|
|
599
|
+
const prev = cache.current;
|
|
600
|
+
if (deepEqual(prev.errors, binding.errors) && deepEqual(prev.warnings, binding.warnings)) return prev;
|
|
601
|
+
const next = { errors: binding.errors, warnings: binding.warnings };
|
|
602
|
+
cache.current = next;
|
|
603
|
+
return next;
|
|
604
|
+
};
|
|
605
|
+
return useSyncExternalStore(
|
|
606
|
+
(onStoreChange) => path ? form.subscribePath(path, onStoreChange) : () => {
|
|
607
|
+
},
|
|
608
|
+
read,
|
|
609
|
+
read
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
var NO_MESSAGES2 = { errors: [], warnings: [] };
|
|
514
613
|
function toPos(raw) {
|
|
515
614
|
const pos = raw.split(".").map(Number);
|
|
516
615
|
return pos.every(Number.isInteger) ? pos : void 0;
|
|
@@ -547,16 +646,19 @@ function handleFormSubmit(form, event, handlers) {
|
|
|
547
646
|
runSubmit(form, handlers);
|
|
548
647
|
}
|
|
549
648
|
function runSubmit(form, handlers) {
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
if (
|
|
555
|
-
|
|
649
|
+
const pending = handlers.onSubmit ? form.submit(handlers.onSubmit) : form.submit();
|
|
650
|
+
void pending.then(
|
|
651
|
+
() => void 0,
|
|
652
|
+
(error) => {
|
|
653
|
+
if (isFieldErrors(error)) {
|
|
654
|
+
handlers.onInvalid?.(error);
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
if (error instanceof SubmitSupersededError) return;
|
|
658
|
+
if (handlers.onSubmitError) handlers.onSubmitError(error);
|
|
659
|
+
else console.error("[shikiform] onSubmit failed", error);
|
|
556
660
|
}
|
|
557
|
-
|
|
558
|
-
console.error("[shikiform] submit callback failed", err);
|
|
559
|
-
});
|
|
661
|
+
);
|
|
560
662
|
}
|
|
561
663
|
function Form({ form, children, onSubmit, onInvalid, onSubmitError, id, kit, ...rest }) {
|
|
562
664
|
const ref = useRef(null);
|
|
@@ -598,7 +700,7 @@ function focusFirstError(root, errors, prefix) {
|
|
|
598
700
|
}
|
|
599
701
|
function Field({ name, children, ...options }) {
|
|
600
702
|
const field = useField(name, options);
|
|
601
|
-
if (field.
|
|
703
|
+
if (!field.visible) return null;
|
|
602
704
|
const Component = field.component;
|
|
603
705
|
const Decorator = field.decorator;
|
|
604
706
|
const content = typeof children === "function" ? children(field) : children !== void 0 ? children : Component ? /* @__PURE__ */ jsx(Component, { ...field.componentProps }) : null;
|
|
@@ -606,13 +708,18 @@ function Field({ name, children, ...options }) {
|
|
|
606
708
|
}
|
|
607
709
|
function ObjectField({
|
|
608
710
|
name,
|
|
609
|
-
|
|
711
|
+
visible,
|
|
712
|
+
disabled,
|
|
713
|
+
readOnly,
|
|
714
|
+
omit,
|
|
610
715
|
children
|
|
611
716
|
}) {
|
|
612
717
|
const form = useRequiredForm(`<ObjectField name="${String(name)}">`);
|
|
613
718
|
const scope = useScope();
|
|
614
719
|
const path = resolveName(form, scope, String(name));
|
|
615
|
-
if (
|
|
720
|
+
if (visible !== void 0 || disabled !== void 0 || readOnly !== void 0 || omit !== void 0) {
|
|
721
|
+
form.registerField(path, { visible, disabled, readOnly, omit });
|
|
722
|
+
}
|
|
616
723
|
const next = useMemo(() => ({ path, index: -1 }), [path]);
|
|
617
724
|
return /* @__PURE__ */ jsx(ScopeContext.Provider, { value: next, children });
|
|
618
725
|
}
|
|
@@ -654,6 +761,15 @@ function ArrayScope({ path, rowKey, children }) {
|
|
|
654
761
|
const value = useMemo(() => ({ path, rowKey }), [path, rowKey]);
|
|
655
762
|
return /* @__PURE__ */ jsx(ArrayContext.Provider, { value, children });
|
|
656
763
|
}
|
|
764
|
+
function TreeScope({
|
|
765
|
+
path,
|
|
766
|
+
childrenKey,
|
|
767
|
+
rowKey,
|
|
768
|
+
children
|
|
769
|
+
}) {
|
|
770
|
+
const value = useMemo(() => ({ path, childrenKey, rowKey }), [path, childrenKey, rowKey]);
|
|
771
|
+
return /* @__PURE__ */ jsx(TreeContext.Provider, { value, children });
|
|
772
|
+
}
|
|
657
773
|
function RowScope({
|
|
658
774
|
path,
|
|
659
775
|
index,
|
|
@@ -675,30 +791,100 @@ function TreeField({
|
|
|
675
791
|
const tree = useTreeField(String(name), { childrenKey, rowKey });
|
|
676
792
|
const identMap = useRef(/* @__PURE__ */ new WeakMap());
|
|
677
793
|
const identSeq = useRef(0);
|
|
794
|
+
const cache = useRef(/* @__PURE__ */ new Map());
|
|
678
795
|
const resolvedChildrenKey = childrenKey ?? "children";
|
|
679
796
|
const resolvedRowKey = rowKey ?? "id";
|
|
680
|
-
const
|
|
797
|
+
const ctx = {
|
|
798
|
+
treePath: tree.treePath,
|
|
799
|
+
childrenKey: resolvedChildrenKey,
|
|
800
|
+
rowKey: resolvedRowKey,
|
|
801
|
+
render: children,
|
|
802
|
+
identMap,
|
|
803
|
+
identSeq
|
|
804
|
+
};
|
|
805
|
+
return /* @__PURE__ */ jsx(TreeScope, { path: tree.treePath, childrenKey, rowKey, children: frameList(tree.value, [], ctx, cache.current) });
|
|
806
|
+
}
|
|
807
|
+
var TreeNodeFrame = memo(
|
|
808
|
+
function TreeNodeFrame2(props) {
|
|
809
|
+
const cache = useRef(/* @__PURE__ */ new Map());
|
|
810
|
+
const children = frameList(props.childNodes, props.pos, props, cache.current);
|
|
811
|
+
return /* @__PURE__ */ jsx(RowScope, { path: props.path, index: props.index, itemKey: props.itemKey, pos: props.pos, children: props.render({
|
|
812
|
+
path: props.path,
|
|
813
|
+
pos: props.pos,
|
|
814
|
+
index: props.index,
|
|
815
|
+
key: props.itemKey,
|
|
816
|
+
depth: props.depth,
|
|
817
|
+
children
|
|
818
|
+
}) });
|
|
819
|
+
},
|
|
820
|
+
(prev, next) => prev.itemKey === next.itemKey && prev.posKey === next.posKey && prev.path === next.path && prev.index === next.index && prev.depth === next.depth && prev.childSig === next.childSig && prev.render === next.render && prev.childrenKey === next.childrenKey && prev.rowKey === next.rowKey && prev.treePath === next.treePath
|
|
821
|
+
);
|
|
822
|
+
function frameList(nodes, parentPos, ctx, cache) {
|
|
823
|
+
if (!nodes.length) return null;
|
|
824
|
+
const seen = /* @__PURE__ */ new Set();
|
|
825
|
+
const frames = nodes.map((nodeObj, index) => {
|
|
681
826
|
const pos = [...parentPos, index];
|
|
682
|
-
const
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
827
|
+
const posKey = pos.join(".");
|
|
828
|
+
const itemKey = treeNodeKey(nodeObj, ctx.rowKey, posKey, ctx.identMap, ctx.identSeq);
|
|
829
|
+
seen.add(itemKey);
|
|
830
|
+
const childNodes = treeChildList(nodeObj, ctx.childrenKey);
|
|
831
|
+
const childSig = treeChildSig(childNodes, pos, ctx);
|
|
832
|
+
const hit = cache.get(itemKey);
|
|
833
|
+
if (hit && hit.sig === childSig && hit.posKey === posKey && hit.render === ctx.render) return hit.element;
|
|
834
|
+
const element = /* @__PURE__ */ jsx(
|
|
835
|
+
TreeNodeFrame,
|
|
836
|
+
{
|
|
837
|
+
treePath: ctx.treePath,
|
|
838
|
+
childrenKey: ctx.childrenKey,
|
|
839
|
+
rowKey: ctx.rowKey,
|
|
840
|
+
render: ctx.render,
|
|
841
|
+
identMap: ctx.identMap,
|
|
842
|
+
identSeq: ctx.identSeq,
|
|
843
|
+
path: pathOf(ctx.treePath, pos, ctx.childrenKey),
|
|
844
|
+
pos,
|
|
845
|
+
posKey,
|
|
846
|
+
index,
|
|
847
|
+
depth: pos.length - 1,
|
|
848
|
+
itemKey,
|
|
849
|
+
childSig,
|
|
850
|
+
childNodes
|
|
851
|
+
},
|
|
852
|
+
itemKey
|
|
853
|
+
);
|
|
854
|
+
cache.set(itemKey, { sig: childSig, posKey, render: ctx.render, element });
|
|
855
|
+
return element;
|
|
700
856
|
});
|
|
701
|
-
|
|
857
|
+
for (const key of cache.keys()) if (!seen.has(key)) cache.delete(key);
|
|
858
|
+
return frames;
|
|
859
|
+
}
|
|
860
|
+
function treeChildList(node, childrenKey) {
|
|
861
|
+
if (!node || typeof node !== "object") return [];
|
|
862
|
+
const sub = node[childrenKey];
|
|
863
|
+
return Array.isArray(sub) ? sub : [];
|
|
864
|
+
}
|
|
865
|
+
function treeNodeKey(node, rowKey, posKey, identMap, identSeq) {
|
|
866
|
+
if (node && typeof node === "object" && node[rowKey] != null) {
|
|
867
|
+
return String(node[rowKey]);
|
|
868
|
+
}
|
|
869
|
+
if (node && typeof node === "object") {
|
|
870
|
+
let id = identMap.current.get(node);
|
|
871
|
+
if (!id) {
|
|
872
|
+
identSeq.current += 1;
|
|
873
|
+
id = `node-${identSeq.current}`;
|
|
874
|
+
identMap.current.set(node, id);
|
|
875
|
+
}
|
|
876
|
+
return id;
|
|
877
|
+
}
|
|
878
|
+
return posKey;
|
|
879
|
+
}
|
|
880
|
+
function treeChildSig(nodes, parentPos, ctx) {
|
|
881
|
+
let sig = "";
|
|
882
|
+
for (let index = 0; index < nodes.length; index++) {
|
|
883
|
+
const posKey = `${parentPos.length ? parentPos.join(".") + "." : ""}${index}`;
|
|
884
|
+
const key = treeNodeKey(nodes[index], ctx.rowKey, posKey, ctx.identMap, ctx.identSeq);
|
|
885
|
+
sig += `${key}@${posKey}(${treeChildSig(treeChildList(nodes[index], ctx.childrenKey), [...parentPos, index], ctx)})`;
|
|
886
|
+
}
|
|
887
|
+
return sig;
|
|
702
888
|
}
|
|
703
889
|
function Watch({ select, children }) {
|
|
704
890
|
const value = useWatch(select);
|
|
@@ -757,7 +943,7 @@ var DEFAULT_TARGETS = {
|
|
|
757
943
|
options: false,
|
|
758
944
|
loading: false
|
|
759
945
|
};
|
|
760
|
-
var MAPPING_META = /* @__PURE__ */ new Set(["format", "parse", "blurOn", "
|
|
946
|
+
var MAPPING_META = /* @__PURE__ */ new Set(["format", "parse", "blurOn", "preview"]);
|
|
761
947
|
function valueFromEvent(...args) {
|
|
762
948
|
const first = args[0];
|
|
763
949
|
if (first && typeof first === "object" && "target" in first && first.target && typeof first.target === "object") {
|
|
@@ -803,7 +989,7 @@ function withBlur(props, field, blurOn, changeProp) {
|
|
|
803
989
|
}
|
|
804
990
|
};
|
|
805
991
|
}
|
|
806
|
-
function applyMapping(field, props, mapping, transforms
|
|
992
|
+
function applyMapping(field, props, mapping, transforms) {
|
|
807
993
|
const target = (key) => mapping[key] === void 0 ? DEFAULT_TARGETS[key] : mapping[key];
|
|
808
994
|
const parseValue = mapping.parse ?? valueFromEvent;
|
|
809
995
|
const out = { ...props };
|
|
@@ -812,9 +998,9 @@ function applyMapping(field, props, mapping, transforms, hasReadPretty) {
|
|
|
812
998
|
const id = target("id");
|
|
813
999
|
if (id) out[id] = props[id] ?? field.id;
|
|
814
1000
|
const disabled = target("disabled");
|
|
815
|
-
if (disabled) out[disabled] = !!out[disabled] || field.disabled;
|
|
1001
|
+
if (disabled) out[disabled] = !!out[disabled] || field.disabled && !field.readOnly;
|
|
816
1002
|
const readOnly = target("readOnly");
|
|
817
|
-
if (readOnly) out[readOnly] = !!out[readOnly] || field.readOnly
|
|
1003
|
+
if (readOnly) out[readOnly] = !!out[readOnly] || field.readOnly;
|
|
818
1004
|
const status = target("status");
|
|
819
1005
|
if (status) {
|
|
820
1006
|
const derived = field.errors.length ? "error" : field.warnings.length ? "warning" : void 0;
|
|
@@ -863,14 +1049,14 @@ function mapProps(...args) {
|
|
|
863
1049
|
if (mapping.blurOn) blur = mapping.blurOn;
|
|
864
1050
|
else if (mapping.onBlur === false) blur = "drop";
|
|
865
1051
|
else if (typeof mapping.onBlur === "string") blur = "rename";
|
|
866
|
-
const rp = mapping.
|
|
867
|
-
const
|
|
1052
|
+
const rp = mapping.preview;
|
|
1053
|
+
const preview = rp ? Array.isArray(rp) ? { component: rp[0], props: rp[1] } : { component: rp } : void 0;
|
|
868
1054
|
const enhancer = {
|
|
869
1055
|
kind: "props",
|
|
870
1056
|
mapping,
|
|
871
1057
|
transforms,
|
|
872
1058
|
...blur ? { blur } : {},
|
|
873
|
-
...
|
|
1059
|
+
...preview ? { preview } : {}
|
|
874
1060
|
};
|
|
875
1061
|
return enhancer;
|
|
876
1062
|
}
|
|
@@ -902,21 +1088,21 @@ var warned = /* @__PURE__ */ new Set();
|
|
|
902
1088
|
function connect(target, ...enhancers) {
|
|
903
1089
|
const mapping = {};
|
|
904
1090
|
const transforms = [];
|
|
905
|
-
let
|
|
1091
|
+
let preview;
|
|
906
1092
|
for (const enhancer of enhancers) {
|
|
907
1093
|
Object.assign(mapping, enhancer.mapping);
|
|
908
1094
|
transforms.push(...enhancer.transforms);
|
|
909
|
-
if (enhancer.
|
|
1095
|
+
if (enhancer.preview) preview = enhancer.preview;
|
|
910
1096
|
}
|
|
911
1097
|
const Target = target;
|
|
912
1098
|
const displayName = `Connected(${Target.displayName ?? Target.name ?? "Component"})`;
|
|
913
1099
|
const render = (field, callerProps, ref) => {
|
|
914
1100
|
const props = { ...field.componentProps, ...callerProps };
|
|
915
|
-
if (
|
|
916
|
-
const
|
|
917
|
-
return /* @__PURE__ */ jsx(
|
|
1101
|
+
if (preview && field.readOnly) {
|
|
1102
|
+
const Preview = preview.component;
|
|
1103
|
+
return /* @__PURE__ */ jsx(Preview, { ...preview.props, ...props, value: field.value, options: props.options ?? field.options });
|
|
918
1104
|
}
|
|
919
|
-
const mapped = applyMapping(field, props, mapping, transforms
|
|
1105
|
+
const mapped = applyMapping(field, props, mapping, transforms);
|
|
920
1106
|
return ref == null ? /* @__PURE__ */ jsx(Target, { ...mapped }) : /* @__PURE__ */ jsx(Target, { ...mapped, ref });
|
|
921
1107
|
};
|
|
922
1108
|
function WithField({ field, rest, forwarded }) {
|
|
@@ -975,8 +1161,9 @@ function useUiNodeOrNull() {
|
|
|
975
1161
|
function UI({
|
|
976
1162
|
name,
|
|
977
1163
|
children,
|
|
978
|
-
|
|
979
|
-
|
|
1164
|
+
visible,
|
|
1165
|
+
disabled,
|
|
1166
|
+
readOnly,
|
|
980
1167
|
...slots
|
|
981
1168
|
}) {
|
|
982
1169
|
const form = useRequiredForm(`<UI name="${String(name)}">`);
|
|
@@ -984,8 +1171,8 @@ function UI({
|
|
|
984
1171
|
const prefix = useIdPrefix();
|
|
985
1172
|
useUiNode(form, path, name, "node");
|
|
986
1173
|
const kit = useFieldKit();
|
|
987
|
-
const ui = withResolvedSlots(withDomId(form.uiBinding(path, name, { ...normalizeUiSpec(slots, `<UI name="${String(name)}">`),
|
|
988
|
-
if (ui.
|
|
1174
|
+
const ui = withResolvedSlots(withDomId(form.uiBinding(path, name, { ...normalizeUiSpec(slots, `<UI name="${String(name)}">`), visible, disabled, readOnly }, "node"), prefix), kit, false);
|
|
1175
|
+
if (!ui.visible) return null;
|
|
989
1176
|
const Component = ui.component;
|
|
990
1177
|
const Decorator = ui.decorator;
|
|
991
1178
|
const inner = typeof children === "function" ? children(ui) : children;
|
|
@@ -1004,7 +1191,7 @@ function useIndex() {
|
|
|
1004
1191
|
return scope.index;
|
|
1005
1192
|
}
|
|
1006
1193
|
function useRecord() {
|
|
1007
|
-
const form = useRequiredForm("useRecord()
|
|
1194
|
+
const form = useRequiredForm("useRecord()");
|
|
1008
1195
|
const scope = useScope();
|
|
1009
1196
|
if (scope.index < 0 && !scope.pos) {
|
|
1010
1197
|
if (process.env.NODE_ENV !== "production") {
|
|
@@ -1016,28 +1203,7 @@ function useRecord() {
|
|
|
1016
1203
|
const select = useCallback((_d) => form.query(path).$value, [form, path]);
|
|
1017
1204
|
return useWatch(select);
|
|
1018
1205
|
}
|
|
1019
|
-
function useTreeNode() {
|
|
1020
|
-
const scope = useScope();
|
|
1021
|
-
if (!scope.pos) {
|
|
1022
|
-
if (process.env.NODE_ENV !== "production") {
|
|
1023
|
-
throw new ShikiUsageError("useTreeNode() must render inside a tree node (<TreeField>).");
|
|
1024
|
-
}
|
|
1025
|
-
throw new Error("useTreeNode() must render inside a tree node (<TreeField>)");
|
|
1026
|
-
}
|
|
1027
|
-
const form = useRequiredForm("useRecord() / useTreeNode()");
|
|
1028
|
-
const record = useRecord();
|
|
1029
|
-
const pos = scope.pos;
|
|
1030
|
-
const path = scope.path;
|
|
1031
|
-
const treePath = form.treeRoot(path) ?? path;
|
|
1032
|
-
const move = useCallback((from, to) => form.moveTree(treePath, from, to), [form, treePath]);
|
|
1033
|
-
const baseProps = useDragProps(treePath, move, (at) => at.join("."), (raw) => {
|
|
1034
|
-
const decoded = raw.split(".").map(Number);
|
|
1035
|
-
return decoded.every(Number.isInteger) ? decoded : void 0;
|
|
1036
|
-
});
|
|
1037
|
-
const dragProps = useCallback((at) => baseProps(at ?? pos), [baseProps, pos.join(".")]);
|
|
1038
|
-
return { path, pos, index: pos[pos.length - 1] ?? -1, record, move, dragProps };
|
|
1039
|
-
}
|
|
1040
1206
|
|
|
1041
|
-
export { ArrayContext, ArrayField, ArrayScope, Field, FieldContext, FieldGroup, FieldGroupContext, FieldGroupStore, FieldKit, Form, FormContext, IdPrefixProvider, ObjectField, RowScope, RowWrapContext, ScopeContext, TreeField, UI, UiNodeContext, VirtualRows, Watch, connect, mapProps, resolveName,
|
|
1042
|
-
//# sourceMappingURL=chunk-
|
|
1043
|
-
//# sourceMappingURL=chunk-
|
|
1207
|
+
export { ArrayContext, ArrayField, ArrayScope, Field, FieldContext, FieldGroup, FieldGroupContext, FieldGroupStore, FieldKit, Form, FormContext, IdPrefixProvider, ObjectField, RowScope, RowWrapContext, ScopeContext, TreeContext, TreeField, TreeScope, UI, UiNodeContext, VirtualRows, Watch, connect, mapProps, resolveName, useArrayAt, useArrayField, useAutoIdPrefix, useField, useFieldContextOrNull, useFieldGroup, useFieldGroupErrors, useFieldGroupRegistration, useForm, useFormContext, useFormMessage, useGroupsErrors, useIndex, useRecord, useRequiredForm, useReset, useScope, useSubmit, useTreeAt, useTreeField, useUiNodeOrNull, useWatch, valueFromEvent };
|
|
1208
|
+
//# sourceMappingURL=chunk-A54EUYGM.js.map
|
|
1209
|
+
//# sourceMappingURL=chunk-A54EUYGM.js.map
|