@shikiform/react 0.1.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-EYDJ6I7P.js → chunk-A54EUYGM.js} +318 -133
- package/dist/chunk-A54EUYGM.js.map +1 -0
- package/dist/index.d.ts +23 -25
- package/dist/index.js +10 -7
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +92 -4
- package/dist/internal.js +1 -1
- package/dist/{use-watch-BZlpyvHw.d.ts → use-watch-BJDsp7Zo.d.ts} +220 -229
- package/package.json +2 -2
- package/dist/chunk-EYDJ6I7P.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, 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,9 +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
|
-
form.registerField(path, {
|
|
416
|
+
if (hasFlags) form.registerField(path, { ...flags, jsx: true });
|
|
396
417
|
return void 0;
|
|
397
418
|
}
|
|
398
419
|
const lease = form.mountJsxField(path, spec);
|
|
@@ -414,7 +435,14 @@ function useField(name, options) {
|
|
|
414
435
|
const path = resolveName(form, scope, String(name));
|
|
415
436
|
const ui = options?.ui;
|
|
416
437
|
if (ui && process.env.NODE_ENV !== "production") warnInstanceOptions(path, ui, options);
|
|
417
|
-
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;
|
|
418
446
|
const ownsValidation = !!spec && (spec.rules !== void 0 || spec.required !== void 0);
|
|
419
447
|
const latestSpec = useRef(spec);
|
|
420
448
|
latestSpec.current = spec;
|
|
@@ -454,7 +482,7 @@ function useUiNode(form, path, ui, kind) {
|
|
|
454
482
|
}
|
|
455
483
|
var warnedInstance = /* @__PURE__ */ new Set();
|
|
456
484
|
function warnInstanceOptions(path, ui, options) {
|
|
457
|
-
const owned = ["rules", "required", "enum"].filter((key2) => options[key2] !== void 0);
|
|
485
|
+
const owned = ["rules", "required", "enum", "omit"].filter((key2) => options[key2] !== void 0);
|
|
458
486
|
const key = `${path}#${ui}`;
|
|
459
487
|
if (!owned.length || warnedInstance.has(key)) return;
|
|
460
488
|
warnedInstance.add(key);
|
|
@@ -478,8 +506,12 @@ function useJsxEnum(form, path, input) {
|
|
|
478
506
|
}
|
|
479
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').`;
|
|
480
508
|
function uiJsx(options, path) {
|
|
481
|
-
|
|
482
|
-
|
|
509
|
+
return {
|
|
510
|
+
...uiLayer(options, path),
|
|
511
|
+
visible: options.visible,
|
|
512
|
+
disabled: options.disabled,
|
|
513
|
+
readOnly: options.readOnly
|
|
514
|
+
};
|
|
483
515
|
}
|
|
484
516
|
function uiLayer(options, path) {
|
|
485
517
|
const { title, description, component, decorator } = options;
|
|
@@ -490,25 +522,94 @@ function useFieldContextOrNull() {
|
|
|
490
522
|
return useContext(FieldContext);
|
|
491
523
|
}
|
|
492
524
|
function useTreeField(name, opts) {
|
|
493
|
-
const
|
|
525
|
+
const ctx = useContext(TreeContext);
|
|
494
526
|
const scope = useScope();
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
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);
|
|
498
559
|
useSyncExternalStore(
|
|
499
|
-
(onStoreChange) => form.subscribeStructure(path, onStoreChange)
|
|
500
|
-
|
|
501
|
-
() => 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
|
|
502
564
|
);
|
|
503
|
-
const value = form.getValue(path);
|
|
504
|
-
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 ?? "";
|
|
505
571
|
return {
|
|
506
|
-
path,
|
|
572
|
+
path: at,
|
|
573
|
+
treePath: at,
|
|
574
|
+
errors: messages.errors,
|
|
575
|
+
warnings: messages.warnings,
|
|
507
576
|
value: Array.isArray(value) ? value : [],
|
|
508
|
-
|
|
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 },
|
|
509
590
|
dragProps
|
|
510
591
|
};
|
|
511
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: [] };
|
|
512
613
|
function toPos(raw) {
|
|
513
614
|
const pos = raw.split(".").map(Number);
|
|
514
615
|
return pos.every(Number.isInteger) ? pos : void 0;
|
|
@@ -545,16 +646,19 @@ function handleFormSubmit(form, event, handlers) {
|
|
|
545
646
|
runSubmit(form, handlers);
|
|
546
647
|
}
|
|
547
648
|
function runSubmit(form, handlers) {
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
if (
|
|
553
|
-
|
|
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);
|
|
554
660
|
}
|
|
555
|
-
|
|
556
|
-
console.error("[shikiform] submit callback failed", err);
|
|
557
|
-
});
|
|
661
|
+
);
|
|
558
662
|
}
|
|
559
663
|
function Form({ form, children, onSubmit, onInvalid, onSubmitError, id, kit, ...rest }) {
|
|
560
664
|
const ref = useRef(null);
|
|
@@ -596,7 +700,7 @@ function focusFirstError(root, errors, prefix) {
|
|
|
596
700
|
}
|
|
597
701
|
function Field({ name, children, ...options }) {
|
|
598
702
|
const field = useField(name, options);
|
|
599
|
-
if (field.
|
|
703
|
+
if (!field.visible) return null;
|
|
600
704
|
const Component = field.component;
|
|
601
705
|
const Decorator = field.decorator;
|
|
602
706
|
const content = typeof children === "function" ? children(field) : children !== void 0 ? children : Component ? /* @__PURE__ */ jsx(Component, { ...field.componentProps }) : null;
|
|
@@ -604,13 +708,18 @@ function Field({ name, children, ...options }) {
|
|
|
604
708
|
}
|
|
605
709
|
function ObjectField({
|
|
606
710
|
name,
|
|
607
|
-
|
|
711
|
+
visible,
|
|
712
|
+
disabled,
|
|
713
|
+
readOnly,
|
|
714
|
+
omit,
|
|
608
715
|
children
|
|
609
716
|
}) {
|
|
610
717
|
const form = useRequiredForm(`<ObjectField name="${String(name)}">`);
|
|
611
718
|
const scope = useScope();
|
|
612
719
|
const path = resolveName(form, scope, String(name));
|
|
613
|
-
if (
|
|
720
|
+
if (visible !== void 0 || disabled !== void 0 || readOnly !== void 0 || omit !== void 0) {
|
|
721
|
+
form.registerField(path, { visible, disabled, readOnly, omit });
|
|
722
|
+
}
|
|
614
723
|
const next = useMemo(() => ({ path, index: -1 }), [path]);
|
|
615
724
|
return /* @__PURE__ */ jsx(ScopeContext.Provider, { value: next, children });
|
|
616
725
|
}
|
|
@@ -652,6 +761,15 @@ function ArrayScope({ path, rowKey, children }) {
|
|
|
652
761
|
const value = useMemo(() => ({ path, rowKey }), [path, rowKey]);
|
|
653
762
|
return /* @__PURE__ */ jsx(ArrayContext.Provider, { value, children });
|
|
654
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
|
+
}
|
|
655
773
|
function RowScope({
|
|
656
774
|
path,
|
|
657
775
|
index,
|
|
@@ -671,7 +789,102 @@ function TreeField({
|
|
|
671
789
|
children
|
|
672
790
|
}) {
|
|
673
791
|
const tree = useTreeField(String(name), { childrenKey, rowKey });
|
|
674
|
-
|
|
792
|
+
const identMap = useRef(/* @__PURE__ */ new WeakMap());
|
|
793
|
+
const identSeq = useRef(0);
|
|
794
|
+
const cache = useRef(/* @__PURE__ */ new Map());
|
|
795
|
+
const resolvedChildrenKey = childrenKey ?? "children";
|
|
796
|
+
const resolvedRowKey = rowKey ?? "id";
|
|
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) => {
|
|
826
|
+
const pos = [...parentPos, index];
|
|
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;
|
|
856
|
+
});
|
|
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;
|
|
675
888
|
}
|
|
676
889
|
function Watch({ select, children }) {
|
|
677
890
|
const value = useWatch(select);
|
|
@@ -730,7 +943,7 @@ var DEFAULT_TARGETS = {
|
|
|
730
943
|
options: false,
|
|
731
944
|
loading: false
|
|
732
945
|
};
|
|
733
|
-
var MAPPING_META = /* @__PURE__ */ new Set(["format", "parse", "blurOn"]);
|
|
946
|
+
var MAPPING_META = /* @__PURE__ */ new Set(["format", "parse", "blurOn", "preview"]);
|
|
734
947
|
function valueFromEvent(...args) {
|
|
735
948
|
const first = args[0];
|
|
736
949
|
if (first && typeof first === "object" && "target" in first && first.target && typeof first.target === "object") {
|
|
@@ -741,15 +954,6 @@ function valueFromEvent(...args) {
|
|
|
741
954
|
}
|
|
742
955
|
return first;
|
|
743
956
|
}
|
|
744
|
-
function mapFieldProps(field, props, ...mappers) {
|
|
745
|
-
const mapping = {};
|
|
746
|
-
const transforms = [];
|
|
747
|
-
for (const mapper of mappers) {
|
|
748
|
-
if (typeof mapper === "function") transforms.push(mapper);
|
|
749
|
-
else Object.assign(mapping, mapper);
|
|
750
|
-
}
|
|
751
|
-
return applyMapping(field, { ...field.componentProps, ...props }, mapping, transforms, false);
|
|
752
|
-
}
|
|
753
957
|
function controlOptions(options) {
|
|
754
958
|
return options.map((option) => {
|
|
755
959
|
const out = { label: option.label, value: option.value, disabled: option.disabled };
|
|
@@ -785,7 +989,7 @@ function withBlur(props, field, blurOn, changeProp) {
|
|
|
785
989
|
}
|
|
786
990
|
};
|
|
787
991
|
}
|
|
788
|
-
function applyMapping(field, props, mapping, transforms
|
|
992
|
+
function applyMapping(field, props, mapping, transforms) {
|
|
789
993
|
const target = (key) => mapping[key] === void 0 ? DEFAULT_TARGETS[key] : mapping[key];
|
|
790
994
|
const parseValue = mapping.parse ?? valueFromEvent;
|
|
791
995
|
const out = { ...props };
|
|
@@ -794,9 +998,9 @@ function applyMapping(field, props, mapping, transforms, hasReadPretty) {
|
|
|
794
998
|
const id = target("id");
|
|
795
999
|
if (id) out[id] = props[id] ?? field.id;
|
|
796
1000
|
const disabled = target("disabled");
|
|
797
|
-
if (disabled) out[disabled] = !!out[disabled] || field.disabled;
|
|
1001
|
+
if (disabled) out[disabled] = !!out[disabled] || field.disabled && !field.readOnly;
|
|
798
1002
|
const readOnly = target("readOnly");
|
|
799
|
-
if (readOnly) out[readOnly] = !!out[readOnly] || field.readOnly
|
|
1003
|
+
if (readOnly) out[readOnly] = !!out[readOnly] || field.readOnly;
|
|
800
1004
|
const status = target("status");
|
|
801
1005
|
if (status) {
|
|
802
1006
|
const derived = field.errors.length ? "error" : field.warnings.length ? "warning" : void 0;
|
|
@@ -845,15 +1049,17 @@ function mapProps(...args) {
|
|
|
845
1049
|
if (mapping.blurOn) blur = mapping.blurOn;
|
|
846
1050
|
else if (mapping.onBlur === false) blur = "drop";
|
|
847
1051
|
else if (typeof mapping.onBlur === "string") blur = "rename";
|
|
848
|
-
const
|
|
1052
|
+
const rp = mapping.preview;
|
|
1053
|
+
const preview = rp ? Array.isArray(rp) ? { component: rp[0], props: rp[1] } : { component: rp } : void 0;
|
|
1054
|
+
const enhancer = {
|
|
1055
|
+
kind: "props",
|
|
1056
|
+
mapping,
|
|
1057
|
+
transforms,
|
|
1058
|
+
...blur ? { blur } : {},
|
|
1059
|
+
...preview ? { preview } : {}
|
|
1060
|
+
};
|
|
849
1061
|
return enhancer;
|
|
850
1062
|
}
|
|
851
|
-
function mapBlur(on) {
|
|
852
|
-
return { kind: "props", mapping: { blurOn: on }, transforms: [], blur: on };
|
|
853
|
-
}
|
|
854
|
-
function mapReadPretty(component, props) {
|
|
855
|
-
return { kind: "readPretty", component, props };
|
|
856
|
-
}
|
|
857
1063
|
var NOT_STATIC = /* @__PURE__ */ new Set([
|
|
858
1064
|
// React component statics and function / class built-ins: never copied.
|
|
859
1065
|
"displayName",
|
|
@@ -882,22 +1088,21 @@ var warned = /* @__PURE__ */ new Set();
|
|
|
882
1088
|
function connect(target, ...enhancers) {
|
|
883
1089
|
const mapping = {};
|
|
884
1090
|
const transforms = [];
|
|
885
|
-
let
|
|
1091
|
+
let preview;
|
|
886
1092
|
for (const enhancer of enhancers) {
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
} else readPretty = enhancer;
|
|
1093
|
+
Object.assign(mapping, enhancer.mapping);
|
|
1094
|
+
transforms.push(...enhancer.transforms);
|
|
1095
|
+
if (enhancer.preview) preview = enhancer.preview;
|
|
891
1096
|
}
|
|
892
1097
|
const Target = target;
|
|
893
1098
|
const displayName = `Connected(${Target.displayName ?? Target.name ?? "Component"})`;
|
|
894
1099
|
const render = (field, callerProps, ref) => {
|
|
895
1100
|
const props = { ...field.componentProps, ...callerProps };
|
|
896
|
-
if (
|
|
897
|
-
const
|
|
898
|
-
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 });
|
|
899
1104
|
}
|
|
900
|
-
const mapped = applyMapping(field, props, mapping, transforms
|
|
1105
|
+
const mapped = applyMapping(field, props, mapping, transforms);
|
|
901
1106
|
return ref == null ? /* @__PURE__ */ jsx(Target, { ...mapped }) : /* @__PURE__ */ jsx(Target, { ...mapped, ref });
|
|
902
1107
|
};
|
|
903
1108
|
function WithField({ field, rest, forwarded }) {
|
|
@@ -956,8 +1161,9 @@ function useUiNodeOrNull() {
|
|
|
956
1161
|
function UI({
|
|
957
1162
|
name,
|
|
958
1163
|
children,
|
|
959
|
-
|
|
960
|
-
|
|
1164
|
+
visible,
|
|
1165
|
+
disabled,
|
|
1166
|
+
readOnly,
|
|
961
1167
|
...slots
|
|
962
1168
|
}) {
|
|
963
1169
|
const form = useRequiredForm(`<UI name="${String(name)}">`);
|
|
@@ -965,8 +1171,8 @@ function UI({
|
|
|
965
1171
|
const prefix = useIdPrefix();
|
|
966
1172
|
useUiNode(form, path, name, "node");
|
|
967
1173
|
const kit = useFieldKit();
|
|
968
|
-
const ui = withResolvedSlots(withDomId(form.uiBinding(path, name, { ...normalizeUiSpec(slots, `<UI name="${String(name)}">`),
|
|
969
|
-
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;
|
|
970
1176
|
const Component = ui.component;
|
|
971
1177
|
const Decorator = ui.decorator;
|
|
972
1178
|
const inner = typeof children === "function" ? children(ui) : children;
|
|
@@ -985,11 +1191,11 @@ function useIndex() {
|
|
|
985
1191
|
return scope.index;
|
|
986
1192
|
}
|
|
987
1193
|
function useRecord() {
|
|
988
|
-
const form = useRequiredForm("useRecord()
|
|
1194
|
+
const form = useRequiredForm("useRecord()");
|
|
989
1195
|
const scope = useScope();
|
|
990
1196
|
if (scope.index < 0 && !scope.pos) {
|
|
991
1197
|
if (process.env.NODE_ENV !== "production") {
|
|
992
|
-
throw new ShikiUsageError("useRecord() must render inside an array row (<ArrayField> rows, ArrayTable cells included) or a tree node (<
|
|
1198
|
+
throw new ShikiUsageError("useRecord() must render inside an array row (<ArrayField> rows, ArrayTable cells included) or a tree node (<TreeField>).");
|
|
993
1199
|
}
|
|
994
1200
|
throw new Error("useRecord() must render inside an array row or tree node");
|
|
995
1201
|
}
|
|
@@ -997,28 +1203,7 @@ function useRecord() {
|
|
|
997
1203
|
const select = useCallback((_d) => form.query(path).$value, [form, path]);
|
|
998
1204
|
return useWatch(select);
|
|
999
1205
|
}
|
|
1000
|
-
function useTreeNode() {
|
|
1001
|
-
const scope = useScope();
|
|
1002
|
-
if (!scope.pos) {
|
|
1003
|
-
if (process.env.NODE_ENV !== "production") {
|
|
1004
|
-
throw new ShikiUsageError("useTreeNode() must render inside a tree node: wrap it in <RowScope path={\u2026} pos={\u2026}>.");
|
|
1005
|
-
}
|
|
1006
|
-
throw new Error("useTreeNode() must render inside a tree node (<RowScope path pos>)");
|
|
1007
|
-
}
|
|
1008
|
-
const form = useRequiredForm("useRecord() / useTreeNode()");
|
|
1009
|
-
const record = useRecord();
|
|
1010
|
-
const pos = scope.pos;
|
|
1011
|
-
const path = scope.path;
|
|
1012
|
-
const treePath = form.treeRoot(path) ?? path;
|
|
1013
|
-
const move = useCallback((from, to) => form.moveTree(treePath, from, to), [form, treePath]);
|
|
1014
|
-
const baseProps = useDragProps(treePath, move, (at) => at.join("."), (raw) => {
|
|
1015
|
-
const decoded = raw.split(".").map(Number);
|
|
1016
|
-
return decoded.every(Number.isInteger) ? decoded : void 0;
|
|
1017
|
-
});
|
|
1018
|
-
const dragProps = useCallback((at) => baseProps(at ?? pos), [baseProps, pos.join(".")]);
|
|
1019
|
-
return { path, pos, index: pos[pos.length - 1] ?? -1, record, move, dragProps };
|
|
1020
|
-
}
|
|
1021
1206
|
|
|
1022
|
-
export { ArrayContext, ArrayField, ArrayScope, Field, FieldGroup, FieldGroupStore, FieldKit, Form, FormContext, IdPrefixProvider, ObjectField, RowScope, RowWrapContext, ScopeContext, TreeField, UI, VirtualRows, Watch, connect,
|
|
1023
|
-
//# sourceMappingURL=chunk-
|
|
1024
|
-
//# 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
|