ding-react-admin 1.0.4 → 2.0.4
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/LICENSE +21 -21
- package/README.md +254 -201
- package/dist/index.css +2 -0
- package/dist/index.js +2073 -1202
- package/dist/src/components/NavMenuLabel.d.ts +8 -0
- package/dist/src/components/NavMenuLabel.d.ts.map +1 -0
- package/dist/src/components/NavMenuSearch.d.ts +9 -0
- package/dist/src/components/NavMenuSearch.d.ts.map +1 -0
- package/dist/src/components/ScrollableArea.d.ts +10 -0
- package/dist/src/components/ScrollableArea.d.ts.map +1 -0
- package/dist/src/components/index.d.ts +3 -0
- package/dist/src/components/index.d.ts.map +1 -1
- package/dist/src/context/AuthProvider.d.ts +2 -0
- package/dist/src/context/AuthProvider.d.ts.map +1 -1
- package/dist/src/crud/FormGlobalErrorsAlert.d.ts +5 -0
- package/dist/src/crud/FormGlobalErrorsAlert.d.ts.map +1 -0
- package/dist/src/crud/InlineFormSet.d.ts +2 -17
- package/dist/src/crud/InlineFormSet.d.ts.map +1 -1
- package/dist/src/crud/ResourceForm.d.ts +2 -10
- package/dist/src/crud/ResourceForm.d.ts.map +1 -1
- package/dist/src/crud/ResourceFormModal.d.ts +7 -1
- package/dist/src/crud/ResourceFormModal.d.ts.map +1 -1
- package/dist/src/crud/ResourceList.d.ts.map +1 -1
- package/dist/src/crud/ResourceRecordForm.d.ts +27 -0
- package/dist/src/crud/ResourceRecordForm.d.ts.map +1 -0
- package/dist/src/crud/columns/ImageColumn.d.ts +14 -0
- package/dist/src/crud/columns/ImageColumn.d.ts.map +1 -0
- package/dist/src/crud/context/InlineFieldsRegistry.d.ts +15 -0
- package/dist/src/crud/context/InlineFieldsRegistry.d.ts.map +1 -0
- package/dist/src/crud/context/PayloadFieldsContext.d.ts +18 -0
- package/dist/src/crud/context/PayloadFieldsContext.d.ts.map +1 -0
- package/dist/src/crud/fields/FileField.d.ts +12 -0
- package/dist/src/crud/fields/FileField.d.ts.map +1 -0
- package/dist/src/crud/fields/ImageField.d.ts +14 -0
- package/dist/src/crud/fields/ImageField.d.ts.map +1 -0
- package/dist/src/crud/fields/ReferenceField.d.ts +12 -2
- package/dist/src/crud/fields/ReferenceField.d.ts.map +1 -1
- package/dist/src/crud/fields/ReferenceInputActions.d.ts +16 -0
- package/dist/src/crud/fields/ReferenceInputActions.d.ts.map +1 -0
- package/dist/src/crud/fields/ReferenceManyField.d.ts +10 -1
- package/dist/src/crud/fields/ReferenceManyField.d.ts.map +1 -1
- package/dist/src/crud/fields/uploadFieldUtils.d.ts +5 -0
- package/dist/src/crud/fields/uploadFieldUtils.d.ts.map +1 -0
- package/dist/src/crud/fields/useUploadPreviewUrl.d.ts +4 -0
- package/dist/src/crud/fields/useUploadPreviewUrl.d.ts.map +1 -0
- package/dist/src/crud/filters/ReferenceFilter.d.ts +1 -1
- package/dist/src/crud/filters/ReferenceFilter.d.ts.map +1 -1
- package/dist/src/crud/index.d.ts +27 -9
- package/dist/src/crud/index.d.ts.map +1 -1
- package/dist/src/crud/types.d.ts +21 -7
- package/dist/src/crud/types.d.ts.map +1 -1
- package/dist/src/crud/utils/buildFormPayload.d.ts +6 -0
- package/dist/src/crud/utils/buildFormPayload.d.ts.map +1 -0
- package/dist/src/crud/utils/buildInlineRowsPayload.d.ts +6 -0
- package/dist/src/crud/utils/buildInlineRowsPayload.d.ts.map +1 -0
- package/dist/src/crud/utils/buildResourceFormSubmitBody.d.ts +5 -0
- package/dist/src/crud/utils/buildResourceFormSubmitBody.d.ts.map +1 -0
- package/dist/src/crud/utils/choiceSelectionUtils.d.ts +10 -0
- package/dist/src/crud/utils/choiceSelectionUtils.d.ts.map +1 -0
- package/dist/src/crud/utils/choiceSelectionUtils.test.d.ts +2 -0
- package/dist/src/crud/utils/choiceSelectionUtils.test.d.ts.map +1 -0
- package/dist/src/crud/utils/formErrors.d.ts +18 -7
- package/dist/src/crud/utils/formErrors.d.ts.map +1 -1
- package/dist/src/crud/utils/formErrors.test.d.ts +2 -0
- package/dist/src/crud/utils/formErrors.test.d.ts.map +1 -0
- package/dist/src/crud/utils/getFormValue.d.ts +3 -0
- package/dist/src/crud/utils/getFormValue.d.ts.map +1 -0
- package/dist/src/crud/utils/hasUploadValues.d.ts +3 -0
- package/dist/src/crud/utils/hasUploadValues.d.ts.map +1 -0
- package/dist/src/crud/utils/nestedFieldPath.d.ts +3 -0
- package/dist/src/crud/utils/nestedFieldPath.d.ts.map +1 -0
- package/dist/src/crud/utils/prepareFormSubmitBody.d.ts +8 -0
- package/dist/src/crud/utils/prepareFormSubmitBody.d.ts.map +1 -0
- package/dist/src/crud/utils/referenceSelectNotFoundContent.d.ts +4 -0
- package/dist/src/crud/utils/referenceSelectNotFoundContent.d.ts.map +1 -0
- package/dist/src/crud/utils/setFormValue.d.ts +3 -0
- package/dist/src/crud/utils/setFormValue.d.ts.map +1 -0
- package/dist/src/crud/utils/toFormData.d.ts +13 -0
- package/dist/src/crud/utils/toFormData.d.ts.map +1 -0
- package/dist/src/crud/utils/uploadReferenceUtils.d.ts +5 -0
- package/dist/src/crud/utils/uploadReferenceUtils.d.ts.map +1 -0
- package/dist/src/crud/utils/useChoices.d.ts +27 -1
- package/dist/src/crud/utils/useChoices.d.ts.map +1 -1
- package/dist/src/crud/utils/useFormRecord.d.ts +44 -0
- package/dist/src/crud/utils/useFormRecord.d.ts.map +1 -0
- package/dist/src/data/createRestResourceHandlers.d.ts +5 -5
- package/dist/src/data/createRestResourceHandlers.d.ts.map +1 -1
- package/dist/src/data/dataProviderTypes.d.ts +6 -5
- package/dist/src/data/dataProviderTypes.d.ts.map +1 -1
- package/dist/src/data/parseFormErrorHelpers.d.ts +23 -6
- package/dist/src/data/parseFormErrorHelpers.d.ts.map +1 -1
- package/dist/src/data/resourceHandlers.d.ts +1 -1
- package/dist/src/data/resourceHandlers.d.ts.map +1 -1
- package/dist/src/index.d.ts +4 -4
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/layouts/AdminLayout.d.ts +1 -1
- package/dist/src/layouts/AdminLayout.d.ts.map +1 -1
- package/dist/src/layouts/AuthPageLayout.d.ts.map +1 -1
- package/dist/src/layouts/navFilter.d.ts +5 -0
- package/dist/src/layouts/navFilter.d.ts.map +1 -0
- package/dist/src/layouts/navMenuItems.d.ts +6 -0
- package/dist/src/layouts/navMenuItems.d.ts.map +1 -0
- package/dist/src/types.d.ts +12 -0
- package/dist/src/types.d.ts.map +1 -1
- package/package.json +79 -59
- package/src/app/AdminApp.tsx +34 -34
- package/src/components/AuthAlternateLink.tsx +23 -23
- package/src/components/DensitySwitch.tsx +31 -31
- package/src/components/NavMenuLabel.tsx +26 -0
- package/src/components/NavMenuSearch.tsx +76 -0
- package/src/components/ScrollableArea.tsx +46 -0
- package/src/components/ThemeSwitch.tsx +28 -28
- package/src/components/ThemeToolbar.tsx +34 -34
- package/src/components/index.ts +7 -4
- package/src/components/navMenu.css +21 -0
- package/src/components/scrollable.css +28 -0
- package/src/context/AppThemeProvider.tsx +128 -128
- package/src/context/AuthProvider.tsx +89 -77
- package/src/context/DataProvider.tsx +30 -30
- package/src/context/PermissionsProvider.tsx +57 -57
- package/src/crud/FilterBar.tsx +36 -36
- package/src/crud/FormGlobalErrorsAlert.tsx +36 -0
- package/src/crud/FormSteps.tsx +135 -135
- package/src/crud/FormTabs.tsx +126 -126
- package/src/crud/InlineFormSet.tsx +162 -219
- package/src/crud/ListActionsBar.tsx +106 -106
- package/src/crud/ResourceForm.tsx +83 -166
- package/src/crud/ResourceFormModal.tsx +75 -133
- package/src/crud/ResourceList.tsx +1 -0
- package/src/crud/ResourceRecordForm.tsx +166 -0
- package/src/crud/columns/BooleanColumn.tsx +32 -32
- package/src/crud/columns/CustomColumn.tsx +33 -33
- package/src/crud/columns/DateColumn.tsx +28 -28
- package/src/crud/columns/ImageColumn.tsx +58 -0
- package/src/crud/columns/NumberColumn.tsx +31 -31
- package/src/crud/columns/ReferenceColumn.tsx +85 -85
- package/src/crud/columns/ReferenceManyColumn.tsx +67 -67
- package/src/crud/columns/TextColumn.tsx +38 -38
- package/src/crud/context/FilterContext.tsx +71 -71
- package/src/crud/context/FormContext.tsx +39 -39
- package/src/crud/context/FormSectionContext.tsx +22 -22
- package/src/crud/context/InlineFieldsRegistry.tsx +61 -0
- package/src/crud/context/ListContext.tsx +93 -93
- package/src/crud/context/PayloadFieldsContext.tsx +62 -0
- package/src/crud/fields/BooleanField.tsx +38 -38
- package/src/crud/fields/DateField.tsx +53 -53
- package/src/crud/fields/FieldWrapper.tsx +87 -87
- package/src/crud/fields/FileField.tsx +123 -0
- package/src/crud/fields/ImageField.tsx +120 -0
- package/src/crud/fields/NumberField.tsx +52 -52
- package/src/crud/fields/PasswordField.tsx +99 -99
- package/src/crud/fields/ReferenceField.tsx +237 -96
- package/src/crud/fields/ReferenceInputActions.tsx +106 -0
- package/src/crud/fields/ReferenceManyField.tsx +200 -76
- package/src/crud/fields/SelectField.tsx +48 -48
- package/src/crud/fields/TextField.tsx +46 -46
- package/src/crud/fields/uploadFieldUtils.ts +21 -0
- package/src/crud/fields/useUploadPreviewUrl.ts +20 -0
- package/src/crud/filters/BooleanFilter.tsx +38 -38
- package/src/crud/filters/DateFilter.tsx +35 -35
- package/src/crud/filters/NumberFilter.tsx +33 -33
- package/src/crud/filters/ReferenceFilter.tsx +192 -173
- package/src/crud/filters/SelectFilter.tsx +44 -44
- package/src/crud/filters/TextFilter.tsx +36 -36
- package/src/crud/index.ts +99 -90
- package/src/crud/types.ts +146 -132
- package/src/crud/utils/buildFormPayload.ts +24 -0
- package/src/crud/utils/buildInlineRowsPayload.ts +37 -0
- package/src/crud/utils/buildResourceFormSubmitBody.ts +29 -0
- package/src/crud/utils/choiceSelectionUtils.test.ts +45 -0
- package/src/crud/utils/choiceSelectionUtils.ts +116 -0
- package/src/crud/utils/formErrors.test.ts +168 -0
- package/src/crud/utils/formErrors.ts +138 -38
- package/src/crud/utils/formSectionErrors.ts +85 -85
- package/src/crud/utils/{getByPath.ts → getFormValue.ts} +13 -13
- package/src/crud/utils/hasUploadValues.ts +9 -0
- package/src/crud/utils/nestedFieldPath.ts +8 -0
- package/src/crud/utils/prepareFormSubmitBody.ts +22 -0
- package/src/crud/utils/referenceSelectNotFoundContent.tsx +7 -0
- package/src/crud/utils/{setByPath.ts → setFormValue.ts} +18 -18
- package/src/crud/utils/sortQueryParam.ts +37 -37
- package/src/crud/utils/toFormData.ts +79 -0
- package/src/crud/utils/uploadReferenceUtils.ts +44 -0
- package/src/crud/utils/useAbortableEffect.ts +17 -17
- package/src/crud/utils/useChoices.ts +330 -182
- package/src/crud/utils/useFormRecord.ts +179 -0
- package/src/crud/utils/useListQueryState.ts +191 -191
- package/src/data/abortError.test.ts +19 -19
- package/src/data/abortError.ts +10 -10
- package/src/data/createMemoryResourceHandlers.ts +78 -78
- package/src/data/createRestResourceHandlers.ts +63 -57
- package/src/data/dataProviderTypes.ts +7 -5
- package/src/data/dataUtils.test.ts +228 -108
- package/src/data/inMemoryList.ts +97 -97
- package/src/data/parseFormErrorHelpers.test.ts +201 -84
- package/src/data/parseFormErrorHelpers.ts +376 -224
- package/src/data/resourceHandlers.ts +1 -1
- package/src/data/sortHelpers.ts +48 -48
- package/src/index.ts +211 -195
- package/src/layouts/AdminLayout.tsx +530 -381
- package/src/layouts/AuthPageLayout.tsx +87 -81
- package/src/layouts/navFilter.ts +49 -0
- package/src/layouts/navMenuItems.tsx +44 -0
- package/src/pages/LoginPage.tsx +80 -80
- package/src/pages/PlaceholderPage.tsx +9 -9
- package/src/permissions/resourcePermissions.ts +47 -47
- package/src/router/createAdminRouter.tsx +64 -64
- package/src/router/guards.tsx +63 -63
- package/src/router/routeAccess.ts +89 -89
- package/src/types.ts +10 -0
- package/src/vite-env.d.ts +1 -1
- package/dist/src/crud/context/SubmitFieldsContext.d.ts +0 -18
- package/dist/src/crud/context/SubmitFieldsContext.d.ts.map +0 -1
- package/dist/src/crud/utils/getByPath.d.ts +0 -3
- package/dist/src/crud/utils/getByPath.d.ts.map +0 -1
- package/dist/src/crud/utils/inlineArrayName.d.ts +0 -3
- package/dist/src/crud/utils/inlineArrayName.d.ts.map +0 -1
- package/dist/src/crud/utils/inlineFieldName.d.ts +0 -3
- package/dist/src/crud/utils/inlineFieldName.d.ts.map +0 -1
- package/dist/src/crud/utils/pickBySources.d.ts +0 -6
- package/dist/src/crud/utils/pickBySources.d.ts.map +0 -1
- package/dist/src/crud/utils/setByPath.d.ts +0 -3
- package/dist/src/crud/utils/setByPath.d.ts.map +0 -1
- package/dist/src/crud/utils/useResourceFormData.d.ts +0 -47
- package/dist/src/crud/utils/useResourceFormData.d.ts.map +0 -1
- package/src/crud/context/SubmitFieldsContext.tsx +0 -62
- package/src/crud/utils/inlineArrayName.ts +0 -4
- package/src/crud/utils/inlineFieldName.ts +0 -8
- package/src/crud/utils/pickBySources.ts +0 -24
- package/src/crud/utils/useResourceFormData.ts +0 -231
package/src/crud/FormTabs.tsx
CHANGED
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
import { Tabs, theme, type TabsProps } from "antd";
|
|
2
|
-
import {
|
|
3
|
-
Children,
|
|
4
|
-
isValidElement,
|
|
5
|
-
useCallback,
|
|
6
|
-
useMemo,
|
|
7
|
-
useState,
|
|
8
|
-
type ReactElement,
|
|
9
|
-
type ReactNode,
|
|
10
|
-
} from "react";
|
|
11
|
-
import { useFormContext, useFormState } from "react-hook-form";
|
|
12
|
-
import { FormSectionSourcesProvider } from "./context/FormSectionContext";
|
|
13
|
-
import {
|
|
14
|
-
sectionHasError,
|
|
15
|
-
useActivateFirstErrorSection,
|
|
16
|
-
useSectionSourceRefs,
|
|
17
|
-
} from "./utils/formSectionErrors";
|
|
18
|
-
|
|
19
|
-
export type FormTabProps = {
|
|
20
|
-
label: ReactNode;
|
|
21
|
-
children: ReactNode;
|
|
22
|
-
disabled?: boolean;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export function FormTab(_props: FormTabProps) {
|
|
26
|
-
return null;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function isFormTab(child: ReactNode): child is ReactElement<FormTabProps> {
|
|
30
|
-
return isValidElement(child) && child.type === FormTab;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export type FormTabsProps = Omit<TabsProps, "items"> & {
|
|
34
|
-
children: ReactNode;
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
export function FormTabs({
|
|
38
|
-
children,
|
|
39
|
-
defaultActiveKey,
|
|
40
|
-
activeKey: controlledActiveKey,
|
|
41
|
-
onChange,
|
|
42
|
-
...tabsProps
|
|
43
|
-
}: FormTabsProps) {
|
|
44
|
-
const { token } = theme.useToken();
|
|
45
|
-
const tabs = useMemo(
|
|
46
|
-
() =>
|
|
47
|
-
Children.toArray(children)
|
|
48
|
-
.filter(isFormTab)
|
|
49
|
-
.map((tab, index) => ({
|
|
50
|
-
key: (tab.key as string | undefined) ?? String(index),
|
|
51
|
-
label: tab.props.label,
|
|
52
|
-
disabled: tab.props.disabled,
|
|
53
|
-
children: tab.props.children,
|
|
54
|
-
})),
|
|
55
|
-
[children],
|
|
56
|
-
);
|
|
57
|
-
|
|
58
|
-
const sectionRefs = useSectionSourceRefs(tabs.length);
|
|
59
|
-
const isControlled = controlledActiveKey !== undefined;
|
|
60
|
-
const [internalActiveKey, setInternalActiveKey] = useState(
|
|
61
|
-
() => (defaultActiveKey as string | undefined) ?? tabs[0]?.key ?? "0",
|
|
62
|
-
);
|
|
63
|
-
const activeKey = isControlled ? controlledActiveKey : internalActiveKey;
|
|
64
|
-
|
|
65
|
-
const setActiveKey = useCallback(
|
|
66
|
-
(key: string) => {
|
|
67
|
-
if (!isControlled) {
|
|
68
|
-
setInternalActiveKey(key);
|
|
69
|
-
}
|
|
70
|
-
onChange?.(key);
|
|
71
|
-
},
|
|
72
|
-
[isControlled, onChange],
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
const activateByIndex = useCallback(
|
|
76
|
-
(index: number) => {
|
|
77
|
-
const key = tabs[index]?.key;
|
|
78
|
-
if (key != null) {
|
|
79
|
-
setActiveKey(key);
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
[setActiveKey, tabs],
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
useActivateFirstErrorSection(sectionRefs, activateByIndex);
|
|
86
|
-
|
|
87
|
-
const { control, getFieldState } = useFormContext();
|
|
88
|
-
const formState = useFormState({ control });
|
|
89
|
-
|
|
90
|
-
const items = useMemo(
|
|
91
|
-
() =>
|
|
92
|
-
tabs.map((tab, index) => {
|
|
93
|
-
const hasError = sectionHasError(
|
|
94
|
-
sectionRefs[index].current,
|
|
95
|
-
getFieldState,
|
|
96
|
-
formState,
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
return {
|
|
100
|
-
key: tab.key,
|
|
101
|
-
label: hasError ? (
|
|
102
|
-
<span style={{ color: token.colorError }}>{tab.label}</span>
|
|
103
|
-
) : (
|
|
104
|
-
tab.label
|
|
105
|
-
),
|
|
106
|
-
disabled: tab.disabled,
|
|
107
|
-
children: (
|
|
108
|
-
<FormSectionSourcesProvider sourcesRef={sectionRefs[index]}>
|
|
109
|
-
{tab.children}
|
|
110
|
-
</FormSectionSourcesProvider>
|
|
111
|
-
),
|
|
112
|
-
};
|
|
113
|
-
}),
|
|
114
|
-
[formState, getFieldState, sectionRefs, tabs, token.colorError],
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
return (
|
|
118
|
-
<Tabs
|
|
119
|
-
destroyOnHidden={false}
|
|
120
|
-
items={items}
|
|
121
|
-
activeKey={activeKey}
|
|
122
|
-
onChange={setActiveKey}
|
|
123
|
-
{...tabsProps}
|
|
124
|
-
/>
|
|
125
|
-
);
|
|
126
|
-
}
|
|
1
|
+
import { Tabs, theme, type TabsProps } from "antd";
|
|
2
|
+
import {
|
|
3
|
+
Children,
|
|
4
|
+
isValidElement,
|
|
5
|
+
useCallback,
|
|
6
|
+
useMemo,
|
|
7
|
+
useState,
|
|
8
|
+
type ReactElement,
|
|
9
|
+
type ReactNode,
|
|
10
|
+
} from "react";
|
|
11
|
+
import { useFormContext, useFormState } from "react-hook-form";
|
|
12
|
+
import { FormSectionSourcesProvider } from "./context/FormSectionContext";
|
|
13
|
+
import {
|
|
14
|
+
sectionHasError,
|
|
15
|
+
useActivateFirstErrorSection,
|
|
16
|
+
useSectionSourceRefs,
|
|
17
|
+
} from "./utils/formSectionErrors";
|
|
18
|
+
|
|
19
|
+
export type FormTabProps = {
|
|
20
|
+
label: ReactNode;
|
|
21
|
+
children: ReactNode;
|
|
22
|
+
disabled?: boolean;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export function FormTab(_props: FormTabProps) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isFormTab(child: ReactNode): child is ReactElement<FormTabProps> {
|
|
30
|
+
return isValidElement(child) && child.type === FormTab;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type FormTabsProps = Omit<TabsProps, "items"> & {
|
|
34
|
+
children: ReactNode;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export function FormTabs({
|
|
38
|
+
children,
|
|
39
|
+
defaultActiveKey,
|
|
40
|
+
activeKey: controlledActiveKey,
|
|
41
|
+
onChange,
|
|
42
|
+
...tabsProps
|
|
43
|
+
}: FormTabsProps) {
|
|
44
|
+
const { token } = theme.useToken();
|
|
45
|
+
const tabs = useMemo(
|
|
46
|
+
() =>
|
|
47
|
+
Children.toArray(children)
|
|
48
|
+
.filter(isFormTab)
|
|
49
|
+
.map((tab, index) => ({
|
|
50
|
+
key: (tab.key as string | undefined) ?? String(index),
|
|
51
|
+
label: tab.props.label,
|
|
52
|
+
disabled: tab.props.disabled,
|
|
53
|
+
children: tab.props.children,
|
|
54
|
+
})),
|
|
55
|
+
[children],
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
const sectionRefs = useSectionSourceRefs(tabs.length);
|
|
59
|
+
const isControlled = controlledActiveKey !== undefined;
|
|
60
|
+
const [internalActiveKey, setInternalActiveKey] = useState(
|
|
61
|
+
() => (defaultActiveKey as string | undefined) ?? tabs[0]?.key ?? "0",
|
|
62
|
+
);
|
|
63
|
+
const activeKey = isControlled ? controlledActiveKey : internalActiveKey;
|
|
64
|
+
|
|
65
|
+
const setActiveKey = useCallback(
|
|
66
|
+
(key: string) => {
|
|
67
|
+
if (!isControlled) {
|
|
68
|
+
setInternalActiveKey(key);
|
|
69
|
+
}
|
|
70
|
+
onChange?.(key);
|
|
71
|
+
},
|
|
72
|
+
[isControlled, onChange],
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const activateByIndex = useCallback(
|
|
76
|
+
(index: number) => {
|
|
77
|
+
const key = tabs[index]?.key;
|
|
78
|
+
if (key != null) {
|
|
79
|
+
setActiveKey(key);
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
[setActiveKey, tabs],
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
useActivateFirstErrorSection(sectionRefs, activateByIndex);
|
|
86
|
+
|
|
87
|
+
const { control, getFieldState } = useFormContext();
|
|
88
|
+
const formState = useFormState({ control });
|
|
89
|
+
|
|
90
|
+
const items = useMemo(
|
|
91
|
+
() =>
|
|
92
|
+
tabs.map((tab, index) => {
|
|
93
|
+
const hasError = sectionHasError(
|
|
94
|
+
sectionRefs[index].current,
|
|
95
|
+
getFieldState,
|
|
96
|
+
formState,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
key: tab.key,
|
|
101
|
+
label: hasError ? (
|
|
102
|
+
<span style={{ color: token.colorError }}>{tab.label}</span>
|
|
103
|
+
) : (
|
|
104
|
+
tab.label
|
|
105
|
+
),
|
|
106
|
+
disabled: tab.disabled,
|
|
107
|
+
children: (
|
|
108
|
+
<FormSectionSourcesProvider sourcesRef={sectionRefs[index]}>
|
|
109
|
+
{tab.children}
|
|
110
|
+
</FormSectionSourcesProvider>
|
|
111
|
+
),
|
|
112
|
+
};
|
|
113
|
+
}),
|
|
114
|
+
[formState, getFieldState, sectionRefs, tabs, token.colorError],
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
return (
|
|
118
|
+
<Tabs
|
|
119
|
+
destroyOnHidden={false}
|
|
120
|
+
items={items}
|
|
121
|
+
activeKey={activeKey}
|
|
122
|
+
onChange={setActiveKey}
|
|
123
|
+
{...tabsProps}
|
|
124
|
+
/>
|
|
125
|
+
);
|
|
126
|
+
}
|
|
@@ -1,219 +1,162 @@
|
|
|
1
|
-
import { Button, Card, Space, Table, Typography } from "antd";
|
|
2
|
-
import { useFieldArray, useFormContext, type FieldValues } from "react-hook-form";
|
|
3
|
-
import { useMemo, type ReactNode } from "react";
|
|
4
|
-
import type {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from "./
|
|
11
|
-
import {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
const {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
const appendEmpty = () => append(emptyRow(sources));
|
|
40
|
-
|
|
41
|
-
return { fields, remove, appendEmpty };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/** Tabular inline (Django TabularInline) — table with column headers. */
|
|
45
|
-
export function InlineFormSet({
|
|
46
|
-
|
|
47
|
-
label,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const sources = useMemo(
|
|
53
|
-
() => columns.map((col) => col.source),
|
|
54
|
-
[columns],
|
|
55
|
-
);
|
|
56
|
-
const { fields, remove, appendEmpty } = useInlineRows(
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
existingIds?: Identifier[];
|
|
164
|
-
/** Return true when the error was handled (field errors applied). Stops saving further rows. */
|
|
165
|
-
onRowError?: (error: unknown, index: number) => boolean;
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
export async function saveInlineRows(
|
|
169
|
-
dp: DataProvider,
|
|
170
|
-
opts: SaveInlineOptions,
|
|
171
|
-
): Promise<boolean> {
|
|
172
|
-
const { resource, foreignKey, parentId, rows, existingIds = [], onRowError } =
|
|
173
|
-
opts;
|
|
174
|
-
const keptIds: Identifier[] = [];
|
|
175
|
-
|
|
176
|
-
for (let index = 0; index < rows.length; index++) {
|
|
177
|
-
const row = rows[index];
|
|
178
|
-
try {
|
|
179
|
-
const { id: _id, ...rest } = row;
|
|
180
|
-
const data = { ...rest, [foreignKey]: parentId };
|
|
181
|
-
const rowId = row.id as Identifier | undefined;
|
|
182
|
-
if (rowId != null && existingIds.some((e) => e === rowId)) {
|
|
183
|
-
await dp.update(resource, { id: rowId, data });
|
|
184
|
-
keptIds.push(rowId);
|
|
185
|
-
} else {
|
|
186
|
-
const res = await dp.create(resource, data);
|
|
187
|
-
const created = res.data as { id: Identifier };
|
|
188
|
-
keptIds.push(created.id);
|
|
189
|
-
}
|
|
190
|
-
} catch (e) {
|
|
191
|
-
if (onRowError?.(e, index)) return false;
|
|
192
|
-
throw e;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
for (const oldId of existingIds) {
|
|
197
|
-
if (!keptIds.some((k) => k === oldId)) {
|
|
198
|
-
await dp.delete(resource, oldId);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
return true;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
export async function loadInlineRows(
|
|
206
|
-
dp: DataProvider,
|
|
207
|
-
resource: string,
|
|
208
|
-
foreignKey: string,
|
|
209
|
-
parentId: Identifier,
|
|
210
|
-
) {
|
|
211
|
-
const res = await dp.getList(resource, {
|
|
212
|
-
filter: { [foreignKey]: Number(parentId) || parentId },
|
|
213
|
-
pagination: { page: 1, perPage: 500 },
|
|
214
|
-
});
|
|
215
|
-
return {
|
|
216
|
-
rows: res.data as Record<string, unknown>[],
|
|
217
|
-
ids: (res.data as { id: Identifier }[]).map((r) => r.id),
|
|
218
|
-
};
|
|
219
|
-
}
|
|
1
|
+
import { Button, Card, Space, Table, Typography } from "antd";
|
|
2
|
+
import { useFieldArray, useFormContext, type FieldValues } from "react-hook-form";
|
|
3
|
+
import { useMemo, type ReactNode } from "react";
|
|
4
|
+
import type {
|
|
5
|
+
InlineColumnDef,
|
|
6
|
+
InlineFormSetBaseProps,
|
|
7
|
+
InlineRowContext,
|
|
8
|
+
} from "./types";
|
|
9
|
+
import { nestedFieldPath } from "./utils/nestedFieldPath";
|
|
10
|
+
import { useRegisterInlineField } from "./context/InlineFieldsRegistry";
|
|
11
|
+
import { useRegisterPayloadField } from "./context/PayloadFieldsContext";
|
|
12
|
+
|
|
13
|
+
export type InlineFormSetLayout = "tabular" | "stacked";
|
|
14
|
+
|
|
15
|
+
export type InlineFormSetProps = InlineFormSetBaseProps & {
|
|
16
|
+
columns: InlineColumnDef[];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type InlineFormSetStackedProps = InlineFormSetBaseProps & {
|
|
20
|
+
/** Field sources in each row — used when appending an empty row. */
|
|
21
|
+
sources: string[];
|
|
22
|
+
renderRow: (ctx: InlineRowContext) => ReactNode;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function emptyRow(sources: string[]) {
|
|
26
|
+
const row: Record<string, unknown> = {};
|
|
27
|
+
for (const source of sources) row[source] = undefined;
|
|
28
|
+
return row;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function useInlineRows(field: string, sources: string[]) {
|
|
32
|
+
const { control } = useFormContext<FieldValues>();
|
|
33
|
+
const { fields, append, remove } = useFieldArray({
|
|
34
|
+
control,
|
|
35
|
+
name: field,
|
|
36
|
+
keyName: "rowKey",
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const appendEmpty = () => append(emptyRow(sources));
|
|
40
|
+
|
|
41
|
+
return { fields, remove, appendEmpty };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Tabular inline (Django TabularInline) — table with column headers. */
|
|
45
|
+
export function InlineFormSet({
|
|
46
|
+
field,
|
|
47
|
+
label,
|
|
48
|
+
payloadKey,
|
|
49
|
+
transformRows,
|
|
50
|
+
columns,
|
|
51
|
+
}: InlineFormSetProps) {
|
|
52
|
+
const sources = useMemo(
|
|
53
|
+
() => columns.map((col) => col.source),
|
|
54
|
+
[columns],
|
|
55
|
+
);
|
|
56
|
+
const { fields, remove, appendEmpty } = useInlineRows(field, sources);
|
|
57
|
+
|
|
58
|
+
useRegisterPayloadField(field);
|
|
59
|
+
useRegisterInlineField(field, sources, payloadKey, transformRows);
|
|
60
|
+
|
|
61
|
+
const tableColumns = useMemo(
|
|
62
|
+
() =>
|
|
63
|
+
columns.map((col) => ({
|
|
64
|
+
title: col.label ?? col.source,
|
|
65
|
+
key: col.source,
|
|
66
|
+
width: col.width,
|
|
67
|
+
onHeaderCell: () =>
|
|
68
|
+
col.minWidth != null ? { style: { minWidth: col.minWidth } } : {},
|
|
69
|
+
onCell: () =>
|
|
70
|
+
col.minWidth != null ? { style: { minWidth: col.minWidth } } : {},
|
|
71
|
+
render: (_: unknown, __: unknown, index: number) =>
|
|
72
|
+
col.cell({
|
|
73
|
+
name: nestedFieldPath(field, index, col.source),
|
|
74
|
+
index,
|
|
75
|
+
field,
|
|
76
|
+
}),
|
|
77
|
+
})),
|
|
78
|
+
[columns, field],
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<div style={{ marginTop: 24 }}>
|
|
83
|
+
<Typography.Title level={5}>{label ?? "Related items"}</Typography.Title>
|
|
84
|
+
<Table
|
|
85
|
+
size="small"
|
|
86
|
+
pagination={false}
|
|
87
|
+
scroll={{ x: "max-content" }}
|
|
88
|
+
dataSource={fields.map((f) => ({ ...f, key: f.rowKey }))}
|
|
89
|
+
columns={[
|
|
90
|
+
...tableColumns,
|
|
91
|
+
{
|
|
92
|
+
title: "",
|
|
93
|
+
key: "__remove",
|
|
94
|
+
width: 80,
|
|
95
|
+
render: (_: unknown, __: unknown, index: number) => (
|
|
96
|
+
<Button
|
|
97
|
+
type="link"
|
|
98
|
+
danger
|
|
99
|
+
size="small"
|
|
100
|
+
onClick={() => remove(index)}
|
|
101
|
+
>
|
|
102
|
+
Remove
|
|
103
|
+
</Button>
|
|
104
|
+
),
|
|
105
|
+
},
|
|
106
|
+
]}
|
|
107
|
+
/>
|
|
108
|
+
<Button type="dashed" style={{ marginTop: 8 }} onClick={appendEmpty}>
|
|
109
|
+
Add row
|
|
110
|
+
</Button>
|
|
111
|
+
</div>
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Stacked inline (Django StackedInline) — each row in a card with field labels. */
|
|
116
|
+
export function InlineFormSetStacked({
|
|
117
|
+
field,
|
|
118
|
+
label,
|
|
119
|
+
payloadKey,
|
|
120
|
+
transformRows,
|
|
121
|
+
sources,
|
|
122
|
+
renderRow,
|
|
123
|
+
}: InlineFormSetStackedProps) {
|
|
124
|
+
const { fields, remove, appendEmpty } = useInlineRows(field, sources);
|
|
125
|
+
|
|
126
|
+
useRegisterPayloadField(field);
|
|
127
|
+
useRegisterInlineField(field, sources, payloadKey, transformRows);
|
|
128
|
+
|
|
129
|
+
return (
|
|
130
|
+
<div style={{ marginTop: 24 }}>
|
|
131
|
+
<Typography.Title level={5}>{label ?? "Related items"}</Typography.Title>
|
|
132
|
+
<Space orientation="vertical" size="middle" style={{ width: "100%" }}>
|
|
133
|
+
{fields.map((rowField, index) => (
|
|
134
|
+
<Card
|
|
135
|
+
key={rowField.rowKey}
|
|
136
|
+
size="small"
|
|
137
|
+
title={`Item ${index + 1}`}
|
|
138
|
+
extra={
|
|
139
|
+
<Button
|
|
140
|
+
type="link"
|
|
141
|
+
danger
|
|
142
|
+
size="small"
|
|
143
|
+
onClick={() => remove(index)}
|
|
144
|
+
>
|
|
145
|
+
Remove
|
|
146
|
+
</Button>
|
|
147
|
+
}
|
|
148
|
+
>
|
|
149
|
+
{renderRow({
|
|
150
|
+
field,
|
|
151
|
+
index,
|
|
152
|
+
name: (source) => nestedFieldPath(field, index, source),
|
|
153
|
+
})}
|
|
154
|
+
</Card>
|
|
155
|
+
))}
|
|
156
|
+
</Space>
|
|
157
|
+
<Button type="dashed" style={{ marginTop: 8 }} onClick={appendEmpty}>
|
|
158
|
+
Add item
|
|
159
|
+
</Button>
|
|
160
|
+
</div>
|
|
161
|
+
);
|
|
162
|
+
}
|