x-ui-design 0.4.72 → 0.4.73

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.
@@ -110,4 +110,5 @@ export interface FormInstance {
110
110
  setOnFieldsChange?: (onFieldsChange?: (changedFields: FieldData[]) => void) => void;
111
111
  setOnFinish?: (onFinish?: ((values: Record<string, RuleTypes>) => void) | undefined) => void;
112
112
  setOnValuesChange?: (onValuesChange?: (changedValues: Record<string, RuleTypes>, allValues: Record<string, RuleTypes>) => void) => void;
113
+ changeStep: (step: number) => void;
113
114
  }
package/dist/index.esm.js CHANGED
@@ -603,35 +603,37 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
603
603
  const rulesRef = useRef({});
604
604
  const warningsRef = useRef({});
605
605
  const _scrollToFirstError = useRef(scrollToFirstError);
606
+ const stepRef = useRef(0);
606
607
  const formHandlersRef = useRef({
607
608
  onFinish,
608
609
  onValuesChange,
609
610
  onFieldsChange
610
611
  });
611
612
  const formRef = useRef({
612
- ...initialValues
613
+ [stepRef.current]: {
614
+ ...initialValues
615
+ }
613
616
  });
614
- const formCatchRef = useRef({});
615
617
  const fieldInstancesRef = useRef({});
616
618
  const [isReseting, setIsReseting] = useState(false);
617
619
  const [errors, setErrors] = useState({});
618
620
  const fieldSubscribers = useRef({});
619
621
  const formSubscribers = useRef([]);
622
+ function getFormFields() {
623
+ return Object.assign({}, ...Object.values(formRef.current));
624
+ }
620
625
  function getFieldInstance(name) {
621
626
  return fieldInstancesRef.current[name] || null;
622
627
  }
623
628
  function getFieldValue(name) {
624
- return formRef.current[name] ?? formCatchRef.current[name];
629
+ return formRef.current[stepRef.current][name];
625
630
  }
626
631
  function getFieldsValue(nameList) {
627
632
  if (!nameList) {
628
- return {
629
- ...formRef.current,
630
- ...formCatchRef.current
631
- };
633
+ return formRef.current[stepRef.current];
632
634
  }
633
635
  return nameList.reduce((acc, key) => {
634
- acc[key] = formRef.current[key] ?? formCatchRef.current[key];
636
+ acc[key] = formRef.current[stepRef.current][key];
635
637
  return acc;
636
638
  }, {});
637
639
  }
@@ -648,10 +650,10 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
648
650
  }));
649
651
  }
650
652
  function setFieldValue(name, value, errors, reset = undefined, touch) {
651
- if (!reset && reset !== null && ([undefined, null].includes(value) || formRef.current[name] === value || formCatchRef.current[name] === value)) {
653
+ if (!reset && reset !== null && ([undefined, null].includes(value) || formRef.current[stepRef.current][name] === value)) {
652
654
  return;
653
655
  }
654
- formRef.current[name] = value;
656
+ formRef.current[stepRef.current][name] = value;
655
657
  if (touch) {
656
658
  touchedFieldsRef.current.add(name);
657
659
  }
@@ -708,18 +710,17 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
708
710
  }
709
711
  function registerField(name, rules = [], remove = false) {
710
712
  if (remove) {
711
- formCatchRef.current[name] = formRef.current[name];
712
- delete formRef.current[name];
713
+ delete formRef.current[stepRef.current]?.[name];
714
+ delete rulesRef.current[name];
713
715
  } else {
714
- if (!(name in formRef.current)) {
715
- formRef.current[name] = formCatchRef.current[name] ?? initialValues?.[name];
716
- delete formCatchRef.current[name];
716
+ if (!(name in formRef.current[stepRef.current])) {
717
+ formRef.current[stepRef.current][name] = initialValues?.[name];
717
718
  }
718
719
  rulesRef.current[name] = rules;
719
720
  }
720
721
  }
721
722
  async function validateField(name) {
722
- const value = formRef.current[name];
723
+ const value = formRef.current[stepRef.current][name];
723
724
  const rules = rulesRef.current[name] || [];
724
725
  const fieldErrors = [];
725
726
  const fieldWarnings = [];
@@ -756,7 +757,7 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
756
757
  return fieldErrors.length === 0;
757
758
  }
758
759
  async function validateFields(nameList) {
759
- const fieldsToValidate = nameList || Object.keys(formRef.current);
760
+ const fieldsToValidate = nameList || Object.keys(formRef.current[stepRef.current]);
760
761
  const results = await Promise.all(fieldsToValidate.map(name => validateField(name)));
761
762
  if (_scrollToFirstError.current) {
762
763
  const firstErrorContent = document.querySelectorAll('.xUi-form-item-error')?.[0];
@@ -775,10 +776,10 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
775
776
  return results.every(valid => valid);
776
777
  }
777
778
  function resetFields(nameList, showError = true) {
779
+ const formData = getFormFields();
778
780
  if (nameList?.length) {
779
781
  nameList.forEach(name => {
780
- formRef.current[name] = initialValues[name];
781
- formCatchRef.current = {};
782
+ formData[name] = initialValues[name];
782
783
  touchedFieldsRef.current.delete(name);
783
784
  delete warningsRef.current[name];
784
785
  setErrors(prev => ({
@@ -791,8 +792,7 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
791
792
  touchedFieldsRef.current.clear();
792
793
  warningsRef.current = {};
793
794
  Object.keys({
794
- ...formRef.current,
795
- ...formCatchRef.current
795
+ ...formData
796
796
  }).forEach(name => {
797
797
  setFieldValue(name, initialValues[name], undefined, showError);
798
798
  });
@@ -802,15 +802,10 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
802
802
  }
803
803
  async function submit() {
804
804
  setScrollToFirstError(true);
805
+ const formData = getFormFields();
805
806
  return (await validateFields()) ? (() => {
806
- formHandlersRef.current.onFinish?.({
807
- ...formRef.current,
808
- ...formCatchRef.current
809
- });
810
- return {
811
- ...formRef.current,
812
- ...formCatchRef.current
813
- };
807
+ formHandlersRef.current.onFinish?.(formData);
808
+ return formData;
814
809
  })() : undefined;
815
810
  }
816
811
  function subscribeToField(name, callback) {
@@ -849,6 +844,12 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
849
844
  function setOnFinish(onFinish) {
850
845
  formHandlersRef.current.onFinish = onFinish;
851
846
  }
847
+ function changeStep(step) {
848
+ stepRef.current = step ?? 0;
849
+ if (!formRef.current[stepRef.current]) {
850
+ formRef.current[stepRef.current] = {};
851
+ }
852
+ }
852
853
  const formInstance = {
853
854
  submit,
854
855
  setFields,
@@ -876,7 +877,8 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
876
877
  isReseting,
877
878
  setOnFinish,
878
879
  setOnFieldsChange,
879
- setOnValuesChange
880
+ setOnValuesChange,
881
+ changeStep
880
882
  };
881
883
  return formInstance;
882
884
  };