@juantroconisf/lib 9.4.0 → 11.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/dist/index.mjs CHANGED
@@ -566,10 +566,17 @@ function useForm(schema, {
566
566
  const { compositeKey, fieldPath, realPath, value } = resolution;
567
567
  const meta = metadataRef.current.get(compositeKey);
568
568
  const isTouched = meta?.isTouched;
569
+ let isRequired = false;
570
+ try {
571
+ const rule = getRule(fieldPath, validationSchema);
572
+ if (rule) isRequired = rule.describe().optional === false;
573
+ } catch {
574
+ }
569
575
  return {
570
576
  id: compositeKey,
571
577
  isInvalid: Boolean(isTouched && meta?.isInvalid),
572
578
  errorMessage: isTouched ? meta?.errorMessage || "" : "",
579
+ isRequired,
573
580
  onBlur: () => {
574
581
  if (metadataRef.current.get(compositeKey)?.isTouched) return;
575
582
  validateField(
@@ -592,7 +599,7 @@ function useForm(schema, {
592
599
  }
593
600
  };
594
601
  },
595
- [validateField]
602
+ [validateField, getRule, validationSchema]
596
603
  );
597
604
  const on = useMemo(
598
605
  () => ({
@@ -644,6 +651,62 @@ function useForm(schema, {
644
651
  handleFieldChange(data, fixed);
645
652
  }
646
653
  };
654
+ },
655
+ numberInput: (...args) => {
656
+ const data = resolveFieldData(
657
+ args,
658
+ stateRef.current,
659
+ getIndex,
660
+ getNestedValue
661
+ );
662
+ if (!data) return {};
663
+ return {
664
+ ...createHandlers(data),
665
+ value: data.value ?? 0,
666
+ onValueChange: (v) => handleFieldChange(data, v)
667
+ };
668
+ },
669
+ checkbox: (...args) => {
670
+ const data = resolveFieldData(
671
+ args,
672
+ stateRef.current,
673
+ getIndex,
674
+ getNestedValue
675
+ );
676
+ if (!data) return {};
677
+ return {
678
+ ...createHandlers(data),
679
+ isSelected: Boolean(data.value),
680
+ onValueChange: (v) => handleFieldChange(data, v)
681
+ };
682
+ },
683
+ switch: (...args) => {
684
+ const data = resolveFieldData(
685
+ args,
686
+ stateRef.current,
687
+ getIndex,
688
+ getNestedValue
689
+ );
690
+ if (!data) return {};
691
+ return {
692
+ ...createHandlers(data),
693
+ isSelected: Boolean(data.value),
694
+ onValueChange: (v) => handleFieldChange(data, v)
695
+ };
696
+ },
697
+ radio: (...args) => {
698
+ const data = resolveFieldData(
699
+ args,
700
+ stateRef.current,
701
+ getIndex,
702
+ getNestedValue
703
+ );
704
+ if (!data) return {};
705
+ return {
706
+ ...createHandlers(data),
707
+ value: data.value ?? "",
708
+ onValueChange: (v) => handleFieldChange(data, v)
709
+ };
647
710
  }
648
711
  }),
649
712
  [createHandlers, getIndex, handleFieldChange]
@@ -663,7 +726,7 @@ function useForm(schema, {
663
726
  });
664
727
  });
665
728
  },
666
- removeItem: (arrayKey, index) => {
729
+ removeItemByIndexByIndex: (arrayKey, index) => {
667
730
  const currentArr = getNestedValue(stateRef.current, arrayKey) || [];
668
731
  const item = currentArr[index];
669
732
  const idKey = arrayIdentifiers?.[arrayKey] || "id";
@@ -700,10 +763,10 @@ function useForm(schema, {
700
763
  setMetadata((prev) => removeCompositeKeysByPrefix(prev, prefix));
701
764
  }
702
765
  },
703
- updateItem: (arrayKey, index, value) => {
766
+ updateByIndex: (arrayKey, index, value) => {
704
767
  setState((prev) => {
705
768
  const arr = [...getNestedValue(prev, arrayKey) || []];
706
- arr[index] = value;
769
+ arr[index] = typeof arr[index] === "object" && typeof value === "object" ? { ...arr[index], ...value } : value;
707
770
  return handleNestedChange({
708
771
  state: prev,
709
772
  id: arrayKey,
@@ -712,7 +775,22 @@ function useForm(schema, {
712
775
  });
713
776
  });
714
777
  },
715
- moveItem: (arrayKey, from, to) => {
778
+ updateById: (arrayKey, itemId, value) => {
779
+ const index = getIndex(arrayKey, itemId);
780
+ if (index !== void 0) {
781
+ setState((prev) => {
782
+ const arr = [...getNestedValue(prev, arrayKey) || []];
783
+ arr[index] = typeof arr[index] === "object" && typeof value === "object" ? { ...arr[index], ...value } : value;
784
+ return handleNestedChange({
785
+ state: prev,
786
+ id: arrayKey,
787
+ value: arr,
788
+ hasNestedValues: String(arrayKey).includes(".")
789
+ });
790
+ });
791
+ }
792
+ },
793
+ moveItemByIndex: (arrayKey, from, to) => {
716
794
  setState((prev) => {
717
795
  const arr = [...getNestedValue(prev, arrayKey) || []];
718
796
  const [item] = arr.splice(from, 1);
@@ -742,7 +820,7 @@ function useForm(schema, {
742
820
  });
743
821
  }
744
822
  },
745
- getItem: (arrayKey, itemId) => {
823
+ getItemById: (arrayKey, itemId) => {
746
824
  const index = getIndex(arrayKey, itemId);
747
825
  if (index === void 0) return void 0;
748
826
  return getNestedValue(stateRef.current, arrayKey)[index];
@@ -772,12 +850,26 @@ function useForm(schema, {
772
850
  },
773
851
  [validateField]
774
852
  );
775
- const polymorphicOnValueChange = useCallback(
776
- (...args) => {
777
- const value = args[args.length - 1];
778
- const idArgs = args.slice(0, args.length - 1);
853
+ const scalarOnFieldChange = useCallback(
854
+ (id, value) => {
855
+ const data = resolveFieldData(
856
+ [id],
857
+ stateRef.current,
858
+ getIndex,
859
+ getNestedValue
860
+ );
861
+ if (data) handleFieldChange(data, value);
862
+ },
863
+ [getIndex, handleFieldChange]
864
+ );
865
+ const arrayItemChange = useCallback(
866
+ ({
867
+ at: compositePath,
868
+ id: itemId,
869
+ value
870
+ }) => {
779
871
  const data = resolveFieldData(
780
- idArgs,
872
+ [compositePath, itemId],
781
873
  stateRef.current,
782
874
  getIndex,
783
875
  getNestedValue
@@ -786,7 +878,7 @@ function useForm(schema, {
786
878
  },
787
879
  [getIndex, handleFieldChange]
788
880
  );
789
- const polymorphicOnSelectionChange = useCallback(
881
+ const scalarOnSelectionChange = useCallback(
790
882
  (id, val) => {
791
883
  const fixed = typeof val === "string" || val === null ? val : Array.from(val);
792
884
  let nextState = handleNestedChange({
@@ -800,6 +892,23 @@ function useForm(schema, {
800
892
  },
801
893
  [validateField]
802
894
  );
895
+ const arraySelectionChange = useCallback(
896
+ ({
897
+ at: compositePath,
898
+ id: itemId,
899
+ value: val
900
+ }) => {
901
+ const fixed = typeof val === "string" || val === null ? val : Array.from(val);
902
+ const data = resolveFieldData(
903
+ [compositePath, itemId],
904
+ stateRef.current,
905
+ getIndex,
906
+ getNestedValue
907
+ );
908
+ if (data) handleFieldChange(data, fixed);
909
+ },
910
+ [getIndex, handleFieldChange]
911
+ );
803
912
  const onFormSubmit = useCallback(
804
913
  (fn) => (e) => {
805
914
  e.preventDefault();
@@ -866,9 +975,11 @@ function useForm(schema, {
866
975
  metadata,
867
976
  on,
868
977
  helpers,
869
- onFieldChange: polymorphicOnValueChange,
978
+ onFieldChange: scalarOnFieldChange,
979
+ onArrayItemChange: arrayItemChange,
870
980
  onFieldBlur: onBlur,
871
- onSelectionChange: polymorphicOnSelectionChange,
981
+ onSelectionChange: scalarOnSelectionChange,
982
+ onArraySelectionChange: arraySelectionChange,
872
983
  isDirty: Array.from(metadata.values()).some((m) => m.isTouched),
873
984
  onFormReset: handleReset,
874
985
  onFormSubmit,
@@ -881,8 +992,10 @@ function useForm(schema, {
881
992
  on,
882
993
  helpers,
883
994
  onBlur,
884
- polymorphicOnValueChange,
885
- polymorphicOnSelectionChange,
995
+ scalarOnFieldChange,
996
+ arrayItemChange,
997
+ scalarOnSelectionChange,
998
+ arraySelectionChange,
886
999
  validateAll,
887
1000
  onFormSubmit,
888
1001
  ControlledForm,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@juantroconisf/lib",
3
- "version": "9.4.0",
3
+ "version": "11.0.0",
4
4
  "description": "A form validation library for HeroUI.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",