@payloadcms/ui 3.0.0-beta.90 → 3.0.0-beta.92

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.
Files changed (67) hide show
  1. package/dist/elements/AddNewRelation/index.d.ts.map +1 -1
  2. package/dist/elements/AddNewRelation/index.js +1 -1
  3. package/dist/elements/AddNewRelation/index.js.map +1 -1
  4. package/dist/elements/AddNewRelation/index.scss +14 -4
  5. package/dist/elements/AppHeader/index.js +41 -80
  6. package/dist/elements/AppHeader/index.js.map +1 -1
  7. package/dist/elements/Autosave/index.js +148 -231
  8. package/dist/elements/Autosave/index.js.map +1 -1
  9. package/dist/elements/Button/index.js +86 -218
  10. package/dist/elements/Button/index.js.map +1 -1
  11. package/dist/elements/Collapsible/index.js +44 -62
  12. package/dist/elements/Collapsible/index.js.map +1 -1
  13. package/dist/elements/Collapsible/index.scss +15 -28
  14. package/dist/elements/CopyToClipboard/index.js +20 -54
  15. package/dist/elements/CopyToClipboard/index.js.map +1 -1
  16. package/dist/elements/Dropzone/index.js +59 -119
  17. package/dist/elements/Dropzone/index.js.map +1 -1
  18. package/dist/elements/EditMany/index.js +124 -230
  19. package/dist/elements/EditMany/index.js.map +1 -1
  20. package/dist/elements/EditUpload/index.js +133 -206
  21. package/dist/elements/EditUpload/index.js.map +1 -1
  22. package/dist/elements/ListControls/index.js +82 -188
  23. package/dist/elements/ListControls/index.js.map +1 -1
  24. package/dist/elements/Nav/context.js +47 -83
  25. package/dist/elements/Nav/context.js.map +1 -1
  26. package/dist/elements/SaveButton/index.js +30 -81
  27. package/dist/elements/SaveButton/index.js.map +1 -1
  28. package/dist/elements/SaveDraftButton/index.js +54 -116
  29. package/dist/elements/SaveDraftButton/index.js.map +1 -1
  30. package/dist/elements/SearchFilter/index.js +21 -77
  31. package/dist/elements/SearchFilter/index.js.map +1 -1
  32. package/dist/elements/Upload/index.js +130 -280
  33. package/dist/elements/Upload/index.js.map +1 -1
  34. package/dist/exports/client/{CodeEditor-C4FLUUO7.js → CodeEditor-MTP455NO.js} +2 -2
  35. package/dist/exports/client/chunk-NJHSEGFB.js +14 -0
  36. package/dist/exports/client/{chunk-UGBVRIVY.js.map → chunk-NJHSEGFB.js.map} +2 -2
  37. package/dist/exports/client/index.d.ts +1 -1
  38. package/dist/exports/client/index.d.ts.map +1 -1
  39. package/dist/exports/client/index.js +19 -19
  40. package/dist/exports/client/index.js.map +3 -3
  41. package/dist/fields/Relationship/index.js +318 -504
  42. package/dist/fields/Relationship/index.js.map +1 -1
  43. package/dist/fields/Upload/HasMany/index.d.ts.map +1 -1
  44. package/dist/fields/Upload/HasMany/index.js +192 -422
  45. package/dist/fields/Upload/HasMany/index.js.map +1 -1
  46. package/dist/fields/Upload/HasMany/index.scss +19 -1
  47. package/dist/forms/Submit/index.js +12 -28
  48. package/dist/forms/Submit/index.js.map +1 -1
  49. package/dist/hooks/useDelay.js +15 -48
  50. package/dist/hooks/useDelay.js.map +1 -1
  51. package/dist/hooks/useDelayedRender.js +32 -71
  52. package/dist/hooks/useDelayedRender.js.map +1 -1
  53. package/dist/hooks/useIntersect.js +14 -20
  54. package/dist/hooks/useIntersect.js.map +1 -1
  55. package/dist/providers/Config/createClientConfig/collections.d.ts.map +1 -1
  56. package/dist/providers/Config/createClientConfig/collections.js +8 -6
  57. package/dist/providers/Config/createClientConfig/collections.js.map +1 -1
  58. package/dist/providers/Preferences/index.js +1 -3
  59. package/dist/providers/Preferences/index.js.map +1 -1
  60. package/dist/providers/Selection/index.js +102 -190
  61. package/dist/providers/Selection/index.js.map +1 -1
  62. package/dist/scss/app.scss +6 -6
  63. package/dist/scss/type.scss +4 -4
  64. package/dist/styles.css +1 -1
  65. package/package.json +9 -9
  66. package/dist/exports/client/chunk-UGBVRIVY.js +0 -14
  67. /package/dist/exports/client/{CodeEditor-C4FLUUO7.js.map → CodeEditor-MTP455NO.js.map} +0 -0
@@ -1,6 +1,5 @@
1
1
  'use client';
2
2
 
3
- import { c as _c } from "react/compiler-runtime";
4
3
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
5
4
  import { wordBoundariesRegex } from 'payload/shared';
6
5
  import * as qs from 'qs-esm';
@@ -28,57 +27,43 @@ import { SingleValue } from './select-components/SingleValue/index.js';
28
27
  const maxResultsPerRequest = 10;
29
28
  const baseClass = 'relationship';
30
29
  const RelationshipFieldComponent = props => {
31
- const $ = _c(76);
32
30
  const {
33
31
  descriptionProps,
34
32
  errorProps,
35
33
  field,
36
- field: t0,
34
+ field: {
35
+ name,
36
+ _path: pathFromProps,
37
+ admin: {
38
+ allowCreate = true,
39
+ className,
40
+ description,
41
+ isSortable = true,
42
+ readOnly: readOnlyFromAdmin,
43
+ sortOptions,
44
+ style,
45
+ width
46
+ } = {},
47
+ hasMany,
48
+ label,
49
+ relationTo,
50
+ required
51
+ },
37
52
  labelProps,
38
53
  readOnly: readOnlyFromTopLevelProps,
39
54
  validate
40
55
  } = props;
41
- const {
42
- name,
43
- _path: pathFromProps,
44
- admin: t1,
45
- hasMany,
46
- label,
47
- relationTo,
48
- required
49
- } = t0;
50
- let t2;
51
- if ($[0] !== t1) {
52
- t2 = t1 === undefined ? {} : t1;
53
- $[0] = t1;
54
- $[1] = t2;
55
- } else {
56
- t2 = $[1];
57
- }
58
- const {
59
- allowCreate: t3,
60
- className,
61
- description,
62
- isSortable: t4,
63
- readOnly: readOnlyFromAdmin,
64
- sortOptions,
65
- style,
66
- width
67
- } = t2;
68
- const allowCreate = t3 === undefined ? true : t3;
69
- const isSortable = t4 === undefined ? true : t4;
70
56
  const readOnlyFromProps = readOnlyFromTopLevelProps || readOnlyFromAdmin;
71
57
  const {
72
58
  config
73
59
  } = useConfig();
74
60
  const {
75
61
  collections,
76
- routes: t5,
62
+ routes: {
63
+ api
64
+ },
77
65
  serverURL
78
66
  } = config;
79
- const {
80
- api
81
- } = t5;
82
67
  const {
83
68
  i18n,
84
69
  t
@@ -90,63 +75,27 @@ const RelationshipFieldComponent = props => {
90
75
  code: locale
91
76
  } = useLocale();
92
77
  const hasMultipleRelations = Array.isArray(relationTo);
93
- let t6;
94
- if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
95
- t6 = [];
96
- $[2] = t6;
97
- } else {
98
- t6 = $[2];
99
- }
100
- const [options, dispatchOptions] = useReducer(optionsReducer, t6);
78
+ const [options, dispatchOptions] = useReducer(optionsReducer, []);
101
79
  const [lastFullyLoadedRelation, setLastFullyLoadedRelation] = useState(-1);
102
- let t7;
103
- if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
104
- t7 = {};
105
- $[3] = t7;
106
- } else {
107
- t7 = $[3];
108
- }
109
- const [lastLoadedPage, setLastLoadedPage] = useState(t7);
110
- const [errorLoading, setErrorLoading] = useState("");
111
- const [search, setSearch] = useState("");
80
+ const [lastLoadedPage, setLastLoadedPage] = useState({});
81
+ const [errorLoading, setErrorLoading] = useState('');
82
+ const [search, setSearch] = useState('');
112
83
  const [isLoading, setIsLoading] = useState(false);
113
84
  const [enableWordBoundarySearch, setEnableWordBoundarySearch] = useState(false);
114
85
  const menuIsOpen = useRef(false);
115
86
  const hasLoadedFirstPageRef = useRef(false);
116
- let t8;
117
- if ($[4] !== validate || $[5] !== required) {
118
- t8 = (value, validationOptions) => {
119
- if (typeof validate === "function") {
120
- return validate(value, {
121
- ...validationOptions,
122
- required
123
- });
124
- }
125
- };
126
- $[4] = validate;
127
- $[5] = required;
128
- $[6] = t8;
129
- } else {
130
- t8 = $[6];
131
- }
132
- const memoizedValidate = t8;
87
+ const memoizedValidate = useCallback((value, validationOptions) => {
88
+ if (typeof validate === 'function') {
89
+ return validate(value, {
90
+ ...validationOptions,
91
+ required
92
+ });
93
+ }
94
+ }, [validate, required]);
133
95
  const {
134
96
  path: pathFromContext,
135
97
  readOnly: readOnlyFromContext
136
98
  } = useFieldProps();
137
- const t9 = pathFromContext ?? pathFromProps ?? name;
138
- let t10;
139
- if ($[7] !== t9 || $[8] !== memoizedValidate) {
140
- t10 = {
141
- path: t9,
142
- validate: memoizedValidate
143
- };
144
- $[7] = t9;
145
- $[8] = memoizedValidate;
146
- $[9] = t10;
147
- } else {
148
- t10 = $[9];
149
- }
150
99
  const {
151
100
  filterOptions,
152
101
  formInitializing,
@@ -156,305 +105,229 @@ const RelationshipFieldComponent = props => {
156
105
  setValue,
157
106
  showError,
158
107
  value: value_0
159
- } = useField(t10);
108
+ } = useField({
109
+ path: pathFromContext ?? pathFromProps ?? name,
110
+ validate: memoizedValidate
111
+ });
160
112
  const readOnly = readOnlyFromProps || readOnlyFromContext || formInitializing;
161
113
  const valueRef = useRef(value_0);
162
114
  valueRef.current = value_0;
163
115
  const [drawerIsOpen, setDrawerIsOpen] = useState(false);
164
- let t11;
165
- if ($[10] !== permissions || $[11] !== relationTo || $[12] !== hasMany || $[13] !== errorLoading || $[14] !== filterOptions || $[15] !== search || $[16] !== collections || $[17] !== sortOptions || $[18] !== locale || $[19] !== serverURL || $[20] !== api || $[21] !== i18n || $[22] !== config || $[23] !== t) {
166
- t11 = async t12 => {
167
- const {
168
- lastFullyLoadedRelation: lastFullyLoadedRelationArg,
169
- lastLoadedPage: lastLoadedPageArg,
170
- onSuccess,
171
- search: searchArg,
172
- sort,
173
- value: valueArg
174
- } = t12;
175
- if (!permissions) {
176
- return;
177
- }
178
- const lastFullyLoadedRelationToUse = typeof lastFullyLoadedRelationArg !== "undefined" ? lastFullyLoadedRelationArg : -1;
179
- const relations = Array.isArray(relationTo) ? relationTo : [relationTo];
180
- const relationsToFetch = lastFullyLoadedRelationToUse === -1 ? relations : relations.slice(lastFullyLoadedRelationToUse + 1);
181
- let resultsFetched;
182
- resultsFetched = 0;
183
- const relationMap = createRelationMap({
184
- hasMany,
185
- relationTo,
186
- value: valueArg
187
- });
188
- if (!errorLoading) {
189
- await relationsToFetch.reduce(async (priorRelation, relation) => {
190
- const relationFilterOption = filterOptions?.[relation];
191
- let lastLoadedPageToUse;
192
- if (search !== searchArg) {
193
- lastLoadedPageToUse = 1;
194
- } else {
195
- lastLoadedPageToUse = lastLoadedPageArg[relation] + 1;
196
- }
197
- await priorRelation;
198
- if (relationFilterOption === false) {
199
- setLastFullyLoadedRelation(relations.indexOf(relation));
200
- return Promise.resolve();
201
- }
202
- if (resultsFetched < 10) {
203
- const collection = collections.find(coll => coll.slug === relation);
204
- const fieldToSearch = collection?.admin?.useAsTitle || "id";
205
- let fieldToSort = collection?.defaultSort || "id";
206
- if (typeof sortOptions === "string") {
207
- fieldToSort = sortOptions;
208
- } else {
209
- if (sortOptions?.[relation]) {
210
- fieldToSort = sortOptions[relation];
211
- }
212
- }
213
- const query = {
214
- depth: 0,
215
- draft: true,
216
- limit: maxResultsPerRequest,
217
- locale,
218
- page: lastLoadedPageToUse,
219
- sort: fieldToSort,
220
- where: {
221
- and: [{
222
- id: {
223
- not_in: relationMap[relation]
224
- }
225
- }]
226
- }
227
- };
228
- if (searchArg) {
229
- query.where.and.push({
230
- [fieldToSearch]: {
231
- like: searchArg
232
- }
233
- });
234
- }
235
- if (relationFilterOption && typeof relationFilterOption !== "boolean") {
236
- query.where.and.push(relationFilterOption);
237
- }
238
- const response = await fetch(`${serverURL}${api}/${relation}`, {
239
- body: qs.stringify(query),
240
- credentials: "include",
241
- headers: {
242
- "Accept-Language": i18n.language,
243
- "Content-Type": "application/x-www-form-urlencoded",
244
- "X-HTTP-Method-Override": "GET"
245
- },
246
- method: "POST"
247
- });
248
- if (response.ok) {
249
- const data = await response.json();
250
- setLastLoadedPage(prevState => ({
251
- ...prevState,
252
- [relation]: lastLoadedPageToUse
253
- }));
254
- if (!data.nextPage) {
255
- setLastFullyLoadedRelation(relations.indexOf(relation));
256
- }
257
- if (data.docs.length > 0) {
258
- resultsFetched = resultsFetched + data.docs.length;
259
- resultsFetched;
260
- dispatchOptions({
261
- type: "ADD",
262
- collection,
263
- config,
264
- docs: data.docs,
265
- i18n,
266
- sort
267
- });
268
- }
269
- } else {
270
- if (response.status === 403) {
271
- setLastFullyLoadedRelation(relations.indexOf(relation));
272
- dispatchOptions({
273
- type: "ADD",
274
- collection,
275
- config,
276
- docs: [],
277
- i18n,
278
- ids: relationMap[relation],
279
- sort
280
- });
281
- } else {
282
- setErrorLoading(t("error:unspecific"));
283
- }
284
- }
285
- }
286
- }, Promise.resolve());
287
- if (typeof onSuccess === "function") {
288
- onSuccess();
116
+ const getResults = useCallback(async ({
117
+ lastFullyLoadedRelation: lastFullyLoadedRelationArg,
118
+ lastLoadedPage: lastLoadedPageArg,
119
+ onSuccess,
120
+ search: searchArg,
121
+ sort,
122
+ value: valueArg
123
+ }) => {
124
+ if (!permissions) {
125
+ return;
126
+ }
127
+ const lastFullyLoadedRelationToUse = typeof lastFullyLoadedRelationArg !== 'undefined' ? lastFullyLoadedRelationArg : -1;
128
+ const relations = Array.isArray(relationTo) ? relationTo : [relationTo];
129
+ const relationsToFetch = lastFullyLoadedRelationToUse === -1 ? relations : relations.slice(lastFullyLoadedRelationToUse + 1);
130
+ let resultsFetched = 0;
131
+ const relationMap = createRelationMap({
132
+ hasMany,
133
+ relationTo,
134
+ value: valueArg
135
+ });
136
+ if (!errorLoading) {
137
+ await relationsToFetch.reduce(async (priorRelation, relation) => {
138
+ const relationFilterOption = filterOptions?.[relation];
139
+ let lastLoadedPageToUse;
140
+ if (search !== searchArg) {
141
+ lastLoadedPageToUse = 1;
142
+ } else {
143
+ lastLoadedPageToUse = lastLoadedPageArg[relation] + 1;
289
144
  }
290
- }
291
- };
292
- $[10] = permissions;
293
- $[11] = relationTo;
294
- $[12] = hasMany;
295
- $[13] = errorLoading;
296
- $[14] = filterOptions;
297
- $[15] = search;
298
- $[16] = collections;
299
- $[17] = sortOptions;
300
- $[18] = locale;
301
- $[19] = serverURL;
302
- $[20] = api;
303
- $[21] = i18n;
304
- $[22] = config;
305
- $[23] = t;
306
- $[24] = t11;
307
- } else {
308
- t11 = $[24];
309
- }
310
- const getResults = t11;
311
- let t12;
312
- if ($[25] !== getResults) {
313
- t12 = (searchArg_0, valueArg_0) => {
314
- getResults({
315
- lastLoadedPage: {},
316
- search: searchArg_0,
317
- sort: true,
318
- value: valueArg_0
319
- });
320
- setSearch(searchArg_0);
321
- };
322
- $[25] = getResults;
323
- $[26] = t12;
324
- } else {
325
- t12 = $[26];
326
- }
327
- const updateSearch = useDebouncedCallback(t12, 300);
328
- let t13;
329
- if ($[27] !== search || $[28] !== updateSearch) {
330
- t13 = (searchArg_1, valueArg_1) => {
331
- if (search !== searchArg_1) {
332
- setLastLoadedPage({});
333
- updateSearch(searchArg_1, valueArg_1, searchArg_1 !== "");
334
- }
335
- };
336
- $[27] = search;
337
- $[28] = updateSearch;
338
- $[29] = t13;
339
- } else {
340
- t13 = $[29];
341
- }
342
- const handleInputChange = t13;
343
- let t14;
344
- if ($[30] !== hasMany || $[31] !== relationTo || $[32] !== value_0 || $[33] !== options || $[34] !== locale || $[35] !== errorLoading || $[36] !== serverURL || $[37] !== api || $[38] !== i18n || $[39] !== collections || $[40] !== config) {
345
- t14 = () => {
346
- const relationMap_0 = createRelationMap({
347
- hasMany,
348
- relationTo,
349
- value: value_0
350
- });
351
- Object.entries(relationMap_0).reduce(async (priorRelation_0, t15) => {
352
- const [relation_0, ids] = t15;
353
- await priorRelation_0;
354
- const idsToLoad = ids.filter(id => !options.find(optionGroup => optionGroup?.options?.find(option => option.value === id && option.relationTo === relation_0)));
355
- if (idsToLoad.length > 0) {
356
- const query_0 = {
145
+ await priorRelation;
146
+ if (relationFilterOption === false) {
147
+ setLastFullyLoadedRelation(relations.indexOf(relation));
148
+ return Promise.resolve();
149
+ }
150
+ if (resultsFetched < 10) {
151
+ const collection = collections.find(coll => coll.slug === relation);
152
+ const fieldToSearch = collection?.admin?.useAsTitle || 'id';
153
+ let fieldToSort = collection?.defaultSort || 'id';
154
+ if (typeof sortOptions === 'string') {
155
+ fieldToSort = sortOptions;
156
+ } else if (sortOptions?.[relation]) {
157
+ fieldToSort = sortOptions[relation];
158
+ }
159
+ const query = {
357
160
  depth: 0,
358
161
  draft: true,
359
- limit: idsToLoad.length,
162
+ limit: maxResultsPerRequest,
360
163
  locale,
164
+ page: lastLoadedPageToUse,
165
+ sort: fieldToSort,
361
166
  where: {
362
- id: {
363
- in: idsToLoad
364
- }
167
+ and: [{
168
+ id: {
169
+ not_in: relationMap[relation]
170
+ }
171
+ }]
365
172
  }
366
173
  };
367
- if (!errorLoading) {
368
- const response_0 = await fetch(`${serverURL}${api}/${relation_0}`, {
369
- body: qs.stringify(query_0),
370
- credentials: "include",
371
- headers: {
372
- "Accept-Language": i18n.language,
373
- "Content-Type": "application/x-www-form-urlencoded",
374
- "X-HTTP-Method-Override": "GET"
375
- },
376
- method: "POST"
174
+ if (searchArg) {
175
+ query.where.and.push({
176
+ [fieldToSearch]: {
177
+ like: searchArg
178
+ }
179
+ });
180
+ }
181
+ if (relationFilterOption && typeof relationFilterOption !== 'boolean') {
182
+ query.where.and.push(relationFilterOption);
183
+ }
184
+ const response = await fetch(`${serverURL}${api}/${relation}`, {
185
+ body: qs.stringify(query),
186
+ credentials: 'include',
187
+ headers: {
188
+ 'Accept-Language': i18n.language,
189
+ 'Content-Type': 'application/x-www-form-urlencoded',
190
+ 'X-HTTP-Method-Override': 'GET'
191
+ },
192
+ method: 'POST'
193
+ });
194
+ if (response.ok) {
195
+ const data = await response.json();
196
+ setLastLoadedPage(prevState => {
197
+ return {
198
+ ...prevState,
199
+ [relation]: lastLoadedPageToUse
200
+ };
377
201
  });
378
- const collection_0 = collections.find(coll_0 => coll_0.slug === relation_0);
379
- let docs = [];
380
- if (response_0.ok) {
381
- const data_0 = await response_0.json();
382
- docs = data_0.docs;
202
+ if (!data.nextPage) {
203
+ setLastFullyLoadedRelation(relations.indexOf(relation));
204
+ }
205
+ if (data.docs.length > 0) {
206
+ resultsFetched += data.docs.length;
207
+ dispatchOptions({
208
+ type: 'ADD',
209
+ collection,
210
+ // TODO: fix this
211
+ // @ts-expect-error-next-line
212
+ config,
213
+ docs: data.docs,
214
+ i18n,
215
+ sort
216
+ });
383
217
  }
218
+ } else if (response.status === 403) {
219
+ setLastFullyLoadedRelation(relations.indexOf(relation));
384
220
  dispatchOptions({
385
- type: "ADD",
386
- collection: collection_0,
221
+ type: 'ADD',
222
+ collection,
223
+ // TODO: fix this
224
+ // @ts-expect-error-next-line
387
225
  config,
388
- docs,
226
+ docs: [],
389
227
  i18n,
390
- ids: idsToLoad,
391
- sort: true
228
+ ids: relationMap[relation],
229
+ sort
392
230
  });
231
+ } else {
232
+ setErrorLoading(t('error:unspecific'));
393
233
  }
394
234
  }
395
235
  }, Promise.resolve());
396
- };
397
- $[30] = hasMany;
398
- $[31] = relationTo;
399
- $[32] = value_0;
400
- $[33] = options;
401
- $[34] = locale;
402
- $[35] = errorLoading;
403
- $[36] = serverURL;
404
- $[37] = api;
405
- $[38] = i18n;
406
- $[39] = collections;
407
- $[40] = config;
408
- $[41] = t14;
409
- } else {
410
- t14 = $[41];
411
- }
412
- let t15;
413
- if ($[42] !== options || $[43] !== value_0 || $[44] !== hasMany || $[45] !== errorLoading || $[46] !== collections || $[47] !== hasMultipleRelations || $[48] !== serverURL || $[49] !== api || $[50] !== i18n || $[51] !== relationTo || $[52] !== locale || $[53] !== config) {
414
- t15 = [options, value_0, hasMany, errorLoading, collections, hasMultipleRelations, serverURL, api, i18n, relationTo, locale, config];
415
- $[42] = options;
416
- $[43] = value_0;
417
- $[44] = hasMany;
418
- $[45] = errorLoading;
419
- $[46] = collections;
420
- $[47] = hasMultipleRelations;
421
- $[48] = serverURL;
422
- $[49] = api;
423
- $[50] = i18n;
424
- $[51] = relationTo;
425
- $[52] = locale;
426
- $[53] = config;
427
- $[54] = t15;
428
- } else {
429
- t15 = $[54];
430
- }
431
- useEffect(t14, t15);
432
- let t16;
433
- let t17;
434
- if ($[55] !== relationTo || $[56] !== collections) {
435
- t16 = () => {
436
- const relations_0 = Array.isArray(relationTo) ? relationTo : [relationTo];
437
- const isIdOnly = relations_0.reduce((idOnly, relation_1) => {
438
- const collection_1 = collections.find(coll_1 => coll_1.slug === relation_1);
439
- const fieldToSearch_0 = collection_1?.admin?.useAsTitle || "id";
440
- return fieldToSearch_0 === "id" && idOnly;
441
- }, true);
442
- setEnableWordBoundarySearch(!isIdOnly);
443
- };
444
- t17 = [relationTo, collections];
445
- $[55] = relationTo;
446
- $[56] = collections;
447
- $[57] = t16;
448
- $[58] = t17;
449
- } else {
450
- t16 = $[57];
451
- t17 = $[58];
452
- }
453
- useEffect(t16, t17);
236
+ if (typeof onSuccess === 'function') onSuccess();
237
+ }
238
+ }, [permissions, relationTo, hasMany, errorLoading, search, collections, locale, filterOptions, serverURL, sortOptions, api, i18n, config, t]);
239
+ const updateSearch = useDebouncedCallback((searchArg_0, valueArg_0) => {
240
+ void getResults({
241
+ lastLoadedPage: {},
242
+ search: searchArg_0,
243
+ sort: true,
244
+ value: valueArg_0
245
+ });
246
+ setSearch(searchArg_0);
247
+ }, 300);
248
+ const handleInputChange = useCallback((searchArg_1, valueArg_1) => {
249
+ if (search !== searchArg_1) {
250
+ setLastLoadedPage({});
251
+ updateSearch(searchArg_1, valueArg_1, searchArg_1 !== '');
252
+ }
253
+ }, [search, updateSearch]);
254
+ // ///////////////////////////////////
255
+ // Ensure we have an option for each value
256
+ // ///////////////////////////////////
257
+ useEffect(() => {
258
+ const relationMap_0 = createRelationMap({
259
+ hasMany,
260
+ relationTo,
261
+ value: value_0
262
+ });
263
+ void Object.entries(relationMap_0).reduce(async (priorRelation_0, [relation_0, ids]) => {
264
+ await priorRelation_0;
265
+ const idsToLoad = ids.filter(id => {
266
+ return !options.find(optionGroup => optionGroup?.options?.find(option => option.value === id && option.relationTo === relation_0));
267
+ });
268
+ if (idsToLoad.length > 0) {
269
+ const query_0 = {
270
+ depth: 0,
271
+ draft: true,
272
+ limit: idsToLoad.length,
273
+ locale,
274
+ where: {
275
+ id: {
276
+ in: idsToLoad
277
+ }
278
+ }
279
+ };
280
+ if (!errorLoading) {
281
+ const response_0 = await fetch(`${serverURL}${api}/${relation_0}`, {
282
+ body: qs.stringify(query_0),
283
+ credentials: 'include',
284
+ headers: {
285
+ 'Accept-Language': i18n.language,
286
+ 'Content-Type': 'application/x-www-form-urlencoded',
287
+ 'X-HTTP-Method-Override': 'GET'
288
+ },
289
+ method: 'POST'
290
+ });
291
+ const collection_0 = collections.find(coll_0 => coll_0.slug === relation_0);
292
+ let docs = [];
293
+ if (response_0.ok) {
294
+ const data_0 = await response_0.json();
295
+ docs = data_0.docs;
296
+ }
297
+ dispatchOptions({
298
+ type: 'ADD',
299
+ collection: collection_0,
300
+ // TODO: fix this
301
+ // @ts-expect-error-next-line
302
+ config,
303
+ docs,
304
+ i18n,
305
+ ids: idsToLoad,
306
+ sort: true
307
+ });
308
+ }
309
+ }
310
+ }, Promise.resolve());
311
+ }, [options, value_0, hasMany, errorLoading, collections, hasMultipleRelations, serverURL, api, i18n, relationTo, locale, config]);
312
+ // Determine if we should switch to word boundary search
313
+ useEffect(() => {
314
+ const relations_0 = Array.isArray(relationTo) ? relationTo : [relationTo];
315
+ const isIdOnly = relations_0.reduce((idOnly, relation_1) => {
316
+ const collection_1 = collections.find(coll_1 => coll_1.slug === relation_1);
317
+ const fieldToSearch_0 = collection_1?.admin?.useAsTitle || 'id';
318
+ return fieldToSearch_0 === 'id' && idOnly;
319
+ }, true);
320
+ setEnableWordBoundarySearch(!isIdOnly);
321
+ }, [relationTo, collections]);
322
+ // When (`relationTo` || `filterOptions` || `locale`) changes, reset component
323
+ // Note - effect should not run on first run
454
324
  useEffect(() => {
325
+ // If the menu is open while filterOptions changes
326
+ // due to latency of getFormState and fast clicking into this field,
327
+ // re-fetch options
455
328
  if (hasLoadedFirstPageRef.current && menuIsOpen.current) {
456
329
  setIsLoading(true);
457
- getResults({
330
+ void getResults({
458
331
  lastLoadedPage: {},
459
332
  onSuccess: () => {
460
333
  hasLoadedFirstPageRef.current = true;
@@ -463,113 +336,74 @@ const RelationshipFieldComponent = props => {
463
336
  value: valueRef.current
464
337
  });
465
338
  }
339
+ // If the menu is not open, still reset the field state
340
+ // because we need to get new options next time the menu
341
+ // opens by the user
466
342
  dispatchOptions({
467
- type: "CLEAR"
343
+ type: 'CLEAR'
468
344
  });
469
345
  setLastFullyLoadedRelation(-1);
470
346
  setLastLoadedPage({});
471
347
  hasLoadedFirstPageRef.current = false;
472
348
  }, [relationTo, filterOptions, locale, menuIsOpen, getResults, valueRef, hasLoadedFirstPageRef, path]);
473
- let t18;
474
- if ($[59] !== config || $[60] !== i18n) {
475
- t18 = args => {
476
- dispatchOptions({
477
- type: "UPDATE",
478
- collection: args.collectionConfig,
479
- config,
480
- doc: args.doc,
481
- i18n
482
- });
483
- };
484
- $[59] = config;
485
- $[60] = i18n;
486
- $[61] = t18;
487
- } else {
488
- t18 = $[61];
489
- }
490
- const onSave = t18;
491
- const filterOption = _temp;
349
+ const onSave = useCallback(args => {
350
+ dispatchOptions({
351
+ type: 'UPDATE',
352
+ collection: args.collectionConfig,
353
+ // TODO: fix this
354
+ // @ts-expect-error-next-line
355
+ config,
356
+ doc: args.doc,
357
+ i18n
358
+ });
359
+ }, [i18n, config]);
360
+ const filterOption = useCallback((item, searchFilter) => {
361
+ if (!searchFilter) {
362
+ return true;
363
+ }
364
+ const r = wordBoundariesRegex(searchFilter || '');
365
+ // breaking the labels to search into smaller parts increases performance
366
+ const breakApartThreshold = 250;
367
+ let string = item.label;
368
+ // strings less than breakApartThreshold length won't be chunked
369
+ while (string.length > breakApartThreshold) {
370
+ // slicing by the next space after the length of the search input prevents slicing the string up by partial words
371
+ const indexOfSpace = string.indexOf(' ', searchFilter.length);
372
+ if (r.test(string.slice(0, indexOfSpace === -1 ? searchFilter.length : indexOfSpace + 1))) {
373
+ return true;
374
+ }
375
+ string = string.slice(indexOfSpace === -1 ? searchFilter.length : indexOfSpace + 1);
376
+ }
377
+ return r.test(string.slice(-breakApartThreshold));
378
+ }, []);
492
379
  const valueToRender = findOptionsByValue({
493
380
  options,
494
381
  value: value_0
495
382
  });
496
- if (!Array.isArray(valueToRender) && valueToRender?.value === "null") {
497
- valueToRender.value = null;
498
- }
499
- const t19 = showError && "error";
500
- const t20 = errorLoading && "error-loading";
501
- const t21 = readOnly && `${baseClass}--read-only`;
502
- const t22 = !readOnly && allowCreate && `${baseClass}--allow-create`;
503
- let t23;
504
- if ($[62] !== className || $[63] !== t19 || $[64] !== t20 || $[65] !== t21 || $[66] !== t22) {
505
- t23 = [fieldBaseClass, baseClass, className, t19, t20, t21, t22].filter(Boolean);
506
- $[62] = className;
507
- $[63] = t19;
508
- $[64] = t20;
509
- $[65] = t21;
510
- $[66] = t22;
511
- $[67] = t23;
512
- } else {
513
- t23 = $[67];
514
- }
515
- let t24;
516
- if ($[68] !== labelProps) {
517
- t24 = labelProps || {};
518
- $[68] = labelProps;
519
- $[69] = t24;
520
- } else {
521
- t24 = $[69];
522
- }
523
- let t25;
524
- if ($[70] !== errorProps) {
525
- t25 = errorProps || {};
526
- $[70] = errorProps;
527
- $[71] = t25;
528
- } else {
529
- t25 = $[71];
530
- }
531
- let t26;
532
- if ($[72] !== errorLoading) {
533
- t26 = errorLoading && _jsx("div", {
534
- className: `${baseClass}__error-loading`,
535
- children: errorLoading
536
- });
537
- $[72] = errorLoading;
538
- $[73] = t26;
539
- } else {
540
- t26 = $[73];
541
- }
542
- let t27;
543
- if ($[74] !== descriptionProps) {
544
- t27 = descriptionProps || {};
545
- $[74] = descriptionProps;
546
- $[75] = t27;
547
- } else {
548
- t27 = $[75];
549
- }
550
- return _jsxs("div", {
551
- className: t23.join(" "),
552
- id: `field-${path.replace(/\./g, "__")}`,
383
+ if (!Array.isArray(valueToRender) && valueToRender?.value === 'null') valueToRender.value = null;
384
+ return /*#__PURE__*/_jsxs("div", {
385
+ className: [fieldBaseClass, baseClass, className, showError && 'error', errorLoading && 'error-loading', readOnly && `${baseClass}--read-only`, !readOnly && allowCreate && `${baseClass}--allow-create`].filter(Boolean).join(' '),
386
+ id: `field-${path.replace(/\./g, '__')}`,
553
387
  style: {
554
388
  ...style,
555
389
  width
556
390
  },
557
- children: [_jsx(FieldLabel, {
391
+ children: [/*#__PURE__*/_jsx(FieldLabel, {
558
392
  Label: field?.admin?.components?.Label,
559
- field,
560
- label,
561
- required,
562
- ...t24
563
- }), _jsxs("div", {
393
+ field: field,
394
+ label: label,
395
+ required: required,
396
+ ...(labelProps || {})
397
+ }), /*#__PURE__*/_jsxs("div", {
564
398
  className: `${fieldBaseClass}__wrap`,
565
- children: [_jsx(FieldError, {
399
+ children: [/*#__PURE__*/_jsx(FieldError, {
566
400
  CustomError: field?.admin?.components?.Error,
567
- field,
568
- path,
569
- ...t25
570
- }), !errorLoading && _jsxs("div", {
401
+ field: field,
402
+ path: path,
403
+ ...(errorProps || {})
404
+ }), !errorLoading && /*#__PURE__*/_jsxs("div", {
571
405
  className: `${baseClass}__wrap`,
572
- children: [_jsx(ReactSelect, {
406
+ children: [/*#__PURE__*/_jsx(ReactSelect, {
573
407
  backspaceRemovesValue: !drawerIsOpen,
574
408
  components: {
575
409
  MultiValueLabel,
@@ -584,40 +418,32 @@ const RelationshipFieldComponent = props => {
584
418
  disabled: readOnly || formProcessing || drawerIsOpen,
585
419
  filterOption: enableWordBoundarySearch ? filterOption : undefined,
586
420
  getOptionValue: option_0 => {
587
- if (!option_0) {
588
- return;
589
- }
421
+ if (!option_0) return undefined;
590
422
  return hasMany && Array.isArray(relationTo) ? `${option_0.relationTo}_${option_0.value}` : option_0.value;
591
423
  },
592
- isLoading,
424
+ isLoading: isLoading,
593
425
  isMulti: hasMany,
594
- isSortable,
426
+ isSortable: isSortable,
595
427
  onChange: !readOnly ? selected => {
596
428
  if (selected === null) {
597
429
  setValue(hasMany ? [] : null);
598
- } else {
599
- if (hasMany && Array.isArray(selected)) {
600
- setValue(selected ? selected.map(option_1 => {
601
- if (hasMultipleRelations) {
602
- return {
603
- relationTo: option_1.relationTo,
604
- value: option_1.value
605
- };
606
- }
607
- return option_1.value;
608
- }) : null);
609
- } else {
610
- if (hasMultipleRelations && !Array.isArray(selected)) {
611
- setValue({
612
- relationTo: selected.relationTo,
613
- value: selected.value
614
- });
615
- } else {
616
- if (!Array.isArray(selected)) {
617
- setValue(selected.value);
618
- }
430
+ } else if (hasMany && Array.isArray(selected)) {
431
+ setValue(selected ? selected.map(option_1 => {
432
+ if (hasMultipleRelations) {
433
+ return {
434
+ relationTo: option_1.relationTo,
435
+ value: option_1.value
436
+ };
619
437
  }
620
- }
438
+ return option_1.value;
439
+ }) : null);
440
+ } else if (hasMultipleRelations && !Array.isArray(selected)) {
441
+ setValue({
442
+ relationTo: selected.relationTo,
443
+ value: selected.value
444
+ });
445
+ } else if (!Array.isArray(selected)) {
446
+ setValue(selected.value);
621
447
  }
622
448
  } : undefined,
623
449
  onInputChange: newSearch => handleInputChange(newSearch, value_0),
@@ -628,7 +454,7 @@ const RelationshipFieldComponent = props => {
628
454
  menuIsOpen.current = true;
629
455
  if (!hasLoadedFirstPageRef.current) {
630
456
  setIsLoading(true);
631
- getResults({
457
+ void getResults({
632
458
  lastLoadedPage: {},
633
459
  onSuccess: () => {
634
460
  hasLoadedFirstPageRef.current = true;
@@ -639,7 +465,7 @@ const RelationshipFieldComponent = props => {
639
465
  }
640
466
  },
641
467
  onMenuScrollToBottom: () => {
642
- getResults({
468
+ void getResults({
643
469
  lastFullyLoadedRelation,
644
470
  lastLoadedPage,
645
471
  search,
@@ -647,39 +473,27 @@ const RelationshipFieldComponent = props => {
647
473
  value: initialValue
648
474
  });
649
475
  },
650
- options,
651
- showError,
476
+ options: options,
477
+ showError: showError,
652
478
  value: valueToRender ?? null
653
- }), !readOnly && allowCreate && _jsx(AddNewRelation, {
654
- hasMany,
655
- path,
656
- relationTo,
657
- setValue,
479
+ }), !readOnly && allowCreate && /*#__PURE__*/_jsx(AddNewRelation, {
480
+ hasMany: hasMany,
481
+ path: path,
482
+ relationTo: relationTo,
483
+ setValue: setValue,
658
484
  value: value_0
659
485
  })]
660
- }), t26, _jsx(FieldDescription, {
486
+ }), errorLoading && /*#__PURE__*/_jsx("div", {
487
+ className: `${baseClass}__error-loading`,
488
+ children: errorLoading
489
+ }), /*#__PURE__*/_jsx(FieldDescription, {
661
490
  Description: field?.admin?.components?.Description,
662
- description,
663
- field,
664
- ...t27
491
+ description: description,
492
+ field: field,
493
+ ...(descriptionProps || {})
665
494
  })]
666
495
  })]
667
496
  });
668
497
  };
669
498
  export const RelationshipField = withCondition(RelationshipFieldComponent);
670
- function _temp(item, searchFilter) {
671
- if (!searchFilter) {
672
- return true;
673
- }
674
- const r = wordBoundariesRegex(searchFilter || "");
675
- let string = item.label;
676
- while (string.length > 250) {
677
- const indexOfSpace = string.indexOf(" ", searchFilter.length);
678
- if (r.test(string.slice(0, indexOfSpace === -1 ? searchFilter.length : indexOfSpace + 1))) {
679
- return true;
680
- }
681
- string = string.slice(indexOfSpace === -1 ? searchFilter.length : indexOfSpace + 1);
682
- }
683
- return r.test(string.slice(-250));
684
- }
685
499
  //# sourceMappingURL=index.js.map