x-ui-design 0.6.29 → 0.6.31

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.
@@ -1,4 +1,14 @@
1
1
  import { RuleTypes } from '../types';
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, scrollToFirstError?: boolean, onFinish?: ((values: Record<string, RuleTypes>) => void) | undefined) => FormInstance;
2
+ import type { FieldData, FieldError, FormInstance } from '../types/form';
3
+ declare const useForm: ({ initialValues, onFieldsChange, onValuesChange, scrollToFirstError, onFinish, onFinishFailed }: {
4
+ initialValues?: Record<string, RuleTypes>;
5
+ onFieldsChange?: (changedFields: FieldData[]) => void;
6
+ onValuesChange?: (changedValues: Record<string, RuleTypes>, allValues: Record<string, RuleTypes>) => void;
7
+ scrollToFirstError?: boolean;
8
+ onFinish?: ((values: Record<string, RuleTypes>) => void) | undefined;
9
+ onFinishFailed?: (errorInfo: {
10
+ values: Record<string, RuleTypes>;
11
+ errorFields: Pick<FieldError, "errors" | "name">[];
12
+ }) => void;
13
+ }) => FormInstance;
4
14
  export { useForm };
@@ -83,7 +83,7 @@ export interface FormItemChildComponentProps {
83
83
  ref?: FieldInstancesRef | null;
84
84
  dependencies?: string[];
85
85
  }
86
- export interface FormInstance {
86
+ export type FormInstance = {
87
87
  submit: () => Promise<Record<string, RuleTypes> | undefined>;
88
88
  setFields: (fields: FieldData[]) => void;
89
89
  resetFields: (nameList?: string[], showError?: boolean | null) => void;
@@ -113,5 +113,9 @@ export interface FormInstance {
113
113
  setOnFieldsChange?: (onFieldsChange?: (changedFields: FieldData[]) => void) => void;
114
114
  setOnFinish?: (onFinish?: ((values: Record<string, RuleTypes>) => void) | undefined) => void;
115
115
  setOnValuesChange?: (onValuesChange?: (changedValues: Record<string, RuleTypes>, allValues: Record<string, RuleTypes>) => void) => void;
116
+ setOnFinishFailed?: (onFinishFailed?: (errorInfo: {
117
+ values: Record<string, RuleTypes>;
118
+ errorFields: Pick<FieldError, 'errors' | 'name'>[];
119
+ }) => void) => void;
116
120
  changeStep: (step: number) => void;
117
- }
121
+ };
package/dist/index.esm.js CHANGED
@@ -598,7 +598,14 @@ const SpinerIcon = () => /*#__PURE__*/React.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, scrollToFirstError, onFinish) => {
601
+ const useForm = ({
602
+ initialValues = {},
603
+ onFieldsChange,
604
+ onValuesChange,
605
+ scrollToFirstError,
606
+ onFinish,
607
+ onFinishFailed
608
+ }) => {
602
609
  const touchedFieldsRef = useRef(new Set());
603
610
  const rulesRef = useRef({});
604
611
  const warningsRef = useRef({});
@@ -607,7 +614,8 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
607
614
  const formHandlersRef = useRef({
608
615
  onFinish,
609
616
  onValuesChange,
610
- onFieldsChange
617
+ onFieldsChange,
618
+ onFinishFailed
611
619
  });
612
620
  const formRef = useRef({
613
621
  [stepRef.current]: {
@@ -775,19 +783,10 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
775
783
  if (_scrollToFirstError.current) {
776
784
  const firstErrorContent = document.querySelectorAll('.xUi-form-item-has-error')?.[0];
777
785
  if (firstErrorContent) {
778
- let toggleDisplay = false;
779
786
  const _firstErrorContent = firstErrorContent.closest('.xUi-form-item');
780
- if (_firstErrorContent.style.display === 'none') {
781
- toggleDisplay = true;
782
- _firstErrorContent.style.display = 'block';
783
- }
784
- firstErrorContent.closest('.xUi-form-item')?.scrollIntoView({
787
+ _firstErrorContent?.scrollIntoView({
785
788
  behavior: 'smooth'
786
789
  });
787
- if (toggleDisplay) {
788
- toggleDisplay = false;
789
- _firstErrorContent.style.display = 'none';
790
- }
791
790
  }
792
791
  }
793
792
  return results.every(valid => valid);
@@ -824,7 +823,14 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
824
823
  return (await validateFields()) ? (() => {
825
824
  formHandlersRef.current.onFinish?.(formData);
826
825
  return formData;
827
- })() : undefined;
826
+ })() : (() => {
827
+ const errorFields = formInstance.getFieldsError().filter(e => e.errors.length);
828
+ formHandlersRef.current.onFinishFailed?.({
829
+ values: formInstance.getFieldsValue(),
830
+ errorFields
831
+ });
832
+ return undefined;
833
+ })();
828
834
  }
829
835
  function subscribeToField(name, callback) {
830
836
  if (!fieldSubscribers.current[name]) {
@@ -868,6 +874,9 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
868
874
  function setOnValuesChange(onValuesChange) {
869
875
  formHandlersRef.current.onValuesChange = onValuesChange;
870
876
  }
877
+ function setOnFinishFailed(onFinishFailed) {
878
+ formHandlersRef.current.onFinishFailed = onFinishFailed;
879
+ }
871
880
  function setOnFinish(onFinish) {
872
881
  formHandlersRef.current.onFinish = onFinish;
873
882
  }
@@ -907,6 +916,7 @@ const useForm = (initialValues = {}, onFieldsChange, onValuesChange, scrollToFir
907
916
  setOnFinish,
908
917
  setOnFieldsChange,
909
918
  setOnValuesChange,
919
+ setOnFinishFailed,
910
920
  changeStep
911
921
  };
912
922
  return formInstance;
@@ -1206,7 +1216,12 @@ const Form$1 = ({
1206
1216
  scrollToFirstError = false,
1207
1217
  ...rest
1208
1218
  }) => {
1209
- const internalForm = useForm(initialValues, onFieldsChange, onValuesChange);
1219
+ const internalForm = useForm({
1220
+ initialValues,
1221
+ onFieldsChange,
1222
+ onValuesChange,
1223
+ onFinishFailed
1224
+ });
1210
1225
  const formRef = useRef(null);
1211
1226
  const formInstance = useMemo(() => form || internalForm, [form, internalForm]);
1212
1227
  const handleSubmit = useCallback(async e => {
@@ -1214,7 +1229,7 @@ const Form$1 = ({
1214
1229
  if (await formInstance.validateFields()) {
1215
1230
  onFinish?.(formInstance.getFieldsValue());
1216
1231
  } else if (onFinishFailed) {
1217
- const errorFields = formInstance.getFieldsError();
1232
+ const errorFields = formInstance.getFieldsError().filter(e => e.errors.length);
1218
1233
  onFinishFailed({
1219
1234
  values: formInstance.getFieldsValue(),
1220
1235
  errorFields
@@ -1224,19 +1239,22 @@ const Form$1 = ({
1224
1239
  const childrenList = useMemo(() => flattenChildren(children), [children]);
1225
1240
  const formClassName = useMemo(() => `${prefixCls} ${className}`.trim(), [prefixCls, className]);
1226
1241
  useEffect(() => {
1242
+ if (onFinish) {
1243
+ formInstance.setOnFinish?.(onFinish);
1244
+ }
1227
1245
  if (onFieldsChange) {
1228
1246
  formInstance.setOnFieldsChange?.(onFieldsChange);
1229
1247
  }
1230
1248
  if (onValuesChange) {
1231
1249
  formInstance.setOnValuesChange?.(onValuesChange);
1232
1250
  }
1233
- if (onFinish) {
1234
- formInstance.setOnFinish?.(onFinish);
1251
+ if (onFinishFailed) {
1252
+ formInstance.setOnFinishFailed?.(onFinishFailed);
1235
1253
  }
1236
1254
  if (scrollToFirstError) {
1237
1255
  formInstance.setScrollToFirstError(scrollToFirstError);
1238
1256
  }
1239
- }, [formInstance, onFieldsChange, onValuesChange, onFinish, scrollToFirstError]);
1257
+ }, [formInstance, onFieldsChange, onValuesChange, onFinishFailed, onFinish, scrollToFirstError]);
1240
1258
  const injectPropsIntoFinalLeaf = useCallback(child => {
1241
1259
  if (! /*#__PURE__*/isValidElement(child)) {
1242
1260
  return child;