@dovetail-v2/refine 0.3.14 → 0.3.15-alpha.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.
@@ -23,6 +23,7 @@ export interface YamlFormProps<Model extends ResourceModel = ResourceModel> {
23
23
  rules?: YamlFormRule[];
24
24
  transformInitValues?: (values: Record<string, unknown>) => Record<string, unknown>;
25
25
  transformApplyValues?: (values: Unstructured) => Unstructured;
26
+ beforeSubmit?: (values: Unstructured, setErrors: (errors: string[]) => void) => Promise<Unstructured>;
26
27
  onSaveButtonPropsChange?: (saveButtonProps: {
27
28
  disabled?: boolean;
28
29
  onClick: () => void;
@@ -9,6 +9,7 @@ export type UseFormReturnType<TQueryFnData extends BaseRecord = BaseRecord, TErr
9
9
  disabled: boolean;
10
10
  onClick: (e: React.BaseSyntheticEvent) => void;
11
11
  };
12
+ beforeSubmitErrors: string[];
12
13
  };
13
14
  export type UseFormProps<TQueryFnData extends BaseRecord = BaseRecord, TError extends HttpError = HttpError, TVariables extends FieldValues = FieldValues, TContext extends object = object, TData extends BaseRecord = TQueryFnData, TResponse extends BaseRecord = TData, TResponseError extends HttpError = TError> = {
14
15
  /**
@@ -29,5 +30,7 @@ export type UseFormProps<TQueryFnData extends BaseRecord = BaseRecord, TError ex
29
30
  disableServerSideValidation?: boolean;
30
31
  transformApplyValues?: (values: TVariables) => TVariables;
31
32
  transformInitValues?: (values: Record<string, unknown>) => DefaultValues<TVariables>;
33
+ beforeSubmit?: (values: TVariables, setErrors: (errors: string[]) => void) => Promise<TVariables>;
34
+ onBeforeSubmitError?: (errors: string[]) => void;
32
35
  } & UseHookFormProps<TVariables, TContext>;
33
- export declare const useForm: <TQueryFnData extends BaseRecord = BaseRecord, TError extends HttpError = HttpError, TVariables extends FieldValues = FieldValues, TContext extends object = object, TData extends BaseRecord = TQueryFnData, TResponse extends BaseRecord = TData, TResponseError extends HttpError = TError>({ refineCoreProps, warnWhenUnsavedChanges: warnWhenUnsavedChangesProp, disableServerSideValidation: disableServerSideValidationProp, transformApplyValues, transformInitValues, ...rest }?: UseFormProps<TQueryFnData, TError, TVariables, TContext, TData, TResponse, TResponseError>) => UseFormReturnType<TQueryFnData, TError, TVariables, TContext, TData, TResponse, TResponseError>;
36
+ export declare const useForm: <TQueryFnData extends BaseRecord = BaseRecord, TError extends HttpError = HttpError, TVariables extends FieldValues = FieldValues, TContext extends object = object, TData extends BaseRecord = TQueryFnData, TResponse extends BaseRecord = TData, TResponseError extends HttpError = TError>({ refineCoreProps, warnWhenUnsavedChanges: warnWhenUnsavedChangesProp, disableServerSideValidation: disableServerSideValidationProp, transformApplyValues, transformInitValues, beforeSubmit, onBeforeSubmitError, ...rest }?: UseFormProps<TQueryFnData, TError, TVariables, TContext, TData, TResponse, TResponseError>) => UseFormReturnType<TQueryFnData, TError, TVariables, TContext, TData, TResponse, TResponseError>;
@@ -2,6 +2,7 @@ import { CommonFormConfig, RefineFormConfig, ResourceConfig } from '../../types'
2
2
  import { UseFormProps } from './useReactHookForm';
3
3
  interface UseRefineFormOptions {
4
4
  initialValues?: Record<string, unknown>;
5
+ onBeforeSubmitError?: (errors: string[]) => void;
5
6
  }
6
7
  export declare const useRefineForm: (props: {
7
8
  formConfig?: RefineFormConfig & CommonFormConfig;
@@ -14,5 +15,6 @@ export declare const useRefineForm: (props: {
14
15
  [x: string]: any;
15
16
  }, {}, import("@refinedev/core").BaseRecord, import("@refinedev/core").BaseRecord, import("@refinedev/core").HttpError>;
16
17
  responseErrorMsgs: string[];
18
+ beforeSubmitErrors: string[];
17
19
  };
18
20
  export {};
@@ -27,6 +27,8 @@ export type UseFormProps<TQueryFnData extends BaseRecord = BaseRecord, TError ex
27
27
  initialValuesForEdit?: Record<string, unknown>;
28
28
  transformInitValues?: (values: Record<string, unknown>) => Record<string, unknown>;
29
29
  transformApplyValues?: (values: Unstructured) => Unstructured;
30
+ beforeSubmit?: (values: Unstructured, setErrors: (errors: string[]) => void) => Promise<Unstructured>;
31
+ onBeforeSubmitError?: (errors: string[]) => void;
30
32
  rules?: YamlFormRule[];
31
33
  };
32
34
  export type UseFormReturnType<TQueryFnData extends BaseRecord = BaseRecord, TError extends HttpError = HttpError, TVariables extends object = object, TData extends BaseRecord = TQueryFnData, TResponse extends BaseRecord = TData, TResponseError extends HttpError = TError> = UseFormReturnTypeCore<TQueryFnData, TError, TVariables, TData, TResponse, TResponseError> & {
@@ -43,6 +45,7 @@ export type UseFormReturnType<TQueryFnData extends BaseRecord = BaseRecord, TErr
43
45
  loadSchemaError: Error | null;
44
46
  fetchSchema: () => void;
45
47
  errorResponseBody?: Record<string, unknown> | null;
48
+ beforeSubmitErrors: string[];
46
49
  };
47
50
  declare const useYamlForm: <TQueryFnData extends Unstructured = Unstructured & {
48
51
  id: string;
@@ -50,5 +53,5 @@ declare const useYamlForm: <TQueryFnData extends Unstructured = Unstructured & {
50
53
  [prop: string]: unknown;
51
54
  } = {
52
55
  [prop: string]: unknown;
53
- }, TData extends Unstructured = TQueryFnData, TResponse extends BaseRecord = TData, TResponseError extends HttpError = TError>({ action: actionFromProps, resource, onMutationSuccess: onMutationSuccessProp, onMutationError, submitOnEnter, warnWhenUnsavedChanges: warnWhenUnsavedChangesProp, redirect, successNotification, errorNotification, meta, metaData, queryMeta, mutationMeta, liveMode, liveParams, mutationMode, dataProviderName, onLiveEvent, invalidates, undoableTimeout, queryOptions, createMutationOptions, updateMutationOptions, id: idFromProps, overtimeOptions, editorOptions, initialValuesForCreate, initialValuesForEdit, transformInitValues, transformApplyValues, rules, }?: UseFormProps<TQueryFnData, TError, TVariables, TData, TResponse, TResponseError>) => UseFormReturnType<TQueryFnData, TError, TVariables, TData, TResponse, TResponseError>;
56
+ }, TData extends Unstructured = TQueryFnData, TResponse extends BaseRecord = TData, TResponseError extends HttpError = TError>({ action: actionFromProps, resource, onMutationSuccess: onMutationSuccessProp, onMutationError, submitOnEnter, warnWhenUnsavedChanges: warnWhenUnsavedChangesProp, redirect, successNotification, errorNotification, meta, metaData, queryMeta, mutationMeta, liveMode, liveParams, mutationMode, dataProviderName, onLiveEvent, invalidates, undoableTimeout, queryOptions, createMutationOptions, updateMutationOptions, id: idFromProps, overtimeOptions, editorOptions, initialValuesForCreate, initialValuesForEdit, transformInitValues, transformApplyValues, beforeSubmit, onBeforeSubmitError, rules, }?: UseFormProps<TQueryFnData, TError, TVariables, TData, TResponse, TResponseError>) => UseFormReturnType<TQueryFnData, TError, TVariables, TData, TResponse, TResponseError>;
54
57
  export default useYamlForm;
package/dist/refine.cjs CHANGED
@@ -15490,6 +15490,8 @@ const useForm = ({
15490
15490
  disableServerSideValidation: disableServerSideValidationProp = false,
15491
15491
  transformApplyValues,
15492
15492
  transformInitValues,
15493
+ beforeSubmit,
15494
+ onBeforeSubmitError,
15493
15495
  ...rest
15494
15496
  } = {}) => {
15495
15497
  const { options } = core.useRefineContext();
@@ -15502,6 +15504,8 @@ const useForm = ({
15502
15504
  defaultValues: transformInitValues && typeof rest.defaultValues === "object" ? transformInitValues(rest.defaultValues) : rest.defaultValues
15503
15505
  });
15504
15506
  const [transformedInitValues, setTransformedInitValues] = React.useState(useHookFormResult.getValues());
15507
+ const [beforeSubmitErrors, setBeforeSubmitErrors] = React.useState([]);
15508
+ const [isBeforeSubmitLoading, setIsBeforeSubmitLoading] = React.useState(false);
15505
15509
  const {
15506
15510
  watch,
15507
15511
  setValue,
@@ -15605,20 +15609,55 @@ const useForm = ({
15605
15609
  const saveButtonProps = React.useMemo(() => {
15606
15610
  return {
15607
15611
  disabled: formLoading,
15608
- onClick: (e2) => {
15612
+ loading: formLoading || isBeforeSubmitLoading,
15613
+ onClick: async (e2) => {
15614
+ setBeforeSubmitErrors([]);
15609
15615
  handleSubmit(
15610
- (v) => onFinish(transformApplyValues ? transformApplyValues(v) : v),
15616
+ async (v) => {
15617
+ let finalValues = transformApplyValues ? transformApplyValues(v) : v;
15618
+ if (beforeSubmit) {
15619
+ try {
15620
+ setIsBeforeSubmitLoading(true);
15621
+ let hasErrors = false;
15622
+ const result = await beforeSubmit(finalValues, (errors) => {
15623
+ if (errors && errors.length > 0) {
15624
+ setBeforeSubmitErrors(errors);
15625
+ onBeforeSubmitError == null ? void 0 : onBeforeSubmitError(errors);
15626
+ hasErrors = true;
15627
+ }
15628
+ });
15629
+ if (hasErrors) {
15630
+ return;
15631
+ }
15632
+ if (result !== void 0) {
15633
+ finalValues = result;
15634
+ }
15635
+ } finally {
15636
+ setIsBeforeSubmitLoading(false);
15637
+ }
15638
+ }
15639
+ return onFinish(finalValues);
15640
+ },
15611
15641
  () => false
15612
15642
  )(e2);
15613
15643
  }
15614
15644
  };
15615
- }, [formLoading, handleSubmit, onFinish, transformApplyValues]);
15645
+ }, [
15646
+ formLoading,
15647
+ isBeforeSubmitLoading,
15648
+ handleSubmit,
15649
+ onFinish,
15650
+ transformApplyValues,
15651
+ beforeSubmit,
15652
+ onBeforeSubmitError
15653
+ ]);
15616
15654
  return {
15617
15655
  ...useHookFormResult,
15618
15656
  transformedInitValues,
15619
15657
  handleSubmit,
15620
15658
  refineCore: useFormCoreResult,
15621
- saveButtonProps
15659
+ saveButtonProps,
15660
+ beforeSubmitErrors
15622
15661
  };
15623
15662
  };
15624
15663
  const useRefineForm = (props) => {
@@ -15660,6 +15699,8 @@ const useRefineForm = (props) => {
15660
15699
  defaultValues: (options == null ? void 0 : options.initialValues) || (config == null ? void 0 : config.initValue),
15661
15700
  transformApplyValues,
15662
15701
  transformInitValues,
15702
+ beforeSubmit: formConfig == null ? void 0 : formConfig.beforeSubmit,
15703
+ onBeforeSubmitError: options == null ? void 0 : options.onBeforeSubmitError,
15663
15704
  ...formConfig == null ? void 0 : formConfig.useFormProps
15664
15705
  });
15665
15706
  React.useEffect(() => {
@@ -15672,7 +15713,7 @@ const useRefineForm = (props) => {
15672
15713
  });
15673
15714
  }
15674
15715
  }, [formConfig, result, i18n2]);
15675
- return { formResult: result, responseErrorMsgs };
15716
+ return { formResult: result, responseErrorMsgs, beforeSubmitErrors: result.beforeSubmitErrors };
15676
15717
  };
15677
15718
  const index_2ivb33 = "";
15678
15719
  const Style = "s1bsn3us";
@@ -16091,6 +16132,8 @@ const useYamlForm = ({
16091
16132
  initialValuesForEdit,
16092
16133
  transformInitValues,
16093
16134
  transformApplyValues,
16135
+ beforeSubmit,
16136
+ onBeforeSubmitError,
16094
16137
  rules
16095
16138
  } = {}) => {
16096
16139
  var _a;
@@ -16100,6 +16143,8 @@ const useYamlForm = ({
16100
16143
  const [isSchemaValid, setIsSchemaValid] = React.useState(true);
16101
16144
  const [editorErrors, setEditorErrors] = React.useState([]);
16102
16145
  const [rulesErrors, setRulesErrors] = React.useState([]);
16146
+ const [isBeforeSubmitLoading, setIsBeforeSubmitLoading] = React.useState(false);
16147
+ const [beforeSubmitErrors, setBeforeSubmitErrors] = React.useState([]);
16103
16148
  const [errorResponseBody, setErrorResponseBody] = React.useState(null);
16104
16149
  const useResourceResult = core.useResource();
16105
16150
  const globalStore = useGlobalStore();
@@ -16175,12 +16220,12 @@ const useYamlForm = ({
16175
16220
  }, [schema]);
16176
16221
  const saveButtonProps = React.useMemo(
16177
16222
  () => ({
16178
- loading: formLoading,
16223
+ loading: formLoading || isBeforeSubmitLoading,
16179
16224
  onClick: () => {
16180
16225
  form2.submit();
16181
16226
  }
16182
16227
  }),
16183
- [formLoading, form2]
16228
+ [formLoading, form2, isBeforeSubmitLoading]
16184
16229
  );
16185
16230
  const emptySchemas = React.useMemo(() => {
16186
16231
  return [];
@@ -16257,6 +16302,7 @@ const useYamlForm = ({
16257
16302
  ...formSF.formProps,
16258
16303
  onFinish: async (values) => {
16259
16304
  var _a2, _b;
16305
+ setBeforeSubmitErrors([]);
16260
16306
  const errors = [
16261
16307
  !isYamlValid ? t2("dovetail.yaml_format_wrong") : "",
16262
16308
  !isSchemaValid ? t2("dovetail.yaml_value_wrong") : ""
@@ -16273,7 +16319,28 @@ const useYamlForm = ({
16273
16319
  }
16274
16320
  try {
16275
16321
  const objectValues = editor.current ? yaml$2.load(((_b = editor.current) == null ? void 0 : _b.getEditorValue()) || "") : values;
16276
- const finalValues = (transformApplyValues == null ? void 0 : transformApplyValues(objectValues)) || objectValues;
16322
+ let finalValues = (transformApplyValues == null ? void 0 : transformApplyValues(objectValues)) || objectValues;
16323
+ if (beforeSubmit) {
16324
+ try {
16325
+ let hasErrors = false;
16326
+ setIsBeforeSubmitLoading(true);
16327
+ const result = await beforeSubmit(finalValues, (errors2) => {
16328
+ if (errors2 && errors2.length > 0) {
16329
+ setBeforeSubmitErrors(errors2);
16330
+ onBeforeSubmitError == null ? void 0 : onBeforeSubmitError(errors2);
16331
+ hasErrors = true;
16332
+ }
16333
+ });
16334
+ if (hasErrors) {
16335
+ return;
16336
+ }
16337
+ if (result !== void 0) {
16338
+ finalValues = result;
16339
+ }
16340
+ } finally {
16341
+ setIsBeforeSubmitLoading(false);
16342
+ }
16343
+ }
16277
16344
  return onFinish(finalValues);
16278
16345
  } catch (error) {
16279
16346
  if (error instanceof Error) {
@@ -16293,6 +16360,7 @@ const useYamlForm = ({
16293
16360
  saveButtonProps,
16294
16361
  ...useFormCoreResult,
16295
16362
  errorResponseBody,
16363
+ beforeSubmitErrors,
16296
16364
  editorProps,
16297
16365
  schemas: schema ? [schema] : [],
16298
16366
  isLoadingSchema,
@@ -16320,6 +16388,7 @@ function YamlForm(props) {
16320
16388
  config,
16321
16389
  transformInitValues,
16322
16390
  transformApplyValues,
16391
+ beforeSubmit,
16323
16392
  onSaveButtonPropsChange,
16324
16393
  onErrorsChange,
16325
16394
  rules
@@ -16340,6 +16409,7 @@ function YamlForm(props) {
16340
16409
  saveButtonProps,
16341
16410
  editorProps,
16342
16411
  errorResponseBody,
16412
+ beforeSubmitErrors,
16343
16413
  mutationResult,
16344
16414
  isLoadingSchema,
16345
16415
  queryResult,
@@ -16356,6 +16426,7 @@ function YamlForm(props) {
16356
16426
  initialValuesForCreate: props.initialValuesForCreate ?? BASE_INIT_VALUE,
16357
16427
  initialValuesForEdit: props.initialValuesForEdit,
16358
16428
  rules,
16429
+ beforeSubmit,
16359
16430
  successNotification(data2) {
16360
16431
  var _a;
16361
16432
  const displayName = config.displayName || ((_a = resource == null ? void 0 : resource.meta) == null ? void 0 : _a.kind);
@@ -16383,6 +16454,18 @@ function YamlForm(props) {
16383
16454
  saveButtonProps
16384
16455
  } : {};
16385
16456
  const responseErrors = React.useMemo(() => errorResponseBody ? getCommonErrors(errorResponseBody, i18n2) : [], [errorResponseBody, i18n2]);
16457
+ const finalErrors = React.useMemo(() => {
16458
+ if (beforeSubmitErrors.length) {
16459
+ return beforeSubmitErrors;
16460
+ }
16461
+ if (mutationResult.error) {
16462
+ if (responseErrors.length) {
16463
+ return responseErrors;
16464
+ }
16465
+ return [mutationResult.error.message];
16466
+ }
16467
+ return [];
16468
+ }, [responseErrors, beforeSubmitErrors, mutationResult.error]);
16386
16469
  const onFinish = React.useCallback(async (store) => {
16387
16470
  var _a, _b;
16388
16471
  try {
@@ -16402,8 +16485,8 @@ function YamlForm(props) {
16402
16485
  onSaveButtonPropsChange == null ? void 0 : onSaveButtonPropsChange(saveButtonProps);
16403
16486
  }, [saveButtonProps, onSaveButtonPropsChange]);
16404
16487
  React.useEffect(() => {
16405
- onErrorsChange == null ? void 0 : onErrorsChange(responseErrors);
16406
- }, [responseErrors, onErrorsChange]);
16488
+ onErrorsChange == null ? void 0 : onErrorsChange(finalErrors);
16489
+ }, [finalErrors, onErrorsChange]);
16407
16490
  return /* @__PURE__ */ common.jsxRuntimeExports.jsx(FormWrapper, {
16408
16491
  ...formWrapperProps,
16409
16492
  children: /* @__PURE__ */ common.jsxRuntimeExports.jsx(eagle.Form, {
@@ -16427,8 +16510,8 @@ function YamlForm(props) {
16427
16510
  collapsable: false
16428
16511
  })
16429
16512
  }), /* @__PURE__ */ common.jsxRuntimeExports.jsx(eagle.Form.Item, {
16430
- children: mutationResult.error && /* @__PURE__ */ common.jsxRuntimeExports.jsx(FormErrorAlert, {
16431
- errorMsgs: errorResponseBody ? responseErrors : [mutationResult.error.message],
16513
+ children: finalErrors.length > 0 && /* @__PURE__ */ common.jsxRuntimeExports.jsx(FormErrorAlert, {
16514
+ errorMsgs: finalErrors,
16432
16515
  style: {
16433
16516
  marginBottom: 16
16434
16517
  },
@@ -16474,7 +16557,14 @@ const RefineFormContainer = React.forwardRef(function RefineFormContainer2({
16474
16557
  ...formConfig == null ? void 0 : formConfig.refineCoreProps
16475
16558
  },
16476
16559
  formConfig,
16477
- options
16560
+ options: {
16561
+ ...options,
16562
+ onBeforeSubmitError: (errors) => {
16563
+ if (errors.length) {
16564
+ onError == null ? void 0 : onError();
16565
+ }
16566
+ }
16567
+ }
16478
16568
  });
16479
16569
  const fieldsConfig = useFieldsConfig(
16480
16570
  config,
@@ -16516,6 +16606,7 @@ const RefineFormContainer = React.forwardRef(function RefineFormContainer2({
16516
16606
  validators: "validators" in config2 ? config2.validators : void 0
16517
16607
  })),
16518
16608
  onSaveButtonPropsChange,
16609
+ beforeSubmit: formConfig == null ? void 0 : formConfig.beforeSubmit,
16519
16610
  onErrorsChange(errors) {
16520
16611
  if (errors.length) {
16521
16612
  onError == null ? void 0 : onError();
@@ -16535,6 +16626,7 @@ const RefineFormContainer = React.forwardRef(function RefineFormContainer2({
16535
16626
  config,
16536
16627
  id,
16537
16628
  refineFormResult,
16629
+ formConfig == null ? void 0 : formConfig.beforeSubmit,
16538
16630
  transformApplyValues,
16539
16631
  onSaveButtonPropsChange,
16540
16632
  onSuccess,
@@ -16576,7 +16668,10 @@ const RefineFormContainer = React.forwardRef(function RefineFormContainer2({
16576
16668
  config,
16577
16669
  step,
16578
16670
  formConfig,
16579
- errorMsgs: refineFormResult.responseErrorMsgs,
16671
+ errorMsgs: [
16672
+ ...refineFormResult.responseErrorMsgs,
16673
+ ...refineFormResult.beforeSubmitErrors
16674
+ ],
16580
16675
  resourceId: id,
16581
16676
  transformedInitValues: refineFormResult.formResult.transformedInitValues,
16582
16677
  customOptions: options == null ? void 0 : options.customOptions
@@ -16606,6 +16701,7 @@ function YamlFormContainer({
16606
16701
  config,
16607
16702
  transformInitValues,
16608
16703
  transformApplyValues,
16704
+ beforeSubmit: formConfig == null ? void 0 : formConfig.beforeSubmit,
16609
16705
  initialValuesForCreate: (customYamlFormProps == null ? void 0 : customYamlFormProps.initialValuesForCreate) || getInitialValues(config),
16610
16706
  initialValuesForEdit: void 0,
16611
16707
  id,
@@ -16628,6 +16724,7 @@ function YamlFormContainer({
16628
16724
  action,
16629
16725
  customYamlFormProps,
16630
16726
  config,
16727
+ formConfig == null ? void 0 : formConfig.beforeSubmit,
16631
16728
  transformInitValues,
16632
16729
  transformApplyValues,
16633
16730
  onSuccess,
package/dist/refine.js CHANGED
@@ -15471,6 +15471,8 @@ const useForm = ({
15471
15471
  disableServerSideValidation: disableServerSideValidationProp = false,
15472
15472
  transformApplyValues,
15473
15473
  transformInitValues,
15474
+ beforeSubmit,
15475
+ onBeforeSubmitError,
15474
15476
  ...rest
15475
15477
  } = {}) => {
15476
15478
  const { options } = useRefineContext();
@@ -15483,6 +15485,8 @@ const useForm = ({
15483
15485
  defaultValues: transformInitValues && typeof rest.defaultValues === "object" ? transformInitValues(rest.defaultValues) : rest.defaultValues
15484
15486
  });
15485
15487
  const [transformedInitValues, setTransformedInitValues] = useState(useHookFormResult.getValues());
15488
+ const [beforeSubmitErrors, setBeforeSubmitErrors] = useState([]);
15489
+ const [isBeforeSubmitLoading, setIsBeforeSubmitLoading] = useState(false);
15486
15490
  const {
15487
15491
  watch,
15488
15492
  setValue,
@@ -15586,20 +15590,55 @@ const useForm = ({
15586
15590
  const saveButtonProps = useMemo(() => {
15587
15591
  return {
15588
15592
  disabled: formLoading,
15589
- onClick: (e2) => {
15593
+ loading: formLoading || isBeforeSubmitLoading,
15594
+ onClick: async (e2) => {
15595
+ setBeforeSubmitErrors([]);
15590
15596
  handleSubmit(
15591
- (v) => onFinish(transformApplyValues ? transformApplyValues(v) : v),
15597
+ async (v) => {
15598
+ let finalValues = transformApplyValues ? transformApplyValues(v) : v;
15599
+ if (beforeSubmit) {
15600
+ try {
15601
+ setIsBeforeSubmitLoading(true);
15602
+ let hasErrors = false;
15603
+ const result = await beforeSubmit(finalValues, (errors) => {
15604
+ if (errors && errors.length > 0) {
15605
+ setBeforeSubmitErrors(errors);
15606
+ onBeforeSubmitError == null ? void 0 : onBeforeSubmitError(errors);
15607
+ hasErrors = true;
15608
+ }
15609
+ });
15610
+ if (hasErrors) {
15611
+ return;
15612
+ }
15613
+ if (result !== void 0) {
15614
+ finalValues = result;
15615
+ }
15616
+ } finally {
15617
+ setIsBeforeSubmitLoading(false);
15618
+ }
15619
+ }
15620
+ return onFinish(finalValues);
15621
+ },
15592
15622
  () => false
15593
15623
  )(e2);
15594
15624
  }
15595
15625
  };
15596
- }, [formLoading, handleSubmit, onFinish, transformApplyValues]);
15626
+ }, [
15627
+ formLoading,
15628
+ isBeforeSubmitLoading,
15629
+ handleSubmit,
15630
+ onFinish,
15631
+ transformApplyValues,
15632
+ beforeSubmit,
15633
+ onBeforeSubmitError
15634
+ ]);
15597
15635
  return {
15598
15636
  ...useHookFormResult,
15599
15637
  transformedInitValues,
15600
15638
  handleSubmit,
15601
15639
  refineCore: useFormCoreResult,
15602
- saveButtonProps
15640
+ saveButtonProps,
15641
+ beforeSubmitErrors
15603
15642
  };
15604
15643
  };
15605
15644
  const useRefineForm = (props) => {
@@ -15641,6 +15680,8 @@ const useRefineForm = (props) => {
15641
15680
  defaultValues: (options == null ? void 0 : options.initialValues) || (config == null ? void 0 : config.initValue),
15642
15681
  transformApplyValues,
15643
15682
  transformInitValues,
15683
+ beforeSubmit: formConfig == null ? void 0 : formConfig.beforeSubmit,
15684
+ onBeforeSubmitError: options == null ? void 0 : options.onBeforeSubmitError,
15644
15685
  ...formConfig == null ? void 0 : formConfig.useFormProps
15645
15686
  });
15646
15687
  useEffect(() => {
@@ -15653,7 +15694,7 @@ const useRefineForm = (props) => {
15653
15694
  });
15654
15695
  }
15655
15696
  }, [formConfig, result, i18n2]);
15656
- return { formResult: result, responseErrorMsgs };
15697
+ return { formResult: result, responseErrorMsgs, beforeSubmitErrors: result.beforeSubmitErrors };
15657
15698
  };
15658
15699
  const index_2ivb33 = "";
15659
15700
  const Style = "s1bsn3us";
@@ -16072,6 +16113,8 @@ const useYamlForm = ({
16072
16113
  initialValuesForEdit,
16073
16114
  transformInitValues,
16074
16115
  transformApplyValues,
16116
+ beforeSubmit,
16117
+ onBeforeSubmitError,
16075
16118
  rules
16076
16119
  } = {}) => {
16077
16120
  var _a;
@@ -16081,6 +16124,8 @@ const useYamlForm = ({
16081
16124
  const [isSchemaValid, setIsSchemaValid] = useState(true);
16082
16125
  const [editorErrors, setEditorErrors] = useState([]);
16083
16126
  const [rulesErrors, setRulesErrors] = useState([]);
16127
+ const [isBeforeSubmitLoading, setIsBeforeSubmitLoading] = useState(false);
16128
+ const [beforeSubmitErrors, setBeforeSubmitErrors] = useState([]);
16084
16129
  const [errorResponseBody, setErrorResponseBody] = useState(null);
16085
16130
  const useResourceResult = useResource();
16086
16131
  const globalStore = useGlobalStore();
@@ -16156,12 +16201,12 @@ const useYamlForm = ({
16156
16201
  }, [schema]);
16157
16202
  const saveButtonProps = useMemo(
16158
16203
  () => ({
16159
- loading: formLoading,
16204
+ loading: formLoading || isBeforeSubmitLoading,
16160
16205
  onClick: () => {
16161
16206
  form2.submit();
16162
16207
  }
16163
16208
  }),
16164
- [formLoading, form2]
16209
+ [formLoading, form2, isBeforeSubmitLoading]
16165
16210
  );
16166
16211
  const emptySchemas = useMemo(() => {
16167
16212
  return [];
@@ -16238,6 +16283,7 @@ const useYamlForm = ({
16238
16283
  ...formSF.formProps,
16239
16284
  onFinish: async (values) => {
16240
16285
  var _a2, _b;
16286
+ setBeforeSubmitErrors([]);
16241
16287
  const errors = [
16242
16288
  !isYamlValid ? t2("dovetail.yaml_format_wrong") : "",
16243
16289
  !isSchemaValid ? t2("dovetail.yaml_value_wrong") : ""
@@ -16254,7 +16300,28 @@ const useYamlForm = ({
16254
16300
  }
16255
16301
  try {
16256
16302
  const objectValues = editor.current ? yaml$2.load(((_b = editor.current) == null ? void 0 : _b.getEditorValue()) || "") : values;
16257
- const finalValues = (transformApplyValues == null ? void 0 : transformApplyValues(objectValues)) || objectValues;
16303
+ let finalValues = (transformApplyValues == null ? void 0 : transformApplyValues(objectValues)) || objectValues;
16304
+ if (beforeSubmit) {
16305
+ try {
16306
+ let hasErrors = false;
16307
+ setIsBeforeSubmitLoading(true);
16308
+ const result = await beforeSubmit(finalValues, (errors2) => {
16309
+ if (errors2 && errors2.length > 0) {
16310
+ setBeforeSubmitErrors(errors2);
16311
+ onBeforeSubmitError == null ? void 0 : onBeforeSubmitError(errors2);
16312
+ hasErrors = true;
16313
+ }
16314
+ });
16315
+ if (hasErrors) {
16316
+ return;
16317
+ }
16318
+ if (result !== void 0) {
16319
+ finalValues = result;
16320
+ }
16321
+ } finally {
16322
+ setIsBeforeSubmitLoading(false);
16323
+ }
16324
+ }
16258
16325
  return onFinish(finalValues);
16259
16326
  } catch (error) {
16260
16327
  if (error instanceof Error) {
@@ -16274,6 +16341,7 @@ const useYamlForm = ({
16274
16341
  saveButtonProps,
16275
16342
  ...useFormCoreResult,
16276
16343
  errorResponseBody,
16344
+ beforeSubmitErrors,
16277
16345
  editorProps,
16278
16346
  schemas: schema ? [schema] : [],
16279
16347
  isLoadingSchema,
@@ -16301,6 +16369,7 @@ function YamlForm(props) {
16301
16369
  config,
16302
16370
  transformInitValues,
16303
16371
  transformApplyValues,
16372
+ beforeSubmit,
16304
16373
  onSaveButtonPropsChange,
16305
16374
  onErrorsChange,
16306
16375
  rules
@@ -16321,6 +16390,7 @@ function YamlForm(props) {
16321
16390
  saveButtonProps,
16322
16391
  editorProps,
16323
16392
  errorResponseBody,
16393
+ beforeSubmitErrors,
16324
16394
  mutationResult,
16325
16395
  isLoadingSchema,
16326
16396
  queryResult,
@@ -16337,6 +16407,7 @@ function YamlForm(props) {
16337
16407
  initialValuesForCreate: props.initialValuesForCreate ?? BASE_INIT_VALUE,
16338
16408
  initialValuesForEdit: props.initialValuesForEdit,
16339
16409
  rules,
16410
+ beforeSubmit,
16340
16411
  successNotification(data2) {
16341
16412
  var _a;
16342
16413
  const displayName = config.displayName || ((_a = resource == null ? void 0 : resource.meta) == null ? void 0 : _a.kind);
@@ -16364,6 +16435,18 @@ function YamlForm(props) {
16364
16435
  saveButtonProps
16365
16436
  } : {};
16366
16437
  const responseErrors = useMemo(() => errorResponseBody ? getCommonErrors(errorResponseBody, i18n2) : [], [errorResponseBody, i18n2]);
16438
+ const finalErrors = useMemo(() => {
16439
+ if (beforeSubmitErrors.length) {
16440
+ return beforeSubmitErrors;
16441
+ }
16442
+ if (mutationResult.error) {
16443
+ if (responseErrors.length) {
16444
+ return responseErrors;
16445
+ }
16446
+ return [mutationResult.error.message];
16447
+ }
16448
+ return [];
16449
+ }, [responseErrors, beforeSubmitErrors, mutationResult.error]);
16367
16450
  const onFinish = useCallback(async (store) => {
16368
16451
  var _a, _b;
16369
16452
  try {
@@ -16383,8 +16466,8 @@ function YamlForm(props) {
16383
16466
  onSaveButtonPropsChange == null ? void 0 : onSaveButtonPropsChange(saveButtonProps);
16384
16467
  }, [saveButtonProps, onSaveButtonPropsChange]);
16385
16468
  useEffect(() => {
16386
- onErrorsChange == null ? void 0 : onErrorsChange(responseErrors);
16387
- }, [responseErrors, onErrorsChange]);
16469
+ onErrorsChange == null ? void 0 : onErrorsChange(finalErrors);
16470
+ }, [finalErrors, onErrorsChange]);
16388
16471
  return /* @__PURE__ */ jsxRuntimeExports.jsx(FormWrapper, {
16389
16472
  ...formWrapperProps,
16390
16473
  children: /* @__PURE__ */ jsxRuntimeExports.jsx(Form, {
@@ -16408,8 +16491,8 @@ function YamlForm(props) {
16408
16491
  collapsable: false
16409
16492
  })
16410
16493
  }), /* @__PURE__ */ jsxRuntimeExports.jsx(Form.Item, {
16411
- children: mutationResult.error && /* @__PURE__ */ jsxRuntimeExports.jsx(FormErrorAlert, {
16412
- errorMsgs: errorResponseBody ? responseErrors : [mutationResult.error.message],
16494
+ children: finalErrors.length > 0 && /* @__PURE__ */ jsxRuntimeExports.jsx(FormErrorAlert, {
16495
+ errorMsgs: finalErrors,
16413
16496
  style: {
16414
16497
  marginBottom: 16
16415
16498
  },
@@ -16455,7 +16538,14 @@ const RefineFormContainer = React.forwardRef(function RefineFormContainer2({
16455
16538
  ...formConfig == null ? void 0 : formConfig.refineCoreProps
16456
16539
  },
16457
16540
  formConfig,
16458
- options
16541
+ options: {
16542
+ ...options,
16543
+ onBeforeSubmitError: (errors) => {
16544
+ if (errors.length) {
16545
+ onError == null ? void 0 : onError();
16546
+ }
16547
+ }
16548
+ }
16459
16549
  });
16460
16550
  const fieldsConfig = useFieldsConfig(
16461
16551
  config,
@@ -16497,6 +16587,7 @@ const RefineFormContainer = React.forwardRef(function RefineFormContainer2({
16497
16587
  validators: "validators" in config2 ? config2.validators : void 0
16498
16588
  })),
16499
16589
  onSaveButtonPropsChange,
16590
+ beforeSubmit: formConfig == null ? void 0 : formConfig.beforeSubmit,
16500
16591
  onErrorsChange(errors) {
16501
16592
  if (errors.length) {
16502
16593
  onError == null ? void 0 : onError();
@@ -16516,6 +16607,7 @@ const RefineFormContainer = React.forwardRef(function RefineFormContainer2({
16516
16607
  config,
16517
16608
  id,
16518
16609
  refineFormResult,
16610
+ formConfig == null ? void 0 : formConfig.beforeSubmit,
16519
16611
  transformApplyValues,
16520
16612
  onSaveButtonPropsChange,
16521
16613
  onSuccess,
@@ -16557,7 +16649,10 @@ const RefineFormContainer = React.forwardRef(function RefineFormContainer2({
16557
16649
  config,
16558
16650
  step,
16559
16651
  formConfig,
16560
- errorMsgs: refineFormResult.responseErrorMsgs,
16652
+ errorMsgs: [
16653
+ ...refineFormResult.responseErrorMsgs,
16654
+ ...refineFormResult.beforeSubmitErrors
16655
+ ],
16561
16656
  resourceId: id,
16562
16657
  transformedInitValues: refineFormResult.formResult.transformedInitValues,
16563
16658
  customOptions: options == null ? void 0 : options.customOptions
@@ -16587,6 +16682,7 @@ function YamlFormContainer({
16587
16682
  config,
16588
16683
  transformInitValues,
16589
16684
  transformApplyValues,
16685
+ beforeSubmit: formConfig == null ? void 0 : formConfig.beforeSubmit,
16590
16686
  initialValuesForCreate: (customYamlFormProps == null ? void 0 : customYamlFormProps.initialValuesForCreate) || getInitialValues(config),
16591
16687
  initialValuesForEdit: void 0,
16592
16688
  id,
@@ -16609,6 +16705,7 @@ function YamlFormContainer({
16609
16705
  action,
16610
16706
  customYamlFormProps,
16611
16707
  config,
16708
+ formConfig == null ? void 0 : formConfig.beforeSubmit,
16612
16709
  transformInitValues,
16613
16710
  transformApplyValues,
16614
16711
  onSuccess,
@@ -111,6 +111,13 @@ export type CommonFormConfig<Model extends ResourceModel = ResourceModel> = {
111
111
  * @returns YAML格式的数据
112
112
  */
113
113
  transformApplyValues?: (values: Record<string, unknown>) => Model['_rawYaml'];
114
+ /**
115
+ * 提交前回调函数
116
+ * @param values 表单值
117
+ * @param setErrors 设置错误信息的函数,如果调用此函数并传入错误信息,将阻止提交
118
+ * @returns 可选的处理后的值,如果返回值存在,将使用返回值替代原始值进行提交
119
+ */
120
+ beforeSubmit?: (values: Record<string, unknown>, setErrors: (errors: string[]) => void) => Promise<Model['_rawYaml']>;
114
121
  /** 表单容器类型:页面形式或模态框形式
115
122
  * PAGE 或者 MODAL
116
123
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dovetail-v2/refine",
3
- "version": "0.3.14",
3
+ "version": "0.3.15-alpha.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",