x-ui-design 0.3.75 → 0.3.76
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/esm/types/hooks/useForm.d.ts +1 -1
- package/dist/esm/types/types/form.d.ts +3 -0
- package/dist/index.esm.js +16 -2
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +16 -2
- package/dist/index.js.map +1 -1
- package/lib/components/Form/Form.tsx +10 -6
- package/lib/hooks/useForm.ts +18 -2
- package/lib/types/form.ts +3 -0
- package/package.json +1 -1
- package/src/app/page.tsx +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { RuleTypes } from '../types';
|
|
2
2
|
import type { FieldData, FormInstance } from '../types/form';
|
|
3
|
-
declare const useForm: (initialValues?: Record<string, RuleTypes>, onFieldsChange?: (changedFields: FieldData[]) => void, onValuesChange?: (changedValues: Record<string, RuleTypes>, allValues: Record<string, RuleTypes>) => void) => FormInstance;
|
|
3
|
+
declare const useForm: (initialValues?: Record<string, RuleTypes>, onFieldsChange?: (changedFields: FieldData[]) => void, onValuesChange?: (changedValues: Record<string, RuleTypes>, allValues: Record<string, RuleTypes>) => void, scrollToFirstError?: boolean) => FormInstance;
|
|
4
4
|
export { useForm };
|
|
@@ -46,6 +46,7 @@ export type FormProps = DefaultProps & {
|
|
|
46
46
|
values: Record<string, RuleTypes>;
|
|
47
47
|
errorFields: Pick<FieldError, 'errors' | 'name'>[];
|
|
48
48
|
}) => void;
|
|
49
|
+
scrollToFirstError?: boolean;
|
|
49
50
|
};
|
|
50
51
|
export type FormItemProps = DefaultProps & {
|
|
51
52
|
name: string;
|
|
@@ -101,5 +102,7 @@ export interface FormInstance {
|
|
|
101
102
|
onFieldsChange?: (changedFields: FieldData[]) => void;
|
|
102
103
|
onValuesChange?: (changedValues: Record<string, RuleTypes>, allValues: Record<string, RuleTypes>) => void;
|
|
103
104
|
getFieldInstance: (fieldName: string) => FieldInstancesRef;
|
|
105
|
+
setScrollToFirstError: (value: boolean) => void;
|
|
106
|
+
scrollToFirstError?: boolean;
|
|
104
107
|
isReseting: boolean;
|
|
105
108
|
}
|
package/dist/index.esm.js
CHANGED
|
@@ -598,10 +598,11 @@ const SpinerIcon = () => /*#__PURE__*/React$1.createElement("svg", {
|
|
|
598
598
|
d: "M988 548c-19.9 0-36-16.1-36-36 0-59.4-11.6-117-34.6-171.3a440.45 440.45 0 00-94.3-139.9 437.71 437.71 0 00-139.9-94.3C629 83.6 571.4 72 512 72c-19.9 0-36-16.1-36-36s16.1-36 36-36c69.1 0 136.2 13.5 199.3 40.3C772.3 66 827 103 874 150c47 47 83.9 101.8 109.7 162.7 26.7 63.1 40.2 130.2 40.2 199.3.1 19.9-16 36-35.9 36z"
|
|
599
599
|
}));
|
|
600
600
|
|
|
601
|
-
const useForm = (initialValues = {}, onFieldsChange, onValuesChange) => {
|
|
601
|
+
const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFirstError) => {
|
|
602
602
|
const touchedFieldsRef = useRef(new Set());
|
|
603
603
|
const rulesRef = useRef({});
|
|
604
604
|
const warningsRef = useRef({});
|
|
605
|
+
const _scrollToFirstError = useRef(scrollToFirstError);
|
|
605
606
|
const formRef = useRef({
|
|
606
607
|
...initialValues
|
|
607
608
|
});
|
|
@@ -737,6 +738,12 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange) => {
|
|
|
737
738
|
[name]: fieldErrors
|
|
738
739
|
}));
|
|
739
740
|
warningsRef.current[name] = fieldWarnings;
|
|
741
|
+
if (_scrollToFirstError.current) {
|
|
742
|
+
const firstErrorContent = document.querySelectorAll('.xUi-form-item-error')?.[0];
|
|
743
|
+
if (firstErrorContent) {
|
|
744
|
+
firstErrorContent.closest('.xUi-form-item')?.scrollIntoView();
|
|
745
|
+
}
|
|
746
|
+
}
|
|
740
747
|
return fieldErrors.length === 0;
|
|
741
748
|
}
|
|
742
749
|
async function validateFields(nameList) {
|
|
@@ -793,6 +800,9 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange) => {
|
|
|
793
800
|
fieldCallbacks.forEach(unsubscribe => unsubscribe());
|
|
794
801
|
};
|
|
795
802
|
}
|
|
803
|
+
function setScrollToFirstError(value) {
|
|
804
|
+
_scrollToFirstError.current = value;
|
|
805
|
+
}
|
|
796
806
|
const formInstance = {
|
|
797
807
|
submit,
|
|
798
808
|
setFields,
|
|
@@ -815,6 +825,8 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange) => {
|
|
|
815
825
|
onValuesChange,
|
|
816
826
|
getFieldInstance,
|
|
817
827
|
subscribeToFields,
|
|
828
|
+
setScrollToFirstError,
|
|
829
|
+
scrollToFirstError,
|
|
818
830
|
isReseting
|
|
819
831
|
};
|
|
820
832
|
return formInstance;
|
|
@@ -1091,10 +1103,12 @@ const Form$1 = ({
|
|
|
1091
1103
|
onValuesChange,
|
|
1092
1104
|
onFieldsChange,
|
|
1093
1105
|
layout = 'horizontal',
|
|
1106
|
+
scrollToFirstError = false,
|
|
1094
1107
|
...rest
|
|
1095
1108
|
}) => {
|
|
1096
|
-
const internalForm = useForm(initialValues, onFieldsChange, onValuesChange);
|
|
1109
|
+
const internalForm = useForm(initialValues, onFieldsChange, onValuesChange, scrollToFirstError);
|
|
1097
1110
|
const formInstance = form || internalForm;
|
|
1111
|
+
formInstance.setScrollToFirstError(scrollToFirstError);
|
|
1098
1112
|
const formRef = useRef(null);
|
|
1099
1113
|
const handleSubmit = async e => {
|
|
1100
1114
|
e.preventDefault();
|