@ditojs/admin 2.26.2 → 2.27.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.
@@ -624,37 +624,53 @@ export function shouldRenderSchema(schema, context) {
624
624
  )
625
625
  }
626
626
 
627
- export function getDefaultValue(schema) {
627
+ function getContext(context) {
628
+ return isFunction(context) ? context() : context
629
+ }
630
+
631
+ export function getDefaultValue(schema, context) {
628
632
  // Support default values both on schema and on component level.
629
633
  // NOTE: At the time of creation, components may not be instantiated, (e.g. if
630
634
  // entries are created through nested forms, the parent form isn't mounted) so
631
635
  // we can't use `dataPath` to get to components, and the `defaultValue` from
632
636
  // there. That's why `defaultValue` is defined statically in the components:
633
- const defaultValue = schema.default
634
- const value =
635
- defaultValue !== undefined
636
- ? defaultValue
637
+ const defaultValue =
638
+ schema.default !== undefined
639
+ ? schema.default
637
640
  : getTypeOptions(schema)?.defaultValue
638
- return isFunction(value)
639
- ? // TODO: Pass `DitoContext` here too, with the (incomplete) item and al
640
- // the other bits!
641
- value(schema)
642
- : clone(value)
641
+ return isFunction(defaultValue)
642
+ ? defaultValue(getContext(context))
643
+ : clone(defaultValue)
643
644
  }
644
645
 
645
- export function ignoreMissingValue(schema) {
646
- const typeOptions = getTypeOptions(schema)
647
- return !!(
648
- typeOptions?.excludeValue || typeOptions?.ignoreMissingValue?.(schema)
649
- )
646
+ export function shouldExcludeValue(schema, context) {
647
+ const excludeValue =
648
+ schema.exclude !== undefined
649
+ ? schema.exclude
650
+ : getTypeOptions(schema)?.excludeValue
651
+ return isFunction(excludeValue)
652
+ ? excludeValue(getContext(context))
653
+ : !!excludeValue
654
+ }
655
+
656
+ export function shouldIgnoreMissingValue(schema, context) {
657
+ return !!getTypeOptions(schema)?.ignoreMissingValue?.(getContext(context))
650
658
  }
651
659
 
652
660
  export function setDefaultValues(schema, data = {}, component) {
653
661
  const options = { component, rootData: data }
654
662
 
655
- const processBefore = (schema, data, name) => {
656
- if (!(name in data) && !ignoreMissingValue(schema)) {
657
- data[name] = getDefaultValue(schema)
663
+ const processBefore = (schema, data, name, dataPath) => {
664
+ const context = () =>
665
+ new DitoContext(component, {
666
+ schema,
667
+ name,
668
+ data,
669
+ dataPath,
670
+ rootData: options.rootData
671
+ })
672
+ if (!(name in data) && !shouldIgnoreMissingValue(schema, context)) {
673
+ data[name] = getDefaultValue(schema, context)
658
674
  }
659
675
  }
660
676
 
@@ -675,20 +691,20 @@ export function computeValue(schema, data, name, dataPath, {
675
691
  component = null,
676
692
  rootData = component?.rootData
677
693
  } = {}) {
694
+ const context = () =>
695
+ new DitoContext(component, {
696
+ schema,
697
+ // Override value to prevent endless recursion through calling the
698
+ // getter for `this.value` in `DitoContext`:
699
+ value: data[name],
700
+ name,
701
+ data,
702
+ dataPath,
703
+ rootData
704
+ })
678
705
  const { compute } = schema
679
706
  if (compute) {
680
- const value = compute(
681
- new DitoContext(component, {
682
- schema,
683
- // Override value to prevent endless recursion through calling the
684
- // getter for `this.value` in `DitoContext`:
685
- value: data[name],
686
- name,
687
- data,
688
- dataPath,
689
- rootData
690
- })
691
- )
707
+ const value = compute(getContext(context))
692
708
  if (value !== undefined) {
693
709
  // Access `data[name]` directly to update the value without calling
694
710
  // parse():
@@ -698,10 +714,10 @@ export function computeValue(schema, data, name, dataPath, {
698
714
  }
699
715
  }
700
716
  // If the value is still missing after compute, set the default for it:
701
- if (!(name in data) && !ignoreMissingValue(schema)) {
717
+ if (!(name in data) && !shouldIgnoreMissingValue(schema, context)) {
702
718
  // TODO: Fix side-effects
703
719
  // eslint-disable-next-line vue/no-side-effects-in-computed-properties
704
- data[name] = getDefaultValue(schema)
720
+ data[name] = getDefaultValue(schema, context)
705
721
  }
706
722
  // Now access the value. This is important for reactivity and needs to
707
723
  // happen after all prior manipulation of `data[name]`, see above:
@@ -772,13 +788,11 @@ export function processData(schema, sourceSchema, data, dataPath, {
772
788
  }
773
789
 
774
790
  const processAfter = (schema, data, name, dataPath, processedData) => {
775
- const { wrapPrimitives, exclude, process } = schema
791
+ const { wrapPrimitives, process } = schema
776
792
  let value = processedData[name]
777
793
 
778
- const typeOptions = getTypeOptions(schema)
779
-
780
794
  // NOTE: We don't cache this context, since `value` is changing.
781
- const getContext = () =>
795
+ const context = () =>
782
796
  new DitoContext(component, {
783
797
  schema,
784
798
  value,
@@ -799,23 +813,18 @@ export function processData(schema, sourceSchema, data, dataPath, {
799
813
 
800
814
  // Each component type can provide its own static `processValue()` method
801
815
  // to convert the data for storage.
802
- const processValue = typeOptions?.processValue
816
+ const processValue = getTypeOptions(schema)?.processValue
803
817
  if (processValue) {
804
- value = processValue(schema, value, dataPath, graph)
818
+ value = processValue(getContext(context), graph)
805
819
  }
806
820
 
807
821
  // Handle the user's `process()` callback next, if one is provided, so that
808
822
  // it can modify data in `processedData` even if it provides `exclude: true`
809
823
  if (process) {
810
- value = process(getContext())
824
+ value = process(getContext(context))
811
825
  }
812
826
 
813
- if (
814
- typeOptions?.excludeValue ||
815
- // Support functions next to booleans for `schema.exclude`:
816
- exclude === true ||
817
- isFunction(exclude) && exclude(getContext())
818
- ) {
827
+ if (shouldExcludeValue(schema, context)) {
819
828
  delete processedData[name]
820
829
  } else {
821
830
  processedData[name] = value