@payloadcms/ui 3.29.0-internal.e4e913b → 3.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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, Fragment as _Fragment } from "react/jsx-runtime";
5
4
  import { useModal } from '@faceless-ui/modal';
6
5
  import * as qs from 'qs-esm';
@@ -24,7 +23,6 @@ import { UploadComponentHasOne } from './HasOne/index.js';
24
23
  import './index.scss';
25
24
  export const baseClass = 'upload';
26
25
  export function UploadInput(props) {
27
- const $ = _c(135);
28
26
  const {
29
27
  AfterInput,
30
28
  allowCreate,
@@ -53,7 +51,7 @@ export function UploadInput(props) {
53
51
  value
54
52
  } = props;
55
53
  const [populatedDocs, setPopulatedDocs] = React.useState();
56
- const [activeRelationTo] = React.useState(Array.isArray(relationTo) ? relationTo[0] : relationTo);
54
+ const [activeRelationTo, setActiveRelationTo] = React.useState(Array.isArray(relationTo) ? relationTo[0] : relationTo);
57
55
  const {
58
56
  openModal
59
57
  } = useModal();
@@ -75,668 +73,361 @@ export function UploadInput(props) {
75
73
  i18n,
76
74
  t
77
75
  } = useTranslation();
78
- let t0;
79
- let t1;
80
- if ($[0] !== activeRelationTo || $[1] !== filterOptionsFromProps) {
81
- t1 = filterOptionsFromProps?.[activeRelationTo] || {};
82
- $[0] = activeRelationTo;
83
- $[1] = filterOptionsFromProps;
84
- $[2] = t1;
85
- } else {
86
- t1 = $[2];
87
- }
88
- let t2;
89
- if ($[3] !== activeRelationTo || $[4] !== filterOptionsFromProps) {
90
- t2 = filterOptionsFromProps?.[activeRelationTo]?.id || {};
91
- $[3] = activeRelationTo;
92
- $[4] = filterOptionsFromProps;
93
- $[5] = t2;
94
- } else {
95
- t2 = $[5];
96
- }
97
- let t3;
98
- if ($[6] !== activeRelationTo || $[7] !== filterOptionsFromProps) {
99
- t3 = filterOptionsFromProps?.[activeRelationTo]?.id?.not_in || [];
100
- $[6] = activeRelationTo;
101
- $[7] = filterOptionsFromProps;
102
- $[8] = t3;
103
- } else {
104
- t3 = $[8];
105
- }
106
- let t4;
107
- if ($[9] !== t3 || $[10] !== value) {
108
- t4 = t3.concat(...(Array.isArray(value) || value ? [value] : []));
109
- $[9] = t3;
110
- $[10] = value;
111
- $[11] = t4;
112
- } else {
113
- t4 = $[11];
114
- }
115
- let t5;
116
- if ($[12] !== t2 || $[13] !== t4) {
117
- t5 = {
118
- ...t2,
119
- not_in: t4
120
- };
121
- $[12] = t2;
122
- $[13] = t4;
123
- $[14] = t5;
124
- } else {
125
- t5 = $[14];
126
- }
127
- let t6;
128
- if ($[15] !== t1 || $[16] !== t5) {
129
- t6 = {
130
- ...t1,
131
- id: t5
132
- };
133
- $[15] = t1;
134
- $[16] = t5;
135
- $[17] = t6;
136
- } else {
137
- t6 = $[17];
138
- }
139
- let t7;
140
- if ($[18] !== activeRelationTo || $[19] !== filterOptionsFromProps || $[20] !== t6) {
141
- t7 = {
76
+ const filterOptions = useMemo(() => {
77
+ return {
142
78
  ...filterOptionsFromProps,
143
- [activeRelationTo]: t6
144
- };
145
- $[18] = activeRelationTo;
146
- $[19] = filterOptionsFromProps;
147
- $[20] = t6;
148
- $[21] = t7;
149
- } else {
150
- t7 = $[21];
151
- }
152
- t0 = t7;
153
- const filterOptions = t0;
154
- let t8;
155
- if ($[22] !== relationTo) {
156
- t8 = typeof relationTo === "string" ? [relationTo] : relationTo;
157
- $[22] = relationTo;
158
- $[23] = t8;
159
- } else {
160
- t8 = $[23];
161
- }
162
- let t9;
163
- if ($[24] !== filterOptions || $[25] !== t8) {
164
- t9 = {
165
- collectionSlugs: t8,
166
- filterOptions
79
+ [activeRelationTo]: {
80
+ ...(filterOptionsFromProps?.[activeRelationTo] || {}),
81
+ id: {
82
+ ...(filterOptionsFromProps?.[activeRelationTo]?.id || {}),
83
+ not_in: (filterOptionsFromProps?.[activeRelationTo]?.id?.not_in || []).concat(...(Array.isArray(value) || value ? [value] : []))
84
+ }
85
+ }
167
86
  };
168
- $[24] = filterOptions;
169
- $[25] = t8;
170
- $[26] = t9;
171
- } else {
172
- t9 = $[26];
173
- }
174
- const [ListDrawer,, t10] = useListDrawer(t9);
175
- const {
87
+ }, [value, activeRelationTo, filterOptionsFromProps]);
88
+ const [ListDrawer,, {
176
89
  closeDrawer: closeListDrawer,
177
90
  openDrawer: openListDrawer
178
- } = t10;
179
- let t11;
180
- if ($[27] !== activeRelationTo) {
181
- t11 = {
182
- collectionSlug: activeRelationTo
183
- };
184
- $[27] = activeRelationTo;
185
- $[28] = t11;
186
- } else {
187
- t11 = $[28];
188
- }
189
- const [CreateDocDrawer,, t12] = useDocumentDrawer(t11);
190
- const {
91
+ }] = useListDrawer({
92
+ collectionSlugs: typeof relationTo === 'string' ? [relationTo] : relationTo,
93
+ filterOptions
94
+ });
95
+ const [CreateDocDrawer,, {
191
96
  closeDrawer: closeCreateDocDrawer,
192
97
  openDrawer: openCreateDocDrawer
193
- } = t12;
98
+ }] = useDocumentDrawer({
99
+ collectionSlug: activeRelationTo
100
+ });
101
+ /**
102
+ * Prevent initial retrieval of documents from running more than once
103
+ */
194
104
  const loadedValueDocsRef = React.useRef(false);
195
- let t13;
196
- bb0: {
105
+ const canCreate = useMemo(() => {
197
106
  if (!allowCreate) {
198
- t13 = false;
199
- break bb0;
107
+ return false;
200
108
  }
201
- if (typeof activeRelationTo === "string") {
109
+ if (typeof activeRelationTo === 'string') {
202
110
  if (permissions?.collections && permissions.collections?.[activeRelationTo]?.create) {
203
- t13 = true;
204
- break bb0;
111
+ return true;
205
112
  }
206
113
  }
207
- t13 = false;
208
- }
209
- const canCreate = t13;
210
- let t14;
211
- if ($[29] !== onChangeFromProps) {
212
- t14 = newValue => {
213
- if (typeof onChangeFromProps === "function") {
214
- onChangeFromProps(newValue);
215
- }
216
- };
217
- $[29] = onChangeFromProps;
218
- $[30] = t14;
219
- } else {
220
- t14 = $[30];
221
- }
222
- const onChange = t14;
223
- let t15;
224
- if ($[31] !== api || $[32] !== code || $[33] !== hasMany || $[34] !== i18n.language || $[35] !== serverURL || $[36] !== t) {
225
- t15 = async (ids, relatedCollectionSlug) => {
226
- const query = {
227
- depth: 0,
228
- draft: true,
229
- limit: ids.length,
230
- locale: code,
231
- where: {
232
- and: [{
233
- id: {
234
- in: ids
235
- }
236
- }]
237
- }
238
- };
239
- const response = await fetch(`${serverURL}${api}/${relatedCollectionSlug}`, {
240
- body: qs.stringify(query),
241
- credentials: "include",
242
- headers: {
243
- "Accept-Language": i18n.language,
244
- "Content-Type": "application/x-www-form-urlencoded",
245
- "X-HTTP-Method-Override": "GET"
246
- },
247
- method: "POST"
248
- });
249
- if (response.ok) {
250
- const json = await response.json();
251
- let sortedDocs = ids.map(id => json.docs.find(doc => String(doc.id) === String(id)));
252
- if (sortedDocs.includes(undefined) && hasMany) {
253
- sortedDocs = sortedDocs.map((doc_0, index) => doc_0 ? doc_0 : {
254
- id: ids[index],
255
- filename: `${t("general:untitled")} - ID: ${ids[index]}`,
256
- isPlaceholder: true
257
- });
258
- }
259
- return {
260
- ...json,
261
- docs: sortedDocs
262
- };
263
- }
264
- return null;
265
- };
266
- $[31] = api;
267
- $[32] = code;
268
- $[33] = hasMany;
269
- $[34] = i18n.language;
270
- $[35] = serverURL;
271
- $[36] = t;
272
- $[37] = t15;
273
- } else {
274
- t15 = $[37];
275
- }
276
- const populateDocs = t15;
277
- let t16;
278
- if ($[38] !== activeRelationTo || $[39] !== hasMany || $[40] !== onChange || $[41] !== value) {
279
- t16 = newDocs => {
280
- if (hasMany) {
281
- const mergedValue = [...(Array.isArray(value) ? value : []), ...newDocs.map(_temp)];
282
- onChange(mergedValue);
283
- setPopulatedDocs(currentDocs => [...(currentDocs || []), ...newDocs.map(doc_2 => ({
284
- relationTo: activeRelationTo,
285
- value: doc_2
286
- }))]);
287
- } else {
288
- const firstDoc = newDocs[0];
289
- onChange(firstDoc.id);
290
- setPopulatedDocs([{
291
- relationTo: activeRelationTo,
292
- value: firstDoc
293
- }]);
294
- }
295
- };
296
- $[38] = activeRelationTo;
297
- $[39] = hasMany;
298
- $[40] = onChange;
299
- $[41] = value;
300
- $[42] = t16;
301
- } else {
302
- t16 = $[42];
303
- }
304
- const onUploadSuccess = t16;
305
- let t17;
306
- if ($[43] !== drawerSlug || $[44] !== hasMany || $[45] !== maxRows || $[46] !== openModal || $[47] !== path || $[48] !== relationTo || $[49] !== setCollectionSlug || $[50] !== setCurrentActivePath || $[51] !== setInitialFiles || $[52] !== setMaxFiles) {
307
- t17 = fileList => {
308
- let fileListToUse = fileList;
309
- if (!hasMany && fileList && fileList.length > 1) {
310
- const dataTransfer = new DataTransfer();
311
- dataTransfer.items.add(fileList[0]);
312
- fileListToUse = dataTransfer.files;
313
- }
314
- if (fileListToUse) {
315
- setInitialFiles(fileListToUse);
316
- }
317
- setCollectionSlug(relationTo);
318
- if (typeof maxRows === "number") {
319
- setMaxFiles(maxRows);
114
+ return false;
115
+ }, [activeRelationTo, permissions, allowCreate]);
116
+ const onChange = React.useCallback(newValue => {
117
+ if (typeof onChangeFromProps === 'function') {
118
+ onChangeFromProps(newValue);
119
+ }
120
+ }, [onChangeFromProps]);
121
+ const populateDocs = React.useCallback(async (ids, relatedCollectionSlug) => {
122
+ if (!ids.length) {
123
+ return;
124
+ }
125
+ const query = {
126
+ depth: 0,
127
+ draft: true,
128
+ limit: ids.length,
129
+ locale: code,
130
+ where: {
131
+ and: [{
132
+ id: {
133
+ in: ids
134
+ }
135
+ }]
320
136
  }
321
- setCurrentActivePath(path);
322
- openModal(drawerSlug);
323
137
  };
324
- $[43] = drawerSlug;
325
- $[44] = hasMany;
326
- $[45] = maxRows;
327
- $[46] = openModal;
328
- $[47] = path;
329
- $[48] = relationTo;
330
- $[49] = setCollectionSlug;
331
- $[50] = setCurrentActivePath;
332
- $[51] = setInitialFiles;
333
- $[52] = setMaxFiles;
334
- $[53] = t17;
335
- } else {
336
- t17 = $[53];
337
- }
338
- const onLocalFileSelection = t17;
339
- let t18;
340
- if ($[54] !== activeRelationTo || $[55] !== closeListDrawer || $[56] !== onChange || $[57] !== populateDocs || $[58] !== value) {
341
- t18 = async docs => {
342
- const selectedDocIDs = [];
343
- for (const [id_0, isSelected] of docs) {
344
- if (isSelected) {
345
- selectedDocIDs.push(id_0);
346
- }
347
- }
348
- const loadedDocs = await populateDocs(selectedDocIDs, activeRelationTo);
349
- if (loadedDocs) {
350
- setPopulatedDocs(currentDocs_0 => [...(currentDocs_0 || []), ...loadedDocs.docs.map(doc_3 => ({
351
- relationTo: activeRelationTo,
352
- value: doc_3
353
- }))]);
138
+ const response = await fetch(`${serverURL}${api}/${relatedCollectionSlug}`, {
139
+ body: qs.stringify(query),
140
+ credentials: 'include',
141
+ headers: {
142
+ 'Accept-Language': i18n.language,
143
+ 'Content-Type': 'application/x-www-form-urlencoded',
144
+ 'X-HTTP-Method-Override': 'GET'
145
+ },
146
+ method: 'POST'
147
+ });
148
+ if (response.ok) {
149
+ const json = await response.json();
150
+ let sortedDocs = ids.map(id => json.docs.find(doc => {
151
+ return String(doc.id) === String(id);
152
+ }));
153
+ if (sortedDocs.includes(undefined) && hasMany) {
154
+ sortedDocs = sortedDocs.map((doc_0, index) => doc_0 ? doc_0 : {
155
+ id: ids[index],
156
+ filename: `${t('general:untitled')} - ID: ${ids[index]}`,
157
+ isPlaceholder: true
158
+ });
354
159
  }
355
- onChange([...(Array.isArray(value) ? value : []), ...selectedDocIDs]);
356
- closeListDrawer();
357
- };
358
- $[54] = activeRelationTo;
359
- $[55] = closeListDrawer;
360
- $[56] = onChange;
361
- $[57] = populateDocs;
362
- $[58] = value;
363
- $[59] = t18;
364
- } else {
365
- t18 = $[59];
366
- }
367
- const onListBulkSelect = t18;
368
- let t19;
369
- if ($[60] !== activeRelationTo || $[61] !== closeCreateDocDrawer || $[62] !== onChange) {
370
- t19 = data => {
371
- if (data.doc) {
372
- setPopulatedDocs(currentDocs_1 => [...(currentDocs_1 || []), {
373
- relationTo: activeRelationTo,
374
- value: data.doc
375
- }]);
376
- onChange(data.doc.id);
160
+ return {
161
+ ...json,
162
+ docs: sortedDocs
163
+ };
164
+ }
165
+ return null;
166
+ }, [code, serverURL, api, i18n.language, t, hasMany]);
167
+ const onUploadSuccess = useCallback(newDocs => {
168
+ if (hasMany) {
169
+ const mergedValue = [...(Array.isArray(value) ? value : []), ...newDocs.map(doc_1 => doc_1.id)];
170
+ onChange(mergedValue);
171
+ setPopulatedDocs(currentDocs => [...(currentDocs || []), ...newDocs.map(doc_2 => ({
172
+ relationTo: activeRelationTo,
173
+ value: doc_2
174
+ }))]);
175
+ } else {
176
+ const firstDoc = newDocs[0];
177
+ onChange(firstDoc.id);
178
+ setPopulatedDocs([{
179
+ relationTo: activeRelationTo,
180
+ value: firstDoc
181
+ }]);
182
+ }
183
+ }, [value, onChange, activeRelationTo, hasMany]);
184
+ const onLocalFileSelection = React.useCallback(fileList => {
185
+ let fileListToUse = fileList;
186
+ if (!hasMany && fileList && fileList.length > 1) {
187
+ const dataTransfer = new DataTransfer();
188
+ dataTransfer.items.add(fileList[0]);
189
+ fileListToUse = dataTransfer.files;
190
+ }
191
+ if (fileListToUse) {
192
+ setInitialFiles(fileListToUse);
193
+ }
194
+ setCollectionSlug(relationTo);
195
+ if (typeof maxRows === 'number') {
196
+ setMaxFiles(maxRows);
197
+ }
198
+ setCurrentActivePath(path);
199
+ openModal(drawerSlug);
200
+ }, [drawerSlug, hasMany, openModal, relationTo, setCollectionSlug, setInitialFiles, maxRows, setMaxFiles, path, setCurrentActivePath]);
201
+ // only hasMany can bulk select
202
+ const onListBulkSelect = React.useCallback(async docs => {
203
+ const selectedDocIDs = [];
204
+ for (const [id_0, isSelected] of docs) {
205
+ if (isSelected) {
206
+ selectedDocIDs.push(id_0);
377
207
  }
378
- closeCreateDocDrawer();
379
- };
380
- $[60] = activeRelationTo;
381
- $[61] = closeCreateDocDrawer;
382
- $[62] = onChange;
383
- $[63] = t19;
384
- } else {
385
- t19 = $[63];
386
- }
387
- const onDocCreate = t19;
388
- let t20;
389
- if ($[64] !== activeRelationTo || $[65] !== closeListDrawer || $[66] !== hasMany || $[67] !== onChange || $[68] !== populateDocs || $[69] !== value) {
390
- t20 = async t21 => {
391
- const {
392
- collectionSlug,
393
- doc: doc_4
394
- } = t21;
395
- const loadedDocs_0 = await populateDocs([doc_4.id], collectionSlug);
396
- const selectedDoc = loadedDocs_0 ? loadedDocs_0.docs?.[0] : null;
397
- setPopulatedDocs(currentDocs_2 => {
398
- if (selectedDoc) {
399
- if (hasMany) {
400
- return [...(currentDocs_2 || []), {
401
- relationTo: activeRelationTo,
402
- value: selectedDoc
403
- }];
404
- }
405
- return [{
208
+ }
209
+ const loadedDocs = await populateDocs(selectedDocIDs, activeRelationTo);
210
+ if (loadedDocs) {
211
+ setPopulatedDocs(currentDocs_0 => [...(currentDocs_0 || []), ...loadedDocs.docs.map(doc_3 => ({
212
+ relationTo: activeRelationTo,
213
+ value: doc_3
214
+ }))]);
215
+ }
216
+ onChange([...(Array.isArray(value) ? value : []), ...selectedDocIDs]);
217
+ closeListDrawer();
218
+ }, [activeRelationTo, closeListDrawer, onChange, populateDocs, value]);
219
+ const onDocCreate = React.useCallback(data => {
220
+ if (data.doc) {
221
+ setPopulatedDocs(currentDocs_1 => [...(currentDocs_1 || []), {
222
+ relationTo: activeRelationTo,
223
+ value: data.doc
224
+ }]);
225
+ onChange(data.doc.id);
226
+ }
227
+ closeCreateDocDrawer();
228
+ }, [closeCreateDocDrawer, activeRelationTo, onChange]);
229
+ const onListSelect = useCallback(async ({
230
+ collectionSlug,
231
+ doc: doc_4
232
+ }) => {
233
+ const loadedDocs_0 = await populateDocs([doc_4.id], collectionSlug);
234
+ const selectedDoc = loadedDocs_0 ? loadedDocs_0.docs?.[0] : null;
235
+ setPopulatedDocs(currentDocs_2 => {
236
+ if (selectedDoc) {
237
+ if (hasMany) {
238
+ return [...(currentDocs_2 || []), {
406
239
  relationTo: activeRelationTo,
407
240
  value: selectedDoc
408
241
  }];
409
242
  }
410
- return currentDocs_2;
411
- });
412
- if (hasMany) {
413
- onChange([...(Array.isArray(value) ? value : []), doc_4.id]);
414
- } else {
415
- onChange(doc_4.id);
243
+ return [{
244
+ relationTo: activeRelationTo,
245
+ value: selectedDoc
246
+ }];
416
247
  }
417
- closeListDrawer();
418
- };
419
- $[64] = activeRelationTo;
420
- $[65] = closeListDrawer;
421
- $[66] = hasMany;
422
- $[67] = onChange;
423
- $[68] = populateDocs;
424
- $[69] = value;
425
- $[70] = t20;
426
- } else {
427
- t20 = $[70];
428
- }
429
- const onListSelect = t20;
430
- let t21;
431
- if ($[71] !== hasMany || $[72] !== onChange || $[73] !== populateDocs) {
432
- t21 = async (docID, collectionSlug_0) => {
433
- const {
434
- docs: docs_0
435
- } = await populateDocs([docID], collectionSlug_0);
436
- if (docs_0[0]) {
437
- let updatedDocsToPropogate;
438
- updatedDocsToPropogate = [];
439
- setPopulatedDocs(currentDocs_3 => {
440
- const existingDocIndex = currentDocs_3?.findIndex(doc_5 => {
441
- const hasExisting = doc_5.value?.id === docs_0[0].id || doc_5.value?.isPlaceholder;
442
- return hasExisting && doc_5.relationTo === collectionSlug_0;
443
- });
444
- if (existingDocIndex > -1) {
445
- const updatedDocs = [...currentDocs_3];
446
- updatedDocs[existingDocIndex] = {
447
- relationTo: collectionSlug_0,
448
- value: docs_0[0]
449
- };
450
- updatedDocsToPropogate = updatedDocs;
451
- return updatedDocs;
452
- }
248
+ return currentDocs_2;
249
+ });
250
+ if (hasMany) {
251
+ onChange([...(Array.isArray(value) ? value : []), doc_4.id]);
252
+ } else {
253
+ onChange(doc_4.id);
254
+ }
255
+ closeListDrawer();
256
+ }, [closeListDrawer, hasMany, populateDocs, onChange, value, activeRelationTo]);
257
+ const reloadDoc = React.useCallback(async (docID, collectionSlug_0) => {
258
+ const {
259
+ docs: docs_0
260
+ } = await populateDocs([docID], collectionSlug_0);
261
+ if (docs_0[0]) {
262
+ let updatedDocsToPropogate = [];
263
+ setPopulatedDocs(currentDocs_3 => {
264
+ const existingDocIndex = currentDocs_3?.findIndex(doc_5 => {
265
+ const hasExisting = doc_5.value?.id === docs_0[0].id || doc_5.value?.isPlaceholder;
266
+ return hasExisting && doc_5.relationTo === collectionSlug_0;
453
267
  });
454
- if (updatedDocsToPropogate.length && hasMany) {
455
- onChange(updatedDocsToPropogate.map(_temp2));
268
+ if (existingDocIndex > -1) {
269
+ const updatedDocs = [...currentDocs_3];
270
+ updatedDocs[existingDocIndex] = {
271
+ relationTo: collectionSlug_0,
272
+ value: docs_0[0]
273
+ };
274
+ updatedDocsToPropogate = updatedDocs;
275
+ return updatedDocs;
456
276
  }
277
+ });
278
+ if (updatedDocsToPropogate.length && hasMany) {
279
+ onChange(updatedDocsToPropogate.map(doc_6 => doc_6.value?.id));
457
280
  }
458
- };
459
- $[71] = hasMany;
460
- $[72] = onChange;
461
- $[73] = populateDocs;
462
- $[74] = t21;
463
- } else {
464
- t21 = $[74];
465
- }
466
- const reloadDoc = t21;
467
- let t22;
468
- if ($[75] !== onChange) {
469
- t22 = newValue_0 => {
470
- const newValueIDs = newValue_0.map(_temp3);
471
- onChange(newValueIDs);
472
- setPopulatedDocs(newValue_0);
473
- };
474
- $[75] = onChange;
475
- $[76] = t22;
476
- } else {
477
- t22 = $[76];
478
- }
479
- const onReorder = t22;
480
- let t23;
481
- if ($[77] !== hasMany || $[78] !== onChange) {
482
- t23 = newValue_1 => {
483
- const newValueIDs_0 = newValue_1 ? newValue_1.map(_temp4) : null;
484
- onChange(hasMany ? newValueIDs_0 : newValueIDs_0 ? newValueIDs_0[0] : null);
485
- setPopulatedDocs(newValue_1 ? newValue_1 : []);
486
- };
487
- $[77] = hasMany;
488
- $[78] = onChange;
489
- $[79] = t23;
490
- } else {
491
- t23 = $[79];
492
- }
493
- const onRemove = t23;
494
- let t24;
495
- let t25;
496
- if ($[80] !== activeRelationTo || $[81] !== populateDocs || $[82] !== value) {
497
- t24 = () => {
498
- const loadInitialDocs = async function loadInitialDocs() {
499
- if (value) {
500
- loadedValueDocsRef.current = true;
501
- const loadedDocs_1 = await populateDocs(Array.isArray(value) ? value : [value], activeRelationTo);
502
- if (loadedDocs_1) {
503
- setPopulatedDocs(loadedDocs_1.docs.map(doc_7 => ({
504
- relationTo: activeRelationTo,
505
- value: doc_7
506
- })));
507
- }
281
+ }
282
+ }, [populateDocs, onChange, hasMany]);
283
+ // only hasMany can reorder
284
+ const onReorder = React.useCallback(newValue_0 => {
285
+ const newValueIDs = newValue_0.map(({
286
+ value: value_0
287
+ }) => value_0.id);
288
+ onChange(newValueIDs);
289
+ setPopulatedDocs(newValue_0);
290
+ }, [onChange]);
291
+ const onRemove = React.useCallback(newValue_1 => {
292
+ const newValueIDs_0 = newValue_1 ? newValue_1.map(({
293
+ value: value_1
294
+ }) => value_1.id) : null;
295
+ onChange(hasMany ? newValueIDs_0 : newValueIDs_0 ? newValueIDs_0[0] : null);
296
+ setPopulatedDocs(newValue_1 ? newValue_1 : []);
297
+ }, [onChange, hasMany]);
298
+ useEffect(() => {
299
+ async function loadInitialDocs() {
300
+ if (value) {
301
+ loadedValueDocsRef.current = true;
302
+ const loadedDocs_1 = await populateDocs(Array.isArray(value) ? value : [value], activeRelationTo);
303
+ if (loadedDocs_1) {
304
+ setPopulatedDocs(loadedDocs_1.docs.map(doc_7 => ({
305
+ relationTo: activeRelationTo,
306
+ value: doc_7
307
+ })));
508
308
  }
509
- };
510
- if (!loadedValueDocsRef.current) {
511
- loadInitialDocs();
512
309
  }
513
- };
514
- t25 = [populateDocs, activeRelationTo, value];
515
- $[80] = activeRelationTo;
516
- $[81] = populateDocs;
517
- $[82] = value;
518
- $[83] = t24;
519
- $[84] = t25;
520
- } else {
521
- t24 = $[83];
522
- t25 = $[84];
523
- }
524
- useEffect(t24, t25);
525
- let t26;
526
- if ($[85] !== onUploadSuccess || $[86] !== path || $[87] !== setOnSuccess) {
527
- t26 = () => {
528
- setOnSuccess(path, onUploadSuccess);
529
- };
530
- $[85] = onUploadSuccess;
531
- $[86] = path;
532
- $[87] = setOnSuccess;
533
- $[88] = t26;
534
- } else {
535
- t26 = $[88];
536
- }
537
- let t27;
538
- if ($[89] !== onUploadSuccess || $[90] !== path || $[91] !== setOnSuccess || $[92] !== value) {
539
- t27 = [value, path, onUploadSuccess, setOnSuccess];
540
- $[89] = onUploadSuccess;
541
- $[90] = path;
542
- $[91] = setOnSuccess;
543
- $[92] = value;
544
- $[93] = t27;
545
- } else {
546
- t27 = $[93];
547
- }
548
- useEffect(t26, t27);
549
- const showDropzone = !value || hasMany && Array.isArray(value) && (typeof maxRows !== "number" || value.length < maxRows) || !hasMany && populatedDocs?.[0] && typeof populatedDocs[0].value === "undefined";
550
- const t28 = showError && "error";
551
- const t29 = readOnly && "read-only";
552
- let t30;
553
- if ($[94] !== className || $[95] !== t28 || $[96] !== t29) {
554
- t30 = [fieldBaseClass, baseClass, className, t28, t29].filter(Boolean);
555
- $[94] = className;
556
- $[95] = t28;
557
- $[96] = t29;
558
- $[97] = t30;
559
- } else {
560
- t30 = $[97];
561
- }
562
- const t31 = t30.join(" ");
563
- const t32 = `field-${path?.replace(/\./g, "__")}`;
564
- let t33;
565
- if ($[98] !== AfterInput || $[99] !== BeforeInput || $[100] !== CreateDocDrawer || $[101] !== Description || $[102] !== Error || $[103] !== Label || $[104] !== ListDrawer || $[105] !== canCreate || $[106] !== description || $[107] !== displayPreview || $[108] !== hasMany || $[109] !== isSortable || $[110] !== label || $[111] !== localized || $[112] !== maxRows || $[113] !== onDocCreate || $[114] !== onListBulkSelect || $[115] !== onListSelect || $[116] !== onLocalFileSelection || $[117] !== onRemove || $[118] !== onReorder || $[119] !== openCreateDocDrawer || $[120] !== openListDrawer || $[121] !== path || $[122] !== populatedDocs || $[123] !== readOnly || $[124] !== reloadDoc || $[125] !== required || $[126] !== serverURL || $[127] !== showDropzone || $[128] !== showError || $[129] !== style || $[130] !== t || $[131] !== t31 || $[132] !== t32 || $[133] !== value) {
566
- t33 = _jsxs("div", {
567
- className: t31,
568
- id: t32,
569
- style,
570
- children: [_jsx(RenderCustomComponent, {
571
- CustomComponent: Label,
572
- Fallback: _jsx(FieldLabel, {
573
- label,
574
- localized,
575
- path,
576
- required
310
+ }
311
+ if (!loadedValueDocsRef.current) {
312
+ void loadInitialDocs();
313
+ }
314
+ }, [populateDocs, activeRelationTo, value]);
315
+ useEffect(() => {
316
+ setOnSuccess(path, onUploadSuccess);
317
+ }, [value, path, onUploadSuccess, setOnSuccess]);
318
+ const showDropzone = !value || hasMany && Array.isArray(value) && (typeof maxRows !== 'number' || value.length < maxRows) || !hasMany && populatedDocs?.[0] && typeof populatedDocs[0].value === 'undefined';
319
+ return /*#__PURE__*/_jsxs("div", {
320
+ className: [fieldBaseClass, baseClass, className, showError && 'error', readOnly && 'read-only'].filter(Boolean).join(' '),
321
+ id: `field-${path?.replace(/\./g, '__')}`,
322
+ style: style,
323
+ children: [/*#__PURE__*/_jsx(RenderCustomComponent, {
324
+ CustomComponent: Label,
325
+ Fallback: /*#__PURE__*/_jsx(FieldLabel, {
326
+ label: label,
327
+ localized: localized,
328
+ path: path,
329
+ required: required
330
+ })
331
+ }), /*#__PURE__*/_jsx("div", {
332
+ className: `${baseClass}__wrap`,
333
+ children: /*#__PURE__*/_jsx(RenderCustomComponent, {
334
+ CustomComponent: Error,
335
+ Fallback: /*#__PURE__*/_jsx(FieldError, {
336
+ path: path,
337
+ showError: showError
577
338
  })
578
- }), _jsx("div", {
579
- className: `${baseClass}__wrap`,
580
- children: _jsx(RenderCustomComponent, {
581
- CustomComponent: Error,
582
- Fallback: _jsx(FieldError, {
583
- path,
584
- showError
585
- })
339
+ })
340
+ }), BeforeInput, /*#__PURE__*/_jsxs("div", {
341
+ className: `${baseClass}__dropzoneAndUpload`,
342
+ children: [hasMany && Array.isArray(value) && value.length > 0 ? /*#__PURE__*/_jsx(_Fragment, {
343
+ children: populatedDocs && populatedDocs?.length > 0 ? /*#__PURE__*/_jsx(UploadComponentHasMany, {
344
+ displayPreview: displayPreview,
345
+ fileDocs: populatedDocs,
346
+ isSortable: isSortable && !readOnly,
347
+ onRemove: onRemove,
348
+ onReorder: onReorder,
349
+ readonly: readOnly,
350
+ reloadDoc: reloadDoc,
351
+ serverURL: serverURL
352
+ }) : /*#__PURE__*/_jsx("div", {
353
+ className: `${baseClass}__loadingRows`,
354
+ children: value.map(id_1 => /*#__PURE__*/_jsx(ShimmerEffect, {
355
+ height: "40px"
356
+ }, id_1))
586
357
  })
587
- }), BeforeInput, _jsxs("div", {
588
- className: `${baseClass}__dropzoneAndUpload`,
589
- children: [hasMany && Array.isArray(value) && value.length > 0 ? _jsx(_Fragment, {
590
- children: populatedDocs && populatedDocs?.length > 0 ? _jsx(UploadComponentHasMany, {
591
- displayPreview,
592
- fileDocs: populatedDocs,
593
- isSortable: isSortable && !readOnly,
594
- onRemove,
595
- onReorder,
596
- readonly: readOnly,
597
- reloadDoc,
598
- serverURL
599
- }) : _jsx("div", {
600
- className: `${baseClass}__loadingRows`,
601
- children: value.map(_temp5)
602
- })
603
- }) : null, !hasMany && value ? _jsx(_Fragment, {
604
- children: populatedDocs && populatedDocs?.length > 0 && populatedDocs[0].value ? _jsx(UploadComponentHasOne, {
605
- displayPreview,
606
- fileDoc: populatedDocs[0],
607
- onRemove,
608
- readonly: readOnly,
609
- reloadDoc,
610
- serverURL
611
- }) : populatedDocs && value && !populatedDocs?.[0]?.value ? _jsxs(_Fragment, {
612
- children: [t("general:untitled"), " - ID: ", value]
613
- }) : _jsx(ShimmerEffect, {
614
- height: "62px"
615
- })
616
- }) : null, showDropzone ? _jsx(Dropzone, {
617
- disabled: readOnly || !canCreate,
618
- multipleFiles: hasMany,
619
- onChange: onLocalFileSelection,
620
- children: _jsxs("div", {
621
- className: `${baseClass}__dropzoneContent`,
622
- children: [_jsxs("div", {
623
- className: `${baseClass}__dropzoneContent__buttons`,
624
- children: [canCreate && _jsxs(_Fragment, {
625
- children: [_jsx(Button, {
626
- buttonStyle: "pill",
627
- className: `${baseClass}__createNewToggler`,
628
- disabled: readOnly || !canCreate,
629
- onClick: () => {
630
- if (!readOnly) {
631
- if (hasMany) {
632
- onLocalFileSelection();
633
- } else {
634
- openCreateDocDrawer();
635
- }
636
- }
637
- },
638
- size: "small",
639
- children: t("general:createNew")
640
- }), _jsx("span", {
641
- className: `${baseClass}__dropzoneContent__orText`,
642
- children: t("general:or")
643
- })]
644
- }), _jsx(Button, {
358
+ }) : null, !hasMany && value ? /*#__PURE__*/_jsx(_Fragment, {
359
+ children: populatedDocs && populatedDocs?.length > 0 && populatedDocs[0].value ? /*#__PURE__*/_jsx(UploadComponentHasOne, {
360
+ displayPreview: displayPreview,
361
+ fileDoc: populatedDocs[0],
362
+ onRemove: onRemove,
363
+ readonly: readOnly,
364
+ reloadDoc: reloadDoc,
365
+ serverURL: serverURL
366
+ }) : populatedDocs && value && !populatedDocs?.[0]?.value ? /*#__PURE__*/_jsxs(_Fragment, {
367
+ children: [t('general:untitled'), " - ID: ", value]
368
+ }) : /*#__PURE__*/_jsx(ShimmerEffect, {
369
+ height: "62px"
370
+ })
371
+ }) : null, showDropzone ? /*#__PURE__*/_jsx(Dropzone, {
372
+ disabled: readOnly || !canCreate,
373
+ multipleFiles: hasMany,
374
+ onChange: onLocalFileSelection,
375
+ children: /*#__PURE__*/_jsxs("div", {
376
+ className: `${baseClass}__dropzoneContent`,
377
+ children: [/*#__PURE__*/_jsxs("div", {
378
+ className: `${baseClass}__dropzoneContent__buttons`,
379
+ children: [canCreate && /*#__PURE__*/_jsxs(_Fragment, {
380
+ children: [/*#__PURE__*/_jsx(Button, {
645
381
  buttonStyle: "pill",
646
- className: `${baseClass}__listToggler`,
647
- disabled: readOnly,
648
- onClick: openListDrawer,
382
+ className: `${baseClass}__createNewToggler`,
383
+ disabled: readOnly || !canCreate,
384
+ onClick: () => {
385
+ if (!readOnly) {
386
+ if (hasMany) {
387
+ onLocalFileSelection();
388
+ } else {
389
+ openCreateDocDrawer();
390
+ }
391
+ }
392
+ },
649
393
  size: "small",
650
- children: t("fields:chooseFromExisting")
651
- }), _jsx(CreateDocDrawer, {
652
- onSave: onDocCreate
653
- }), _jsx(ListDrawer, {
654
- allowCreate: canCreate,
655
- enableRowSelections: hasMany,
656
- onBulkSelect: onListBulkSelect,
657
- onSelect: onListSelect
394
+ children: t('general:createNew')
395
+ }), /*#__PURE__*/_jsx("span", {
396
+ className: `${baseClass}__dropzoneContent__orText`,
397
+ children: t('general:or')
658
398
  })]
659
- }), canCreate && !readOnly && _jsxs("p", {
660
- className: `${baseClass}__dragAndDropText`,
661
- children: [t("general:or"), " ", t("upload:dragAndDrop")]
399
+ }), /*#__PURE__*/_jsx(Button, {
400
+ buttonStyle: "pill",
401
+ className: `${baseClass}__listToggler`,
402
+ disabled: readOnly,
403
+ onClick: openListDrawer,
404
+ size: "small",
405
+ children: t('fields:chooseFromExisting')
406
+ }), /*#__PURE__*/_jsx(CreateDocDrawer, {
407
+ onSave: onDocCreate
408
+ }), /*#__PURE__*/_jsx(ListDrawer, {
409
+ allowCreate: canCreate,
410
+ enableRowSelections: hasMany,
411
+ onBulkSelect: onListBulkSelect,
412
+ onSelect: onListSelect
662
413
  })]
663
- })
664
- }) : _jsx(_Fragment, {
665
- children: !readOnly && !populatedDocs && (!value || typeof maxRows !== "number" || Array.isArray(value) && value.length < maxRows) ? _jsx(ShimmerEffect, {
666
- height: "40px"
667
- }) : null
668
- })]
669
- }), AfterInput, _jsx(RenderCustomComponent, {
670
- CustomComponent: Description,
671
- Fallback: _jsx(FieldDescription, {
672
- description,
673
- path
414
+ }), canCreate && !readOnly && /*#__PURE__*/_jsxs("p", {
415
+ className: `${baseClass}__dragAndDropText`,
416
+ children: [t('general:or'), " ", t('upload:dragAndDrop')]
417
+ })]
674
418
  })
419
+ }) : /*#__PURE__*/_jsx(_Fragment, {
420
+ children: !readOnly && !populatedDocs && (!value || typeof maxRows !== 'number' || Array.isArray(value) && value.length < maxRows) ? /*#__PURE__*/_jsx(ShimmerEffect, {
421
+ height: "40px"
422
+ }) : null
675
423
  })]
676
- });
677
- $[98] = AfterInput;
678
- $[99] = BeforeInput;
679
- $[100] = CreateDocDrawer;
680
- $[101] = Description;
681
- $[102] = Error;
682
- $[103] = Label;
683
- $[104] = ListDrawer;
684
- $[105] = canCreate;
685
- $[106] = description;
686
- $[107] = displayPreview;
687
- $[108] = hasMany;
688
- $[109] = isSortable;
689
- $[110] = label;
690
- $[111] = localized;
691
- $[112] = maxRows;
692
- $[113] = onDocCreate;
693
- $[114] = onListBulkSelect;
694
- $[115] = onListSelect;
695
- $[116] = onLocalFileSelection;
696
- $[117] = onRemove;
697
- $[118] = onReorder;
698
- $[119] = openCreateDocDrawer;
699
- $[120] = openListDrawer;
700
- $[121] = path;
701
- $[122] = populatedDocs;
702
- $[123] = readOnly;
703
- $[124] = reloadDoc;
704
- $[125] = required;
705
- $[126] = serverURL;
706
- $[127] = showDropzone;
707
- $[128] = showError;
708
- $[129] = style;
709
- $[130] = t;
710
- $[131] = t31;
711
- $[132] = t32;
712
- $[133] = value;
713
- $[134] = t33;
714
- } else {
715
- t33 = $[134];
716
- }
717
- return t33;
718
- }
719
- function _temp5(id_1) {
720
- return _jsx(ShimmerEffect, {
721
- height: "40px"
722
- }, id_1);
723
- }
724
- function _temp4(t0) {
725
- const {
726
- value: value_1
727
- } = t0;
728
- return value_1.id;
729
- }
730
- function _temp3(t0) {
731
- const {
732
- value: value_0
733
- } = t0;
734
- return value_0.id;
735
- }
736
- function _temp2(doc_6) {
737
- return doc_6.value?.id;
738
- }
739
- function _temp(doc_1) {
740
- return doc_1.id;
424
+ }), AfterInput, /*#__PURE__*/_jsx(RenderCustomComponent, {
425
+ CustomComponent: Description,
426
+ Fallback: /*#__PURE__*/_jsx(FieldDescription, {
427
+ description: description,
428
+ path: path
429
+ })
430
+ })]
431
+ });
741
432
  }
742
433
  //# sourceMappingURL=Input.js.map