@faasjs/ant-design 8.0.0-beta.16 → 8.0.0-beta.17
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/README.md +2 -1
- package/dist/index.cjs +138 -60
- package/dist/index.d.ts +83 -41
- package/dist/index.mjs +138 -60
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -47,7 +47,6 @@
|
|
|
47
47
|
- [FaasDataWrapperProps](interfaces/FaasDataWrapperProps.md)
|
|
48
48
|
- [FaasItemProps](interfaces/FaasItemProps.md)
|
|
49
49
|
- [FormItemProps](interfaces/FormItemProps.md)
|
|
50
|
-
- [FormProps](interfaces/FormProps.md)
|
|
51
50
|
- [LinkProps](interfaces/LinkProps.md)
|
|
52
51
|
- [ModalProps](interfaces/ModalProps.md)
|
|
53
52
|
- [RoutesProps](interfaces/RoutesProps.md)
|
|
@@ -71,6 +70,8 @@
|
|
|
71
70
|
- [FaasItemType](type-aliases/FaasItemType.md)
|
|
72
71
|
- [FaasItemTypeValue](type-aliases/FaasItemTypeValue.md)
|
|
73
72
|
- [FaasReactClientOptions](type-aliases/FaasReactClientOptions.md)
|
|
73
|
+
- [FormFaasProps](type-aliases/FormFaasProps.md)
|
|
74
|
+
- [FormProps](type-aliases/FormProps.md)
|
|
74
75
|
- [FormSubmitProps](type-aliases/FormSubmitProps.md)
|
|
75
76
|
- [LoadingProps](type-aliases/LoadingProps.md)
|
|
76
77
|
- [ResolvedTheme](type-aliases/ResolvedTheme.md)
|
package/dist/index.cjs
CHANGED
|
@@ -898,105 +898,183 @@ function isFormItemProps(item) {
|
|
|
898
898
|
* Form component with Ant Design & FaasJS
|
|
899
899
|
*
|
|
900
900
|
* - Based on [Ant Design Form](https://ant.design/components/form/).
|
|
901
|
+
* - Use `onFinish` for custom submit logic.
|
|
902
|
+
* - Use `faas` for the built-in FaasJS submit flow.
|
|
903
|
+
*
|
|
904
|
+
* @example
|
|
905
|
+
* ```tsx
|
|
906
|
+
* import { Form } from '@faasjs/ant-design'
|
|
907
|
+
*
|
|
908
|
+
* export function ProfileForm() {
|
|
909
|
+
* return (
|
|
910
|
+
* <Form
|
|
911
|
+
* items={[
|
|
912
|
+
* { id: 'name', required: true },
|
|
913
|
+
* { id: 'email', required: true },
|
|
914
|
+
* ]}
|
|
915
|
+
* onFinish={async (values) => {
|
|
916
|
+
* console.log(values)
|
|
917
|
+
* }}
|
|
918
|
+
* />
|
|
919
|
+
* )
|
|
920
|
+
* }
|
|
921
|
+
* ```
|
|
922
|
+
*
|
|
923
|
+
* @example
|
|
924
|
+
* ```tsx
|
|
925
|
+
* import { Form } from '@faasjs/ant-design'
|
|
926
|
+
*
|
|
927
|
+
* export function CreateUserForm() {
|
|
928
|
+
* return (
|
|
929
|
+
* <Form
|
|
930
|
+
* initialValues={{ role: 'user' }}
|
|
931
|
+
* items={[
|
|
932
|
+
* { id: 'name', required: true },
|
|
933
|
+
* { id: 'role', options: ['user', 'admin'] },
|
|
934
|
+
* ]}
|
|
935
|
+
* faas={{
|
|
936
|
+
* action: 'user/create',
|
|
937
|
+
* params: (values) => ({
|
|
938
|
+
* role: values.role || 'user',
|
|
939
|
+
* }),
|
|
940
|
+
* }}
|
|
941
|
+
* />
|
|
942
|
+
* )
|
|
943
|
+
* }
|
|
944
|
+
* ```
|
|
901
945
|
*/
|
|
902
946
|
function Form(props) {
|
|
903
947
|
const [loading, setLoading] = (0, react.useState)(false);
|
|
904
|
-
const [
|
|
905
|
-
const [submit, setSubmit] = (0, react.useState)();
|
|
948
|
+
const [antdProps, setAntdProps] = (0, react.useState)();
|
|
949
|
+
const [submit, setSubmit] = (0, react.useState)(props.submit === false ? false : {});
|
|
950
|
+
const [items, setItems] = (0, react.useState)([]);
|
|
906
951
|
const config = useConfigContext();
|
|
907
952
|
const [extendTypes, setExtendTypes] = (0, react.useState)();
|
|
908
953
|
const [form] = antd.Form.useForm(props.form);
|
|
909
954
|
const [initialValues, setInitialValues] = (0, react.useState)(props.initialValues || Object.create(null));
|
|
955
|
+
const [onFinish, setOnFinish] = (0, react.useState)();
|
|
910
956
|
(0, _faasjs_react.useEqualEffect)(() => {
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
}
|
|
923
|
-
if (propsCopy.items?.length) {
|
|
924
|
-
for (const item of propsCopy.items) if (isFormItemProps(item) && item.if) item.hidden = !item.if(nextInitialValues || Object.create(null));
|
|
957
|
+
if (props.onFinish) {
|
|
958
|
+
setOnFinish(() => async (values) => {
|
|
959
|
+
if (!props.onFinish) return;
|
|
960
|
+
setLoading(true);
|
|
961
|
+
try {
|
|
962
|
+
return await props.onFinish(values);
|
|
963
|
+
} finally {
|
|
964
|
+
setLoading(false);
|
|
965
|
+
}
|
|
966
|
+
});
|
|
967
|
+
return;
|
|
925
968
|
}
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
propsCopy.onFinish = async (values) => {
|
|
969
|
+
if (props.faas?.action) {
|
|
970
|
+
setOnFinish(() => async (values) => {
|
|
971
|
+
if (!props.faas?.action) return;
|
|
930
972
|
setLoading(true);
|
|
973
|
+
let submitValues = values;
|
|
931
974
|
try {
|
|
932
|
-
if (
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
975
|
+
if (props.faas?.transformValues) submitValues = await props.faas.transformValues(values);
|
|
976
|
+
const extraParams = typeof props.faas?.params === "function" ? props.faas.params(submitValues) : props.faas.params;
|
|
977
|
+
if (extraParams) submitValues = {
|
|
978
|
+
...submitValues,
|
|
979
|
+
...extraParams
|
|
980
|
+
};
|
|
981
|
+
const result = await (0, _faasjs_react.faas)(props.faas.action, submitValues);
|
|
982
|
+
props.faas.onSuccess?.(result, submitValues);
|
|
983
|
+
return result;
|
|
937
984
|
} catch (error) {
|
|
938
|
-
|
|
985
|
+
props.faas.onError?.(error, submitValues);
|
|
986
|
+
throw error;
|
|
987
|
+
} finally {
|
|
988
|
+
props.faas.onFinally?.();
|
|
989
|
+
setLoading(false);
|
|
939
990
|
}
|
|
940
|
-
setLoading(false);
|
|
941
|
-
};
|
|
942
|
-
} else if (submitTo?.action) propsCopy.onFinish = async (values) => {
|
|
943
|
-
setLoading(true);
|
|
944
|
-
return (0, _faasjs_react.faas)(submitTo.action, submitTo.params ? {
|
|
945
|
-
...values,
|
|
946
|
-
...submitTo.params
|
|
947
|
-
} : values).then((result) => {
|
|
948
|
-
submitTo.then?.(result);
|
|
949
|
-
return result;
|
|
950
|
-
}).catch((error) => {
|
|
951
|
-
submitTo.catch?.(error);
|
|
952
|
-
return Promise.reject(error);
|
|
953
|
-
}).finally(() => {
|
|
954
|
-
submitTo.finally?.();
|
|
955
|
-
setLoading(false);
|
|
956
991
|
});
|
|
957
|
-
|
|
958
|
-
if (propsCopy.extendTypes) {
|
|
959
|
-
setExtendTypes(propsCopy.extendTypes);
|
|
960
|
-
delete propsCopy.extendTypes;
|
|
992
|
+
return;
|
|
961
993
|
}
|
|
962
|
-
|
|
963
|
-
}, [
|
|
994
|
+
setOnFinish(void 0);
|
|
995
|
+
}, [props.onFinish, props.faas]);
|
|
996
|
+
(0, _faasjs_react.useEqualEffect)(() => {
|
|
997
|
+
setExtendTypes(props.extendTypes);
|
|
998
|
+
}, [props.extendTypes]);
|
|
999
|
+
(0, _faasjs_react.useEqualEffect)(() => {
|
|
1000
|
+
setSubmit(props.submit === false ? false : props.submit || {});
|
|
1001
|
+
}, [props.submit]);
|
|
1002
|
+
(0, _faasjs_react.useEqualEffect)(() => {
|
|
1003
|
+
const nextInitialValues = props.initialValues ? JSON.parse(JSON.stringify(props.initialValues)) : Object.create(null);
|
|
1004
|
+
for (const key in nextInitialValues) nextInitialValues[key] = transferValue(props.items?.find((item) => isFormItemProps(item) && item.id === key)?.type, nextInitialValues[key]);
|
|
1005
|
+
if (props.items?.length) setItems(props.items.map((item) => {
|
|
1006
|
+
if (!isFormItemProps(item) || !item.if) return item;
|
|
1007
|
+
return {
|
|
1008
|
+
...item,
|
|
1009
|
+
hidden: !item.if(nextInitialValues)
|
|
1010
|
+
};
|
|
1011
|
+
}));
|
|
1012
|
+
else setItems([]);
|
|
1013
|
+
if (props.initialValues) {
|
|
1014
|
+
setInitialValues(nextInitialValues);
|
|
1015
|
+
return;
|
|
1016
|
+
}
|
|
1017
|
+
setInitialValues(null);
|
|
1018
|
+
}, [props.initialValues, props.items]);
|
|
1019
|
+
(0, _faasjs_react.useEqualEffect)(() => {
|
|
1020
|
+
const propsCopy = { ...props };
|
|
1021
|
+
delete propsCopy.onFinish;
|
|
1022
|
+
delete propsCopy.faas;
|
|
1023
|
+
delete propsCopy.extendTypes;
|
|
1024
|
+
delete propsCopy.submit;
|
|
1025
|
+
delete propsCopy.items;
|
|
1026
|
+
delete propsCopy.initialValues;
|
|
1027
|
+
setAntdProps(propsCopy);
|
|
1028
|
+
}, [props]);
|
|
964
1029
|
const onValuesChange = (0, _faasjs_react.useEqualCallback)((changedValues, allValues) => {
|
|
965
1030
|
console.debug("Form:onValuesChange", changedValues, allValues);
|
|
966
1031
|
if (props.onValuesChange) props.onValuesChange(changedValues, allValues);
|
|
967
|
-
if (!
|
|
1032
|
+
if (!items.length) return;
|
|
968
1033
|
for (const key in changedValues) {
|
|
969
|
-
const item =
|
|
1034
|
+
const item = items.find((i) => isFormItemProps(i) && i.id === key);
|
|
970
1035
|
if (item?.onValueChange) item.onValueChange(changedValues[key], allValues, form);
|
|
971
1036
|
}
|
|
972
|
-
}, [
|
|
1037
|
+
}, [
|
|
1038
|
+
items,
|
|
1039
|
+
props.onValuesChange,
|
|
1040
|
+
form
|
|
1041
|
+
]);
|
|
973
1042
|
(0, _faasjs_react.useEqualEffect)(() => {
|
|
974
1043
|
if (!initialValues) return;
|
|
975
1044
|
console.debug("Form:initialValues", initialValues);
|
|
976
1045
|
form.setFieldsValue(initialValues);
|
|
977
1046
|
setInitialValues(null);
|
|
978
|
-
}, [
|
|
979
|
-
|
|
1047
|
+
}, [
|
|
1048
|
+
form,
|
|
1049
|
+
initialValues,
|
|
1050
|
+
items
|
|
1051
|
+
]);
|
|
1052
|
+
if (!antdProps) return null;
|
|
1053
|
+
const submitButtonProps = typeof submit === "object" ? submit.buttonProps : void 0;
|
|
1054
|
+
const submitButtonLoading = loading ? true : submitButtonProps?.loading ?? false;
|
|
980
1055
|
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(antd.Form, {
|
|
981
|
-
...
|
|
1056
|
+
...antdProps,
|
|
1057
|
+
form,
|
|
1058
|
+
onFinish,
|
|
982
1059
|
onValuesChange,
|
|
983
1060
|
children: [
|
|
984
|
-
|
|
985
|
-
|
|
1061
|
+
props.beforeItems,
|
|
1062
|
+
items.map((item) => {
|
|
986
1063
|
if (isFormItemProps(item)) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FormItem, {
|
|
987
1064
|
...item,
|
|
988
1065
|
...extendTypes ? { extendTypes } : {}
|
|
989
1066
|
}, item.id);
|
|
990
1067
|
return item;
|
|
991
1068
|
}),
|
|
992
|
-
|
|
1069
|
+
props.children,
|
|
993
1070
|
typeof submit !== "boolean" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(antd.Button, {
|
|
1071
|
+
...submitButtonProps,
|
|
994
1072
|
htmlType: "submit",
|
|
995
|
-
type: "primary",
|
|
996
|
-
loading,
|
|
1073
|
+
type: submitButtonProps?.type || "primary",
|
|
1074
|
+
loading: submitButtonLoading,
|
|
997
1075
|
children: submit?.text || config.theme.Form.submit.text
|
|
998
1076
|
}),
|
|
999
|
-
|
|
1077
|
+
props.footer
|
|
1000
1078
|
]
|
|
1001
1079
|
});
|
|
1002
1080
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { ErrorBoundaryProps, FaasDataInjection as FaasDataInjection$1, FaasDataWrapperProps as FaasDataWrapperProps$1, FaasDataWrapperRef, FaasReactClient, FaasReactClientOptions, faas, useFaas } from "@faasjs/react";
|
|
2
2
|
import { ButtonProps, ConfigProviderProps as ConfigProviderProps$1, DatePickerProps, DescriptionsProps, Drawer, DrawerProps as DrawerProps$1, FormInstance, FormItemProps as FormItemProps$1, FormProps as FormProps$1, GlobalToken, InputNumberProps, InputProps, Modal, ModalProps as ModalProps$1, RadioProps, SelectProps, SwitchProps, TableColumnProps, TablePaginationConfig, TableProps as TableProps$1, TabsProps as TabsProps$1 } from "antd";
|
|
3
3
|
import { BrowserRouterProps, RouteProps } from "react-router-dom";
|
|
4
4
|
import * as react from "react";
|
|
5
5
|
import { CSSProperties, ComponentType, Dispatch, FC, JSX, LazyExoticComponent, ReactElement, ReactNode, SetStateAction, lazy } from "react";
|
|
6
|
-
import
|
|
6
|
+
import * as react_jsx_runtime0 from "react/jsx-runtime";
|
|
7
7
|
import { Dayjs } from "dayjs";
|
|
8
8
|
import * as antd_es_form_FormItem0 from "antd/es/form/FormItem";
|
|
9
9
|
import * as antd_es_form0 from "antd/es/form";
|
|
@@ -752,55 +752,97 @@ declare function useDrawer(init?: DrawerProps): {
|
|
|
752
752
|
//#endregion
|
|
753
753
|
//#region src/Form.d.ts
|
|
754
754
|
type FormSubmitProps = {
|
|
755
|
-
/** Default: Submit */text?: string;
|
|
756
|
-
|
|
757
|
-
* Submit to FaasJS server.
|
|
758
|
-
*
|
|
759
|
-
* If use onFinish, you should call submit manually.
|
|
760
|
-
* ```ts
|
|
761
|
-
* {
|
|
762
|
-
* submit: {
|
|
763
|
-
* to: {
|
|
764
|
-
* action: 'action_name'
|
|
765
|
-
* }
|
|
766
|
-
* },
|
|
767
|
-
* onFinish: (values, submit) => {
|
|
768
|
-
* // do something before submit
|
|
769
|
-
*
|
|
770
|
-
* // submit
|
|
771
|
-
* await submit({
|
|
772
|
-
* ...values,
|
|
773
|
-
* extraProps: 'some extra props'
|
|
774
|
-
* })
|
|
775
|
-
*
|
|
776
|
-
* // do something after submit
|
|
777
|
-
* }
|
|
778
|
-
* }
|
|
779
|
-
* ```
|
|
780
|
-
*/
|
|
781
|
-
to?: {
|
|
782
|
-
action: FaasAction; /** params will overwrite form values before submit */
|
|
783
|
-
params?: Record<string, any>;
|
|
784
|
-
then?: (result: any) => void;
|
|
785
|
-
catch?: (error: any) => void;
|
|
786
|
-
finally?: () => void;
|
|
787
|
-
};
|
|
755
|
+
/** Default: Submit */text?: string; /** Props for the built-in submit button */
|
|
756
|
+
buttonProps?: ButtonProps;
|
|
788
757
|
};
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
758
|
+
/**
|
|
759
|
+
* Built-in FaasJS submit handler for Form.
|
|
760
|
+
*
|
|
761
|
+
* @example
|
|
762
|
+
* ```ts
|
|
763
|
+
* {
|
|
764
|
+
* action: 'user/create',
|
|
765
|
+
* params: (values) => ({
|
|
766
|
+
* ...values,
|
|
767
|
+
* role: values.role || 'user',
|
|
768
|
+
* }),
|
|
769
|
+
* onSuccess: (result) => {
|
|
770
|
+
* console.log(result)
|
|
771
|
+
* },
|
|
772
|
+
* }
|
|
773
|
+
* ```
|
|
774
|
+
*/
|
|
775
|
+
type FormFaasProps<Values extends Record<string, any> = any> = {
|
|
776
|
+
/** Action name to submit to */action: FaasAction; /** params will overwrite form values before submit */
|
|
777
|
+
params?: Record<string, any> | ((values: Record<string, any>) => Record<string, any>); /** Transform form values before sending the request */
|
|
778
|
+
transformValues?: (values: Values) => Record<string, any> | Promise<Record<string, any>>; /** Called when the request succeeds */
|
|
779
|
+
onSuccess?: (result: any, values: Record<string, any>) => void; /** Called when the request fails */
|
|
780
|
+
onError?: (error: any, values: Record<string, any>) => void; /** Called after the request settles */
|
|
781
|
+
onFinally?: () => void;
|
|
782
|
+
};
|
|
783
|
+
type FormProps<Values extends Record<string, any> = any, ExtendItemProps extends ExtendFormItemProps = ExtendFormItemProps> = Omit<FormProps$1<Values>, 'onFinish' | 'children' | 'initialValues'> & {
|
|
784
|
+
items?: ((ExtendItemProps extends ExtendFormItemProps ? ExtendItemProps | FormItemProps : FormItemProps) | JSX.Element)[]; /** Default: { text: 'Submit' }, set false to disable it */
|
|
792
785
|
submit?: false | FormSubmitProps;
|
|
793
|
-
onFinish?: (values: Values, submit?: (values: any) => Promise<any>) => Promise<any>;
|
|
794
786
|
beforeItems?: JSX.Element | JSX.Element[];
|
|
795
787
|
footer?: JSX.Element | JSX.Element[];
|
|
796
788
|
extendTypes?: ExtendTypes;
|
|
797
789
|
children?: ReactNode;
|
|
798
790
|
initialValues?: Partial<Values>;
|
|
799
|
-
}
|
|
791
|
+
} & ({
|
|
792
|
+
/** Built-in FaasJS submit handler, ignored when onFinish is provided */faas?: FormFaasProps<Values>;
|
|
793
|
+
onFinish?: never;
|
|
794
|
+
} | {
|
|
795
|
+
faas?: never; /** Custom submit handler, takes precedence over faas when both are provided */
|
|
796
|
+
onFinish?: (values: Values) => void | Promise<void>;
|
|
797
|
+
});
|
|
800
798
|
/**
|
|
801
799
|
* Form component with Ant Design & FaasJS
|
|
802
800
|
*
|
|
803
801
|
* - Based on [Ant Design Form](https://ant.design/components/form/).
|
|
802
|
+
* - Use `onFinish` for custom submit logic.
|
|
803
|
+
* - Use `faas` for the built-in FaasJS submit flow.
|
|
804
|
+
*
|
|
805
|
+
* @example
|
|
806
|
+
* ```tsx
|
|
807
|
+
* import { Form } from '@faasjs/ant-design'
|
|
808
|
+
*
|
|
809
|
+
* export function ProfileForm() {
|
|
810
|
+
* return (
|
|
811
|
+
* <Form
|
|
812
|
+
* items={[
|
|
813
|
+
* { id: 'name', required: true },
|
|
814
|
+
* { id: 'email', required: true },
|
|
815
|
+
* ]}
|
|
816
|
+
* onFinish={async (values) => {
|
|
817
|
+
* console.log(values)
|
|
818
|
+
* }}
|
|
819
|
+
* />
|
|
820
|
+
* )
|
|
821
|
+
* }
|
|
822
|
+
* ```
|
|
823
|
+
*
|
|
824
|
+
* @example
|
|
825
|
+
* ```tsx
|
|
826
|
+
* import { Form } from '@faasjs/ant-design'
|
|
827
|
+
*
|
|
828
|
+
* export function CreateUserForm() {
|
|
829
|
+
* return (
|
|
830
|
+
* <Form
|
|
831
|
+
* initialValues={{ role: 'user' }}
|
|
832
|
+
* items={[
|
|
833
|
+
* { id: 'name', required: true },
|
|
834
|
+
* { id: 'role', options: ['user', 'admin'] },
|
|
835
|
+
* ]}
|
|
836
|
+
* faas={{
|
|
837
|
+
* action: 'user/create',
|
|
838
|
+
* params: (values) => ({
|
|
839
|
+
* role: values.role || 'user',
|
|
840
|
+
* }),
|
|
841
|
+
* }}
|
|
842
|
+
* />
|
|
843
|
+
* )
|
|
844
|
+
* }
|
|
845
|
+
* ```
|
|
804
846
|
*/
|
|
805
847
|
declare function Form<Values extends Record<string, any> = any>(props: FormProps<Values>): react_jsx_runtime0.JSX.Element | null;
|
|
806
848
|
declare namespace Form {
|
|
@@ -1001,4 +1043,4 @@ declare function useApp<NewT extends useAppProps = useAppProps>(this: void): Rea
|
|
|
1001
1043
|
*/
|
|
1002
1044
|
declare function useThemeToken(): GlobalToken;
|
|
1003
1045
|
//#endregion
|
|
1004
|
-
export { App, AppContext, AppProps, BaseItemProps, BaseOption, Blank, BlankProps, ConfigContext, ConfigProvider, ConfigProviderProps, Description, DescriptionItemContentProps, DescriptionItemProps, DescriptionProps, Drawer, DrawerProps, ErrorBoundary, type ErrorBoundaryProps, ExtendDescriptionItemProps, ExtendDescriptionTypeProps, type ExtendFormItemProps, type ExtendFormTypeProps, ExtendTableItemProps, ExtendTableTypeProps, ExtendTypes, FaasDataInjection, FaasDataWrapper, FaasDataWrapperProps, type FaasDataWrapperRef, FaasItemProps, FaasItemType, FaasItemTypeValue, FaasReactClient, type FaasReactClientOptions, Form, FormItem, FormItemProps, FormProps, FormSubmitProps, Link, LinkProps, Loading, LoadingProps, Modal, ModalProps, PageNotFound, ResolvedTheme, Routes, RoutesProps, TabProps, Table, TableFaasDataParams, TableFaasDataResponse, TableItemProps, TableProps, Tabs, TabsProps, Title, TitleProps, UnionFaasItemElement, UnionFaasItemInjection, UnionFaasItemProps, UnionFaasItemRender, UnionScene, cloneUnionFaasItemElement, faas, idToTitle, lazy, setDrawerProps, setModalProps, transferOptions, transferValue, useApp, useAppProps, useConfigContext, useDrawer, useFaas, useModal, useThemeToken, withFaasData };
|
|
1046
|
+
export { App, AppContext, AppProps, BaseItemProps, BaseOption, Blank, BlankProps, ConfigContext, ConfigProvider, ConfigProviderProps, Description, DescriptionItemContentProps, DescriptionItemProps, DescriptionProps, Drawer, DrawerProps, ErrorBoundary, type ErrorBoundaryProps, ExtendDescriptionItemProps, ExtendDescriptionTypeProps, type ExtendFormItemProps, type ExtendFormTypeProps, ExtendTableItemProps, ExtendTableTypeProps, ExtendTypes, FaasDataInjection, FaasDataWrapper, FaasDataWrapperProps, type FaasDataWrapperRef, FaasItemProps, FaasItemType, FaasItemTypeValue, FaasReactClient, type FaasReactClientOptions, Form, FormFaasProps, FormItem, FormItemProps, FormProps, FormSubmitProps, Link, LinkProps, Loading, LoadingProps, Modal, ModalProps, PageNotFound, ResolvedTheme, Routes, RoutesProps, TabProps, Table, TableFaasDataParams, TableFaasDataResponse, TableItemProps, TableProps, Tabs, TabsProps, Title, TitleProps, UnionFaasItemElement, UnionFaasItemInjection, UnionFaasItemProps, UnionFaasItemRender, UnionScene, cloneUnionFaasItemElement, faas, idToTitle, lazy, setDrawerProps, setModalProps, transferOptions, transferValue, useApp, useAppProps, useConfigContext, useDrawer, useFaas, useModal, useThemeToken, withFaasData };
|
package/dist/index.mjs
CHANGED
|
@@ -874,105 +874,183 @@ function isFormItemProps(item) {
|
|
|
874
874
|
* Form component with Ant Design & FaasJS
|
|
875
875
|
*
|
|
876
876
|
* - Based on [Ant Design Form](https://ant.design/components/form/).
|
|
877
|
+
* - Use `onFinish` for custom submit logic.
|
|
878
|
+
* - Use `faas` for the built-in FaasJS submit flow.
|
|
879
|
+
*
|
|
880
|
+
* @example
|
|
881
|
+
* ```tsx
|
|
882
|
+
* import { Form } from '@faasjs/ant-design'
|
|
883
|
+
*
|
|
884
|
+
* export function ProfileForm() {
|
|
885
|
+
* return (
|
|
886
|
+
* <Form
|
|
887
|
+
* items={[
|
|
888
|
+
* { id: 'name', required: true },
|
|
889
|
+
* { id: 'email', required: true },
|
|
890
|
+
* ]}
|
|
891
|
+
* onFinish={async (values) => {
|
|
892
|
+
* console.log(values)
|
|
893
|
+
* }}
|
|
894
|
+
* />
|
|
895
|
+
* )
|
|
896
|
+
* }
|
|
897
|
+
* ```
|
|
898
|
+
*
|
|
899
|
+
* @example
|
|
900
|
+
* ```tsx
|
|
901
|
+
* import { Form } from '@faasjs/ant-design'
|
|
902
|
+
*
|
|
903
|
+
* export function CreateUserForm() {
|
|
904
|
+
* return (
|
|
905
|
+
* <Form
|
|
906
|
+
* initialValues={{ role: 'user' }}
|
|
907
|
+
* items={[
|
|
908
|
+
* { id: 'name', required: true },
|
|
909
|
+
* { id: 'role', options: ['user', 'admin'] },
|
|
910
|
+
* ]}
|
|
911
|
+
* faas={{
|
|
912
|
+
* action: 'user/create',
|
|
913
|
+
* params: (values) => ({
|
|
914
|
+
* role: values.role || 'user',
|
|
915
|
+
* }),
|
|
916
|
+
* }}
|
|
917
|
+
* />
|
|
918
|
+
* )
|
|
919
|
+
* }
|
|
920
|
+
* ```
|
|
877
921
|
*/
|
|
878
922
|
function Form(props) {
|
|
879
923
|
const [loading, setLoading] = useState(false);
|
|
880
|
-
const [
|
|
881
|
-
const [submit, setSubmit] = useState();
|
|
924
|
+
const [antdProps, setAntdProps] = useState();
|
|
925
|
+
const [submit, setSubmit] = useState(props.submit === false ? false : {});
|
|
926
|
+
const [items, setItems] = useState([]);
|
|
882
927
|
const config = useConfigContext();
|
|
883
928
|
const [extendTypes, setExtendTypes] = useState();
|
|
884
929
|
const [form] = Form$1.useForm(props.form);
|
|
885
930
|
const [initialValues, setInitialValues] = useState(props.initialValues || Object.create(null));
|
|
931
|
+
const [onFinish, setOnFinish] = useState();
|
|
886
932
|
useEqualEffect(() => {
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
}
|
|
899
|
-
if (propsCopy.items?.length) {
|
|
900
|
-
for (const item of propsCopy.items) if (isFormItemProps(item) && item.if) item.hidden = !item.if(nextInitialValues || Object.create(null));
|
|
933
|
+
if (props.onFinish) {
|
|
934
|
+
setOnFinish(() => async (values) => {
|
|
935
|
+
if (!props.onFinish) return;
|
|
936
|
+
setLoading(true);
|
|
937
|
+
try {
|
|
938
|
+
return await props.onFinish(values);
|
|
939
|
+
} finally {
|
|
940
|
+
setLoading(false);
|
|
941
|
+
}
|
|
942
|
+
});
|
|
943
|
+
return;
|
|
901
944
|
}
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
propsCopy.onFinish = async (values) => {
|
|
945
|
+
if (props.faas?.action) {
|
|
946
|
+
setOnFinish(() => async (values) => {
|
|
947
|
+
if (!props.faas?.action) return;
|
|
906
948
|
setLoading(true);
|
|
949
|
+
let submitValues = values;
|
|
907
950
|
try {
|
|
908
|
-
if (
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
951
|
+
if (props.faas?.transformValues) submitValues = await props.faas.transformValues(values);
|
|
952
|
+
const extraParams = typeof props.faas?.params === "function" ? props.faas.params(submitValues) : props.faas.params;
|
|
953
|
+
if (extraParams) submitValues = {
|
|
954
|
+
...submitValues,
|
|
955
|
+
...extraParams
|
|
956
|
+
};
|
|
957
|
+
const result = await faas(props.faas.action, submitValues);
|
|
958
|
+
props.faas.onSuccess?.(result, submitValues);
|
|
959
|
+
return result;
|
|
913
960
|
} catch (error) {
|
|
914
|
-
|
|
961
|
+
props.faas.onError?.(error, submitValues);
|
|
962
|
+
throw error;
|
|
963
|
+
} finally {
|
|
964
|
+
props.faas.onFinally?.();
|
|
965
|
+
setLoading(false);
|
|
915
966
|
}
|
|
916
|
-
setLoading(false);
|
|
917
|
-
};
|
|
918
|
-
} else if (submitTo?.action) propsCopy.onFinish = async (values) => {
|
|
919
|
-
setLoading(true);
|
|
920
|
-
return faas(submitTo.action, submitTo.params ? {
|
|
921
|
-
...values,
|
|
922
|
-
...submitTo.params
|
|
923
|
-
} : values).then((result) => {
|
|
924
|
-
submitTo.then?.(result);
|
|
925
|
-
return result;
|
|
926
|
-
}).catch((error) => {
|
|
927
|
-
submitTo.catch?.(error);
|
|
928
|
-
return Promise.reject(error);
|
|
929
|
-
}).finally(() => {
|
|
930
|
-
submitTo.finally?.();
|
|
931
|
-
setLoading(false);
|
|
932
967
|
});
|
|
933
|
-
|
|
934
|
-
if (propsCopy.extendTypes) {
|
|
935
|
-
setExtendTypes(propsCopy.extendTypes);
|
|
936
|
-
delete propsCopy.extendTypes;
|
|
968
|
+
return;
|
|
937
969
|
}
|
|
938
|
-
|
|
939
|
-
}, [
|
|
970
|
+
setOnFinish(void 0);
|
|
971
|
+
}, [props.onFinish, props.faas]);
|
|
972
|
+
useEqualEffect(() => {
|
|
973
|
+
setExtendTypes(props.extendTypes);
|
|
974
|
+
}, [props.extendTypes]);
|
|
975
|
+
useEqualEffect(() => {
|
|
976
|
+
setSubmit(props.submit === false ? false : props.submit || {});
|
|
977
|
+
}, [props.submit]);
|
|
978
|
+
useEqualEffect(() => {
|
|
979
|
+
const nextInitialValues = props.initialValues ? JSON.parse(JSON.stringify(props.initialValues)) : Object.create(null);
|
|
980
|
+
for (const key in nextInitialValues) nextInitialValues[key] = transferValue(props.items?.find((item) => isFormItemProps(item) && item.id === key)?.type, nextInitialValues[key]);
|
|
981
|
+
if (props.items?.length) setItems(props.items.map((item) => {
|
|
982
|
+
if (!isFormItemProps(item) || !item.if) return item;
|
|
983
|
+
return {
|
|
984
|
+
...item,
|
|
985
|
+
hidden: !item.if(nextInitialValues)
|
|
986
|
+
};
|
|
987
|
+
}));
|
|
988
|
+
else setItems([]);
|
|
989
|
+
if (props.initialValues) {
|
|
990
|
+
setInitialValues(nextInitialValues);
|
|
991
|
+
return;
|
|
992
|
+
}
|
|
993
|
+
setInitialValues(null);
|
|
994
|
+
}, [props.initialValues, props.items]);
|
|
995
|
+
useEqualEffect(() => {
|
|
996
|
+
const propsCopy = { ...props };
|
|
997
|
+
delete propsCopy.onFinish;
|
|
998
|
+
delete propsCopy.faas;
|
|
999
|
+
delete propsCopy.extendTypes;
|
|
1000
|
+
delete propsCopy.submit;
|
|
1001
|
+
delete propsCopy.items;
|
|
1002
|
+
delete propsCopy.initialValues;
|
|
1003
|
+
setAntdProps(propsCopy);
|
|
1004
|
+
}, [props]);
|
|
940
1005
|
const onValuesChange = useEqualCallback((changedValues, allValues) => {
|
|
941
1006
|
console.debug("Form:onValuesChange", changedValues, allValues);
|
|
942
1007
|
if (props.onValuesChange) props.onValuesChange(changedValues, allValues);
|
|
943
|
-
if (!
|
|
1008
|
+
if (!items.length) return;
|
|
944
1009
|
for (const key in changedValues) {
|
|
945
|
-
const item =
|
|
1010
|
+
const item = items.find((i) => isFormItemProps(i) && i.id === key);
|
|
946
1011
|
if (item?.onValueChange) item.onValueChange(changedValues[key], allValues, form);
|
|
947
1012
|
}
|
|
948
|
-
}, [
|
|
1013
|
+
}, [
|
|
1014
|
+
items,
|
|
1015
|
+
props.onValuesChange,
|
|
1016
|
+
form
|
|
1017
|
+
]);
|
|
949
1018
|
useEqualEffect(() => {
|
|
950
1019
|
if (!initialValues) return;
|
|
951
1020
|
console.debug("Form:initialValues", initialValues);
|
|
952
1021
|
form.setFieldsValue(initialValues);
|
|
953
1022
|
setInitialValues(null);
|
|
954
|
-
}, [
|
|
955
|
-
|
|
1023
|
+
}, [
|
|
1024
|
+
form,
|
|
1025
|
+
initialValues,
|
|
1026
|
+
items
|
|
1027
|
+
]);
|
|
1028
|
+
if (!antdProps) return null;
|
|
1029
|
+
const submitButtonProps = typeof submit === "object" ? submit.buttonProps : void 0;
|
|
1030
|
+
const submitButtonLoading = loading ? true : submitButtonProps?.loading ?? false;
|
|
956
1031
|
return /* @__PURE__ */ jsxs(Form$1, {
|
|
957
|
-
...
|
|
1032
|
+
...antdProps,
|
|
1033
|
+
form,
|
|
1034
|
+
onFinish,
|
|
958
1035
|
onValuesChange,
|
|
959
1036
|
children: [
|
|
960
|
-
|
|
961
|
-
|
|
1037
|
+
props.beforeItems,
|
|
1038
|
+
items.map((item) => {
|
|
962
1039
|
if (isFormItemProps(item)) return /* @__PURE__ */ jsx(FormItem, {
|
|
963
1040
|
...item,
|
|
964
1041
|
...extendTypes ? { extendTypes } : {}
|
|
965
1042
|
}, item.id);
|
|
966
1043
|
return item;
|
|
967
1044
|
}),
|
|
968
|
-
|
|
1045
|
+
props.children,
|
|
969
1046
|
typeof submit !== "boolean" && /* @__PURE__ */ jsx(Button, {
|
|
1047
|
+
...submitButtonProps,
|
|
970
1048
|
htmlType: "submit",
|
|
971
|
-
type: "primary",
|
|
972
|
-
loading,
|
|
1049
|
+
type: submitButtonProps?.type || "primary",
|
|
1050
|
+
loading: submitButtonLoading,
|
|
973
1051
|
children: submit?.text || config.theme.Form.submit.text
|
|
974
1052
|
}),
|
|
975
|
-
|
|
1053
|
+
props.footer
|
|
976
1054
|
]
|
|
977
1055
|
});
|
|
978
1056
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@faasjs/ant-design",
|
|
3
|
-
"version": "8.0.0-beta.
|
|
3
|
+
"version": "8.0.0-beta.17",
|
|
4
4
|
"homepage": "https://faasjs.com/doc/ant-design",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/faasjs/faasjs/issues"
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@ant-design/icons": "*",
|
|
31
|
-
"@faasjs/react": ">=8.0.0-beta.
|
|
31
|
+
"@faasjs/react": ">=8.0.0-beta.17",
|
|
32
32
|
"@types/lodash-es": "*",
|
|
33
33
|
"@types/react": "^19.0.0",
|
|
34
34
|
"@types/react-dom": "^19.0.0",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"peerDependencies": {
|
|
42
42
|
"@ant-design/icons": "*",
|
|
43
|
-
"@faasjs/react": ">=8.0.0-beta.
|
|
43
|
+
"@faasjs/react": ">=8.0.0-beta.17",
|
|
44
44
|
"antd": "^6.0.0",
|
|
45
45
|
"lodash-es": "*",
|
|
46
46
|
"react": "^19.0.0",
|