ding-react-admin 2.0.0 → 2.0.5
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 -195
- package/dist/index.css +1 -0
- package/dist/index.js +1773 -1170
- 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/ResourceForm.d.ts +1 -4
- 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/ResourceRecordForm.d.ts +27 -0
- package/dist/src/crud/ResourceRecordForm.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/filters/ReferenceFilter.d.ts +1 -1
- package/dist/src/crud/filters/ReferenceFilter.d.ts.map +1 -1
- package/dist/src/crud/index.d.ts +5 -0
- package/dist/src/crud/index.d.ts.map +1 -1
- package/dist/src/crud/types.d.ts +12 -0
- package/dist/src/crud/types.d.ts.map +1 -1
- 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/prepareFormSubmitBody.d.ts +1 -0
- package/dist/src/crud/utils/prepareFormSubmitBody.d.ts.map +1 -1
- 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/toFormData.d.ts +1 -1
- package/dist/src/crud/utils/toFormData.d.ts.map +1 -1
- 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 +9 -7
- package/dist/src/crud/utils/useFormRecord.d.ts.map +1 -1
- package/dist/src/data/parseFormErrorHelpers.d.ts +6 -27
- package/dist/src/data/parseFormErrorHelpers.d.ts.map +1 -1
- package/dist/src/index.d.ts +2 -2
- 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 +85 -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 -162
- package/src/crud/ListActionsBar.tsx +106 -106
- package/src/crud/ResourceForm.tsx +83 -160
- package/src/crud/ResourceFormModal.tsx +75 -133
- 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 -58
- 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 -61
- package/src/crud/context/ListContext.tsx +93 -93
- package/src/crud/fields/BooleanField.tsx +38 -38
- package/src/crud/fields/DateField.tsx +53 -53
- package/src/crud/fields/FileField.tsx +123 -123
- package/src/crud/fields/ImageField.tsx +120 -120
- 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 -21
- package/src/crud/fields/useUploadPreviewUrl.ts +20 -20
- 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 -94
- package/src/crud/types.ts +146 -134
- package/src/crud/utils/buildFormPayload.ts +24 -24
- package/src/crud/utils/buildInlineRowsPayload.ts +37 -37
- package/src/crud/utils/buildResourceFormSubmitBody.ts +29 -29
- 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 -42
- package/src/crud/utils/formSectionErrors.ts +85 -85
- package/src/crud/utils/getFormValue.ts +13 -13
- package/src/crud/utils/hasUploadValues.ts +9 -9
- package/src/crud/utils/nestedFieldPath.ts +8 -8
- package/src/crud/utils/prepareFormSubmitBody.ts +22 -17
- package/src/crud/utils/referenceSelectNotFoundContent.tsx +7 -0
- package/src/crud/utils/setFormValue.ts +18 -18
- package/src/crud/utils/sortQueryParam.ts +37 -37
- package/src/crud/utils/toFormData.ts +79 -81
- 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 -156
- 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 -63
- package/src/data/dataUtils.test.ts +228 -220
- package/src/data/inMemoryList.ts +97 -97
- package/src/data/parseFormErrorHelpers.test.ts +201 -138
- package/src/data/parseFormErrorHelpers.ts +376 -311
- package/src/data/sortHelpers.ts +48 -48
- package/src/index.ts +211 -207
- package/src/layouts/AdminLayout.tsx +530 -382
- 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
|
@@ -1,182 +1,330 @@
|
|
|
1
|
-
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
-
import { useDataProvider } from "../../context/DataProvider";
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
})
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function
|
|
50
|
-
effectiveLoader: ChoicesLoader,
|
|
51
|
-
dp: ReturnType<typeof useDataProvider>,
|
|
52
|
-
optionLabel: string | ((record: Record<string, unknown>) => string),
|
|
53
|
-
optionValue: string,
|
|
54
|
-
search?: string,
|
|
55
|
-
): Promise<ChoiceOption[]> {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import { useDataProvider } from "../../context/DataProvider";
|
|
3
|
+
import type { Identifier } from "../../data/dataProviderTypes";
|
|
4
|
+
import type { ChoiceOption, ChoicesLoader } from "../types";
|
|
5
|
+
import {
|
|
6
|
+
collectSelectedRecords,
|
|
7
|
+
mergeOptions,
|
|
8
|
+
normalizeSelectedIds,
|
|
9
|
+
recordsToOptions,
|
|
10
|
+
resolveOptionLabel,
|
|
11
|
+
} from "./choiceSelectionUtils";
|
|
12
|
+
|
|
13
|
+
const choicesCache = new Map<string, ChoiceOption[]>();
|
|
14
|
+
const choicesInflight = new Map<string, Promise<ChoiceOption[]>>();
|
|
15
|
+
|
|
16
|
+
export type UseChoicesOptions = {
|
|
17
|
+
/** When true, the option list loads only while the dropdown is open or the user is searching. */
|
|
18
|
+
lazy?: boolean;
|
|
19
|
+
/** Dropdown open or active search — triggers lazy list loading. */
|
|
20
|
+
active?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Current field value — primitive id(s), nested object(s), or mixed.
|
|
23
|
+
* Objects shaped like `{ id, name, … }` are turned into options without `getOne`.
|
|
24
|
+
*/
|
|
25
|
+
selectedValues?: unknown | unknown[];
|
|
26
|
+
/**
|
|
27
|
+
* Related record(s) from the loaded form row (e.g. `recordSource="branch"`).
|
|
28
|
+
* Used to show labels on edit when the retrieve API embeds relations.
|
|
29
|
+
*/
|
|
30
|
+
selectedRecords?: Record<string, unknown> | Record<string, unknown>[];
|
|
31
|
+
/**
|
|
32
|
+
* When true (default), fetch labels for primitive ids via `getOne` if they are
|
|
33
|
+
* not already known from `selectedValues` / `selectedRecords`.
|
|
34
|
+
*/
|
|
35
|
+
fetchSelected?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Cache list results in memory. Defaults to `false` when `lazy` is true so each
|
|
38
|
+
* dropdown open refetches; eager loads (e.g. table columns) default to `true`.
|
|
39
|
+
*/
|
|
40
|
+
cache?: boolean;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function loaderKey(loader: ChoicesLoader, search?: string): string {
|
|
44
|
+
if (typeof loader === "function") return `fn:${search ?? ""}`;
|
|
45
|
+
if (Array.isArray(loader)) return `static:${loader.length}`;
|
|
46
|
+
return `res:${loader.resource}:${JSON.stringify(loader.filter ?? {})}:${search ?? ""}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function fetchChoices(
|
|
50
|
+
effectiveLoader: ChoicesLoader,
|
|
51
|
+
dp: ReturnType<typeof useDataProvider>,
|
|
52
|
+
optionLabel: string | ((record: Record<string, unknown>) => string),
|
|
53
|
+
optionValue: string,
|
|
54
|
+
search?: string,
|
|
55
|
+
): Promise<ChoiceOption[]> {
|
|
56
|
+
if (typeof effectiveLoader === "function") {
|
|
57
|
+
return effectiveLoader({ dataProvider: dp, search });
|
|
58
|
+
}
|
|
59
|
+
if (Array.isArray(effectiveLoader)) {
|
|
60
|
+
return effectiveLoader;
|
|
61
|
+
}
|
|
62
|
+
const res = await dp.getList(effectiveLoader.resource, {
|
|
63
|
+
filter: {
|
|
64
|
+
...effectiveLoader.filter,
|
|
65
|
+
...(search ? { q: search } : {}),
|
|
66
|
+
},
|
|
67
|
+
pagination: { page: 1, perPage: 500 },
|
|
68
|
+
});
|
|
69
|
+
return (res.data as Record<string, unknown>[]).map((row) => ({
|
|
70
|
+
label: resolveOptionLabel(row, optionLabel),
|
|
71
|
+
value: row[optionValue],
|
|
72
|
+
record: row,
|
|
73
|
+
}));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function loadChoicesShared(
|
|
77
|
+
effectiveLoader: ChoicesLoader,
|
|
78
|
+
dp: ReturnType<typeof useDataProvider>,
|
|
79
|
+
optionLabel: string | ((record: Record<string, unknown>) => string),
|
|
80
|
+
optionValue: string,
|
|
81
|
+
search: string | undefined,
|
|
82
|
+
useCache: boolean,
|
|
83
|
+
): Promise<ChoiceOption[]> {
|
|
84
|
+
const key = loaderKey(effectiveLoader, search);
|
|
85
|
+
if (useCache) {
|
|
86
|
+
const cached = choicesCache.get(key);
|
|
87
|
+
if (cached && !search) {
|
|
88
|
+
return Promise.resolve(cached);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const inflight = choicesInflight.get(key);
|
|
93
|
+
if (inflight) return inflight;
|
|
94
|
+
|
|
95
|
+
const promise = fetchChoices(
|
|
96
|
+
effectiveLoader,
|
|
97
|
+
dp,
|
|
98
|
+
optionLabel,
|
|
99
|
+
optionValue,
|
|
100
|
+
search,
|
|
101
|
+
)
|
|
102
|
+
.then((result) => {
|
|
103
|
+
if (useCache && !search) choicesCache.set(key, result);
|
|
104
|
+
return result;
|
|
105
|
+
})
|
|
106
|
+
.finally(() => {
|
|
107
|
+
choicesInflight.delete(key);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
choicesInflight.set(key, promise);
|
|
111
|
+
return promise;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function useChoices(
|
|
115
|
+
loader: ChoicesLoader | undefined,
|
|
116
|
+
reference: string | undefined,
|
|
117
|
+
optionLabel: string | ((record: Record<string, unknown>) => string) = "name",
|
|
118
|
+
optionValue = "id",
|
|
119
|
+
search?: string,
|
|
120
|
+
hookOptions: UseChoicesOptions = {},
|
|
121
|
+
) {
|
|
122
|
+
const {
|
|
123
|
+
lazy = false,
|
|
124
|
+
active = false,
|
|
125
|
+
selectedValues,
|
|
126
|
+
selectedRecords,
|
|
127
|
+
fetchSelected = true,
|
|
128
|
+
cache: cacheOption,
|
|
129
|
+
} = hookOptions;
|
|
130
|
+
const useCache = cacheOption ?? !lazy;
|
|
131
|
+
const dp = useDataProvider();
|
|
132
|
+
|
|
133
|
+
const effectiveLoader = useMemo((): ChoicesLoader | undefined => {
|
|
134
|
+
if (loader) return loader;
|
|
135
|
+
if (!reference) return undefined;
|
|
136
|
+
return {
|
|
137
|
+
resource: reference,
|
|
138
|
+
filter: search ? { q: search } : undefined,
|
|
139
|
+
};
|
|
140
|
+
}, [loader, reference, search]);
|
|
141
|
+
|
|
142
|
+
const cacheKey = effectiveLoader
|
|
143
|
+
? loaderKey(effectiveLoader, search)
|
|
144
|
+
: undefined;
|
|
145
|
+
|
|
146
|
+
const normalizedSelected = useMemo(
|
|
147
|
+
() => normalizeSelectedIds(selectedValues, optionValue),
|
|
148
|
+
[selectedValues, optionValue],
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const embeddedOptions = useMemo(
|
|
152
|
+
() =>
|
|
153
|
+
recordsToOptions(
|
|
154
|
+
collectSelectedRecords(selectedValues, selectedRecords, optionValue),
|
|
155
|
+
optionLabel,
|
|
156
|
+
optionValue,
|
|
157
|
+
),
|
|
158
|
+
[selectedValues, selectedRecords, optionLabel, optionValue],
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
const shouldLoadList = Boolean(
|
|
162
|
+
effectiveLoader && (!lazy || active || Array.isArray(effectiveLoader)),
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
const [options, setOptions] = useState<ChoiceOption[]>(() => {
|
|
166
|
+
if (embeddedOptions.length) return embeddedOptions;
|
|
167
|
+
if (!cacheKey || search || lazy || !useCache) return [];
|
|
168
|
+
return choicesCache.get(cacheKey) ?? [];
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const [loading, setLoading] = useState(() => {
|
|
172
|
+
if (!shouldLoadList) return false;
|
|
173
|
+
if (!useCache || !cacheKey || search) return Boolean(effectiveLoader);
|
|
174
|
+
return !choicesCache.has(cacheKey);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
if (!embeddedOptions.length) return;
|
|
179
|
+
setOptions((prev) => mergeOptions(prev, embeddedOptions));
|
|
180
|
+
}, [embeddedOptions]);
|
|
181
|
+
|
|
182
|
+
const load = useCallback(async () => {
|
|
183
|
+
if (!effectiveLoader || !shouldLoadList) {
|
|
184
|
+
if (!effectiveLoader) {
|
|
185
|
+
setOptions(embeddedOptions);
|
|
186
|
+
}
|
|
187
|
+
setLoading(false);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (useCache) {
|
|
192
|
+
const key = loaderKey(effectiveLoader, search);
|
|
193
|
+
const cached = choicesCache.get(key);
|
|
194
|
+
if (cached && !search) {
|
|
195
|
+
setOptions(mergeOptions(embeddedOptions, cached));
|
|
196
|
+
setLoading(false);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
setLoading(true);
|
|
202
|
+
if (lazy) {
|
|
203
|
+
setOptions(embeddedOptions);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
const result = await loadChoicesShared(
|
|
208
|
+
effectiveLoader,
|
|
209
|
+
dp,
|
|
210
|
+
optionLabel,
|
|
211
|
+
optionValue,
|
|
212
|
+
search,
|
|
213
|
+
useCache,
|
|
214
|
+
);
|
|
215
|
+
setOptions(mergeOptions(embeddedOptions, result));
|
|
216
|
+
} catch {
|
|
217
|
+
if (!normalizedSelected.length && !embeddedOptions.length) {
|
|
218
|
+
setOptions([]);
|
|
219
|
+
} else if (lazy) {
|
|
220
|
+
setOptions(embeddedOptions);
|
|
221
|
+
}
|
|
222
|
+
} finally {
|
|
223
|
+
setLoading(false);
|
|
224
|
+
}
|
|
225
|
+
}, [
|
|
226
|
+
effectiveLoader,
|
|
227
|
+
shouldLoadList,
|
|
228
|
+
useCache,
|
|
229
|
+
dp,
|
|
230
|
+
optionLabel,
|
|
231
|
+
optionValue,
|
|
232
|
+
search,
|
|
233
|
+
lazy,
|
|
234
|
+
normalizedSelected.length,
|
|
235
|
+
embeddedOptions,
|
|
236
|
+
]);
|
|
237
|
+
|
|
238
|
+
useEffect(() => {
|
|
239
|
+
void load();
|
|
240
|
+
}, [load]);
|
|
241
|
+
|
|
242
|
+
useEffect(() => {
|
|
243
|
+
if (lazy && !active && !search) {
|
|
244
|
+
setOptions(embeddedOptions);
|
|
245
|
+
setLoading(false);
|
|
246
|
+
}
|
|
247
|
+
}, [lazy, active, search, embeddedOptions]);
|
|
248
|
+
|
|
249
|
+
useEffect(() => {
|
|
250
|
+
if (!fetchSelected || !reference || !normalizedSelected.length) return;
|
|
251
|
+
|
|
252
|
+
const idsToFetch = normalizedSelected.filter(
|
|
253
|
+
(value) => !embeddedOptions.some((option) => option.value === value),
|
|
254
|
+
);
|
|
255
|
+
if (!idsToFetch.length) return;
|
|
256
|
+
|
|
257
|
+
let cancelled = false;
|
|
258
|
+
|
|
259
|
+
void (async () => {
|
|
260
|
+
const resolved: ChoiceOption[] = [];
|
|
261
|
+
for (const value of idsToFetch) {
|
|
262
|
+
try {
|
|
263
|
+
const result = await dp.getOne(reference, value as Identifier);
|
|
264
|
+
const row = result.data as Record<string, unknown>;
|
|
265
|
+
resolved.push({
|
|
266
|
+
label: resolveOptionLabel(row, optionLabel),
|
|
267
|
+
value: row[optionValue],
|
|
268
|
+
record: row,
|
|
269
|
+
});
|
|
270
|
+
} catch {
|
|
271
|
+
resolved.push({
|
|
272
|
+
label: String(value),
|
|
273
|
+
value,
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
if (cancelled || !resolved.length) return;
|
|
278
|
+
|
|
279
|
+
setOptions((prev) => {
|
|
280
|
+
const missing = resolved.filter(
|
|
281
|
+
(option) => !prev.some((existing) => existing.value === option.value),
|
|
282
|
+
);
|
|
283
|
+
return missing.length ? mergeOptions(prev, missing) : prev;
|
|
284
|
+
});
|
|
285
|
+
})();
|
|
286
|
+
|
|
287
|
+
return () => {
|
|
288
|
+
cancelled = true;
|
|
289
|
+
};
|
|
290
|
+
}, [
|
|
291
|
+
fetchSelected,
|
|
292
|
+
reference,
|
|
293
|
+
dp,
|
|
294
|
+
optionLabel,
|
|
295
|
+
optionValue,
|
|
296
|
+
normalizedSelected,
|
|
297
|
+
embeddedOptions,
|
|
298
|
+
]);
|
|
299
|
+
|
|
300
|
+
const labelForValue = useCallback(
|
|
301
|
+
(value: unknown): string => {
|
|
302
|
+
const opt = options.find((o) => o.value === value);
|
|
303
|
+
return opt?.label ?? String(value ?? "—");
|
|
304
|
+
},
|
|
305
|
+
[options],
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
const labelsForValues = useCallback(
|
|
309
|
+
(values: unknown[]): string => {
|
|
310
|
+
if (!values?.length) return "—";
|
|
311
|
+
return values.map((v) => labelForValue(v)).join(", ");
|
|
312
|
+
},
|
|
313
|
+
[labelForValue],
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
const optionForValue = useCallback(
|
|
317
|
+
(value: unknown): ChoiceOption | undefined =>
|
|
318
|
+
options.find((o) => o.value === value),
|
|
319
|
+
[options],
|
|
320
|
+
);
|
|
321
|
+
|
|
322
|
+
return {
|
|
323
|
+
options,
|
|
324
|
+
loading,
|
|
325
|
+
labelForValue,
|
|
326
|
+
labelsForValues,
|
|
327
|
+
optionForValue,
|
|
328
|
+
reload: load,
|
|
329
|
+
};
|
|
330
|
+
}
|