funda-ui 4.3.355 → 4.3.721

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 (49) hide show
  1. package/DynamicFields/index.d.ts +1 -0
  2. package/DynamicFields/index.js +23 -3
  3. package/EventCalendar/index.css +150 -106
  4. package/EventCalendar/index.d.ts +11 -2
  5. package/EventCalendar/index.js +835 -151
  6. package/EventCalendarTimeline/index.css +293 -251
  7. package/EventCalendarTimeline/index.d.ts +18 -9
  8. package/EventCalendarTimeline/index.js +1077 -505
  9. package/MultipleCheckboxes/index.d.ts +1 -0
  10. package/MultipleCheckboxes/index.js +17 -7
  11. package/MultipleSelect/index.d.ts +4 -0
  12. package/MultipleSelect/index.js +54 -8
  13. package/NativeSelect/index.js +1 -0
  14. package/Radio/index.d.ts +2 -1
  15. package/Radio/index.js +54 -24
  16. package/Select/index.js +115 -42
  17. package/Table/index.js +27 -3
  18. package/lib/cjs/DynamicFields/index.d.ts +1 -0
  19. package/lib/cjs/DynamicFields/index.js +23 -3
  20. package/lib/cjs/EventCalendar/index.d.ts +11 -2
  21. package/lib/cjs/EventCalendar/index.js +835 -151
  22. package/lib/cjs/EventCalendarTimeline/index.d.ts +18 -9
  23. package/lib/cjs/EventCalendarTimeline/index.js +1077 -505
  24. package/lib/cjs/MultipleCheckboxes/index.d.ts +1 -0
  25. package/lib/cjs/MultipleCheckboxes/index.js +17 -7
  26. package/lib/cjs/MultipleSelect/index.d.ts +4 -0
  27. package/lib/cjs/MultipleSelect/index.js +54 -8
  28. package/lib/cjs/NativeSelect/index.js +1 -0
  29. package/lib/cjs/Radio/index.d.ts +2 -1
  30. package/lib/cjs/Radio/index.js +54 -24
  31. package/lib/cjs/Select/index.js +115 -42
  32. package/lib/cjs/Table/index.js +27 -3
  33. package/lib/css/EventCalendar/index.css +150 -106
  34. package/lib/css/EventCalendarTimeline/index.css +293 -251
  35. package/lib/esm/DynamicFields/index.tsx +28 -3
  36. package/lib/esm/EventCalendar/index.scss +172 -104
  37. package/lib/esm/EventCalendar/index.tsx +272 -139
  38. package/lib/esm/EventCalendarTimeline/index.scss +275 -213
  39. package/lib/esm/EventCalendarTimeline/index.tsx +706 -708
  40. package/lib/esm/MultipleCheckboxes/index.tsx +18 -5
  41. package/lib/esm/MultipleSelect/ItemList.tsx +1 -0
  42. package/lib/esm/MultipleSelect/index.tsx +103 -52
  43. package/lib/esm/NativeSelect/index.tsx +2 -0
  44. package/lib/esm/Radio/index.tsx +68 -27
  45. package/lib/esm/Select/index.tsx +236 -65
  46. package/lib/esm/Table/Table.tsx +1 -0
  47. package/lib/esm/Table/TableCell.tsx +6 -0
  48. package/lib/esm/Table/table-utils/ToggleSelection.tsx +17 -2
  49. package/package.json +1 -1
@@ -33,6 +33,7 @@ export type MultipleCheckboxesProps = {
33
33
  groupWrapperClassName?: string;
34
34
  groupLabelClassName?: string;
35
35
  inline?: boolean;
36
+ defaultValue?: string;
36
37
  value?: string;
37
38
  label?: React.ReactNode | string;
38
39
  name?: string;
@@ -74,6 +75,7 @@ const MultipleCheckboxes = forwardRef((props: MultipleCheckboxesProps, externalR
74
75
  options,
75
76
  disabled,
76
77
  required,
78
+ defaultValue,
77
79
  value,
78
80
  label,
79
81
  name,
@@ -177,10 +179,10 @@ const MultipleCheckboxes = forwardRef((props: MultipleCheckboxesProps, externalR
177
179
 
178
180
 
179
181
 
180
- async function fetchData(params: any) {
182
+ async function fetchData(params: any, inputDefault: any) {
181
183
 
182
184
  // set default value
183
- if (typeof value !== 'undefined' && value !== '') rootRef.current.dataset.value = value;
185
+ if (typeof inputDefault !== 'undefined' && inputDefault !== '') rootRef.current.dataset.value = inputDefault;
184
186
 
185
187
  //
186
188
  if (typeof fetchFuncAsync === 'object') {
@@ -207,7 +209,7 @@ const MultipleCheckboxes = forwardRef((props: MultipleCheckboxesProps, externalR
207
209
 
208
210
 
209
211
  //
210
- initDefaultValue(value, _ORGIN_DATA); // value must be initialized
212
+ initDefaultValue(inputDefault, _ORGIN_DATA); // value must be initialized
211
213
 
212
214
 
213
215
  //
@@ -225,7 +227,7 @@ const MultipleCheckboxes = forwardRef((props: MultipleCheckboxesProps, externalR
225
227
  optionsDataInit = removeArrDuplicateItems(optionsDataInit, 'value');
226
228
 
227
229
  //
228
- initDefaultValue(value, optionsDataInit); // value must be initialized
230
+ initDefaultValue(inputDefault, optionsDataInit); // value must be initialized
229
231
 
230
232
  //
231
233
  setDataInit(optionsDataInit); // data must be initialized
@@ -540,12 +542,23 @@ const MultipleCheckboxes = forwardRef((props: MultipleCheckboxesProps, externalR
540
542
  // data init
541
543
  //--------------
542
544
  const _params: any[] = fetchFuncMethodParams || [];
543
- fetchData((_params).join(','));
545
+ fetchData((_params).join(','), value);
544
546
 
545
547
 
546
548
  }, [value, options]);
547
549
 
548
550
 
551
+ useEffect(() => {
552
+
553
+ // update default value (It does not re-render the component because the incoming value changes.)
554
+ //--------------
555
+ if (typeof defaultValue !== 'undefined') { //REQUIRED
556
+ const _params: any[] = fetchFuncMethodParams || [];
557
+ fetchData((_params).join(','), defaultValue);
558
+ }
559
+
560
+ }, []);
561
+
549
562
  return (
550
563
  <>
551
564
 
@@ -7,6 +7,7 @@ import {
7
7
  import { clsWrite, combinedCls } from 'funda-utils/dist/cjs/cls';
8
8
 
9
9
 
10
+
10
11
  import { formatIndentVal, multiSelControlOptionExist } from './multiple-select-utils/func';
11
12
 
12
13
  /* Recursively nested components to traverse nodes
@@ -1,5 +1,4 @@
1
- import React, { useState, useEffect, useRef, forwardRef } from 'react';
2
-
1
+ import React, { useState, useEffect, useRef, forwardRef, useImperativeHandle } from 'react';
3
2
 
4
3
 
5
4
  import useComId from 'funda-utils/dist/cjs/useComId';
@@ -33,6 +32,7 @@ export interface OptionConfig {
33
32
 
34
33
 
35
34
  export type MultipleSelectProps = {
35
+ contentRef?: React.ForwardedRef<any>; // could use "Array" on contentRef.current, such as contentRef.current[0], contentRef.current[1]
36
36
  wrapperClassName?: string;
37
37
  childClassName?: string;
38
38
  wrapperMinHeight?: string;
@@ -50,6 +50,7 @@ export type MultipleSelectProps = {
50
50
  doubleIndent?: boolean;
51
51
  alternateCollapse?: boolean;
52
52
  arrow?: React.ReactNode;
53
+ defaultValue?: string;
53
54
  value?: string;
54
55
  label?: React.ReactNode | string;
55
56
  name?: string;
@@ -70,6 +71,8 @@ export type MultipleSelectProps = {
70
71
  fetchFuncMethodParams?: any[];
71
72
  fetchCallback?: (data: any) => void;
72
73
  onFetch?: (data: any) => void;
74
+ onAddAll?: (e: any, data: any, dataStr: any) => void;
75
+ onRemoveAll?: (e: any, data: any, dataStr: any) => void;
73
76
  onChange?: (e: any, data: any, dataStr: any, currentData: any, type: string) => void;
74
77
 
75
78
  };
@@ -77,6 +80,7 @@ export type MultipleSelectProps = {
77
80
 
78
81
  const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any) => {
79
82
  const {
83
+ contentRef,
80
84
  wrapperClassName,
81
85
  childClassName,
82
86
  wrapperMinHeight,
@@ -98,6 +102,7 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
98
102
  disabled,
99
103
  required,
100
104
  appendControl,
105
+ defaultValue,
101
106
  value,
102
107
  label,
103
108
  name,
@@ -110,11 +115,13 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
110
115
  fetchFuncMethodParams,
111
116
  fetchCallback,
112
117
  onFetch,
118
+ onAddAll,
119
+ onRemoveAll,
113
120
  onChange,
114
121
  ...attributes
115
122
  } = props;
116
123
 
117
-
124
+
118
125
  const UN_ATTACHED_SELECT = typeof unattachedSelect === 'undefined' || unattachedSelect === false ? false : true;
119
126
  const WRAPPER_MIN_H = typeof wrapperMinHeight === 'undefined' ? '' : wrapperMinHeight;
120
127
  const WRAPPER_MIN_W = typeof wrapperMinWidth === 'undefined' ? '' : wrapperMinWidth;
@@ -133,7 +140,7 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
133
140
 
134
141
 
135
142
  // return a array of options
136
- let optionsDataInit: OptionConfig[] = optionsRes;
143
+ let optionsDataInit: OptionConfig[] = optionsRes;
137
144
 
138
145
 
139
146
  //
@@ -142,39 +149,63 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
142
149
  //
143
150
  const [dataInit, setDataInit] = useState<OptionConfig[]>(optionsDataInit);
144
151
  const [hasErr, setHasErr] = useState<boolean>(false);
152
+
145
153
 
154
+ // exposes the following methods
155
+
156
+ useImperativeHandle(
157
+ contentRef,
158
+ () => ({
159
+ clear: (cb?: any) => {
160
+ initDefaultValue('', dataInit);
161
+ cb?.();
162
+ },
163
+ /*
164
+ set([{"label": "Option 1","listItemLabel":"Option 1 (No: 001)","value": "value-1","queryString": "option1"}], () => { console.log('callback') }])
165
+ */
166
+ set: (inputData: any[], cb?: any) => {
167
+ if (! Array.isArray(inputData)) return;
168
+
169
+ initDefaultValue(inputData.map((v: any) => `[${v.value}]`).join(''), dataInit);
170
+
171
+ cb?.();
172
+ }
173
+ }),
174
+ [contentRef, dataInit],
175
+ );
176
+
177
+
178
+ async function fetchData(params: any, inputDefault: any) {
146
179
 
147
- async function fetchData(params: any) {
148
-
149
180
  // set default value
150
- if (typeof value !== 'undefined' && value !== '') inputRef.current.dataset.value = value;
181
+ if (typeof inputDefault !== 'undefined' && inputDefault !== '') inputRef.current.dataset.value = inputDefault;
151
182
 
152
183
  //
153
- if ( typeof fetchFuncAsync === 'object' ) {
184
+ if (typeof fetchFuncAsync === 'object') {
154
185
 
155
186
  const response: any = await fetchFuncAsync[`${fetchFuncMethod}`](...params.split(','));
156
187
  let _ORGIN_DATA = response.data;
157
-
188
+
158
189
  // reset data structure
159
190
  if (typeof (fetchCallback) === 'function') {
160
191
  _ORGIN_DATA = fetchCallback(_ORGIN_DATA);
161
192
  }
162
193
 
163
194
  // Determine whether the data structure matches
164
- if ( _ORGIN_DATA.length > 0 && typeof _ORGIN_DATA[0].value === 'undefined' ) {
165
- console.warn( 'The data structure does not match, please refer to the example in the component documentation.' );
195
+ if (_ORGIN_DATA.length > 0 && typeof _ORGIN_DATA[0].value === 'undefined') {
196
+ console.warn('The data structure does not match, please refer to the example in the component documentation.');
166
197
  setHasErr(true);
167
198
  _ORGIN_DATA = [];
168
199
  }
169
200
 
170
-
201
+
171
202
  // Set hierarchical categories ( with sub-categories )
172
- if ( hierarchical ) {
203
+ if (hierarchical) {
173
204
  _ORGIN_DATA = addTreeDepth(_ORGIN_DATA);
174
205
  addTreeIndent(_ORGIN_DATA, INDENT_PLACEHOLDER, INDENT_LAST_PLACEHOLDER, 'label');
175
206
  }
176
207
 
177
-
208
+
178
209
  // remove Duplicate objects from JSON Array
179
210
  _ORGIN_DATA = removeArrDuplicateItems(_ORGIN_DATA, 'value');
180
211
 
@@ -182,22 +213,22 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
182
213
  setDataInit(_ORGIN_DATA); // data must be initialized
183
214
 
184
215
  //
185
- initDefaultValue(value, _ORGIN_DATA); // value must be initialized
216
+ initDefaultValue(inputDefault, _ORGIN_DATA); // value must be initialized
186
217
 
187
218
 
188
219
  //
189
220
  onFetch?.(_ORGIN_DATA);
190
-
221
+
191
222
  return _ORGIN_DATA;
192
223
  } else {
193
224
 
194
225
  // Set hierarchical categories ( with sub-categories )
195
- if ( hierarchical ) {
226
+ if (hierarchical) {
196
227
  optionsDataInit = addTreeDepth(optionsDataInit);
197
228
  addTreeIndent(optionsDataInit, INDENT_PLACEHOLDER, INDENT_LAST_PLACEHOLDER, 'label');
198
229
  }
199
230
 
200
-
231
+
201
232
  // remove Duplicate objects from JSON Array
202
233
  optionsDataInit = removeArrDuplicateItems(optionsDataInit, 'value');
203
234
 
@@ -207,7 +238,7 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
207
238
 
208
239
 
209
240
  //
210
- initDefaultValue(value, optionsDataInit); // value must be initialized
241
+ initDefaultValue(inputDefault, optionsDataInit); // value must be initialized
211
242
 
212
243
 
213
244
 
@@ -225,7 +256,7 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
225
256
 
226
257
  function initDefaultValue(defaultValue: any, options: any[]) {
227
258
 
228
-
259
+
229
260
  // change the value to trigger component rendering
230
261
  if (typeof defaultValue === 'undefined' || defaultValue === '') {
231
262
  setValSelected([]);
@@ -246,7 +277,7 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
246
277
  let _data = [...prevState, ...options.filter((item: any) => {
247
278
  return multiSelControlOptionExist(_initVal, item.value);
248
279
  })];
249
-
280
+
250
281
  return uniqueArr(_data);
251
282
  });
252
283
  } else {
@@ -276,7 +307,7 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
276
307
  const _label = _listItemLabel === '' ? _li.dataset.label : _listItemLabel;
277
308
  const _data = typeof _li.dataset.itemdata !== 'undefined' ? JSON.parse(_li.dataset.itemdata) : {};
278
309
 
279
-
310
+
280
311
  // set selected items
281
312
  setValSelected((prevState) => {
282
313
  const newData = JSON.parse(JSON.stringify(prevState));
@@ -305,48 +336,48 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
305
336
  return _res;
306
337
  });
307
338
  }
308
-
339
+
309
340
  function removeItem(el: HTMLElement) {
310
341
  if (el === null) return;
311
-
342
+
312
343
  const _li = el;
313
344
  const _val = _li.dataset.value;
314
345
  const _data = typeof _li.dataset.itemdata !== 'undefined' ? JSON.parse(_li.dataset.itemdata) : {};
315
-
316
-
346
+
347
+
317
348
  // set selected items
318
349
  setValSelected((prevState) => {
319
350
  const newData = JSON.parse(JSON.stringify(prevState));
320
351
  const index = newData.findIndex((item: string | number) => item == _val);
321
352
  if (index !== -1) newData.splice(index, 1);
322
-
353
+
323
354
  const _res = newData;
324
-
355
+
325
356
  onChange?.(_li, _res, VALUE_BY_BRACKETS ? convertArrToValByBrackets(_res) : _res.join(','), _data, 'remove');
326
-
357
+
327
358
  // show current item
328
359
  if (availableListRef.current) {
329
360
  const removedItem = availableListRef.current.querySelector(`li[data-value="${_val}"]`);
330
361
  if (removedItem !== null) removedItem.classList.remove('hide');
331
362
  }
332
-
333
-
363
+
364
+
334
365
  return _res;
335
366
  });
336
-
337
-
367
+
368
+
338
369
  // update selected data
339
370
  setValSelectedData((prevState) => {
340
371
  const newData = JSON.parse(JSON.stringify(prevState));
341
372
  const index = newData.findIndex((item: any) => item.value == _val);
342
373
  if (index !== -1) newData.splice(index, 1);
343
-
374
+
344
375
  const _res = newData;
345
-
376
+
346
377
  return _res;
347
378
  });
348
-
349
-
379
+
380
+
350
381
  }
351
382
 
352
383
 
@@ -358,13 +389,18 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
358
389
 
359
390
  const items = [].slice.call(availableListRef.current.querySelectorAll('li[data-value]'));
360
391
  items.forEach((item: any) => {
361
-
392
+
362
393
  if (!item.classList.contains('disabled')) {
363
394
  selectItem(item);
364
395
  }
365
-
396
+
366
397
  });
367
398
 
399
+ //
400
+ onAddAll?.(event, dataInit.map((v: any) => `${v.value}`), VALUE_BY_BRACKETS ? dataInit.map((v: any) => `[${v.value}]`).join('') : dataInit.map((v: any) => v.value).join(''));
401
+
402
+
403
+
368
404
 
369
405
  }
370
406
 
@@ -378,6 +414,11 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
378
414
  removeItem(item);
379
415
  });
380
416
 
417
+ //
418
+ onRemoveAll?.(event, [], '');
419
+
420
+
421
+
381
422
  }
382
423
 
383
424
  function handleChangeSearch(event: any) {
@@ -388,11 +429,11 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
388
429
 
389
430
  const items = [].slice.call(availableListRef.current.querySelectorAll('li[data-value]'));
390
431
  items.forEach((item: any) => {
391
-
432
+
392
433
  // Avoid fatal errors causing page crashes
393
434
  const _label = typeof item.dataset.label !== 'undefined' && item.dataset.label !== null ? item.dataset.label : '';
394
435
  const _queryString = typeof item.dataset.querystring !== 'undefined' && item.dataset.querystring !== null ? item.dataset.querystring : '';
395
-
436
+
396
437
  if (
397
438
  (
398
439
  _queryString.split(',').some((l: any) => l.charAt(0) === inputVal.toLowerCase()) ||
@@ -405,7 +446,7 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
405
446
  } else {
406
447
  item.classList.add('hide');
407
448
  }
408
-
449
+
409
450
  });
410
451
 
411
452
 
@@ -426,20 +467,30 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
426
467
  // data init
427
468
  //--------------
428
469
  const _params: any[] = fetchFuncMethodParams || [];
429
- fetchData((_params).join(','));
470
+ fetchData((_params).join(','), value);
471
+
430
472
 
431
-
432
473
  }, [value, options, data]);
433
474
 
475
+ useEffect(() => {
476
+
477
+ // update default value (It does not re-render the component because the incoming value changes.)
478
+ //--------------
479
+ if (typeof defaultValue !== 'undefined') { //REQUIRED
480
+ const _params: any[] = fetchFuncMethodParams || [];
481
+ fetchData((_params).join(','), defaultValue);
482
+ }
483
+
484
+ }, []);
434
485
 
435
486
  return (
436
487
  <>
437
488
 
438
- <div
489
+ <div
439
490
  className={combinedCls(
440
491
  'm-select__wrapper',
441
492
  clsWrite(wrapperClassName, 'mb-3')
442
- )}
493
+ )}
443
494
  ref={rootRef}
444
495
  style={{
445
496
  minWidth: WRAPPER_MIN_W === '' ? 'var(--m-select-wrapper-min-w)' : WRAPPER_MIN_W,
@@ -466,7 +517,7 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
466
517
  id={idRes}
467
518
  name={name}
468
519
  value={VALUE_BY_BRACKETS ? convertArrToValByBrackets(valSelected) : valSelected.join(',')} // do not use `defaultValue`
469
- onChange={() => void(0)}
520
+ onChange={() => void (0)}
470
521
  required={required || null}
471
522
  {...attributes}
472
523
  />
@@ -497,7 +548,7 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
497
548
  </div>
498
549
  {/* /SEARCH */}
499
550
 
500
- <span className="m-select__title" dangerouslySetInnerHTML={{__html: `${availableHeaderTitle || ''}`}}></span>
551
+ <span className="m-select__title" dangerouslySetInnerHTML={{ __html: `${availableHeaderTitle || ''}` }}></span>
501
552
 
502
553
  <a href="#" className="m-select__btn--add-all" onClick={handleSelectAll}>{addAllBtnLabel || 'Add all'}</a>
503
554
  </div>
@@ -506,7 +557,7 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
506
557
 
507
558
  {/* OPTIONS LIST */}
508
559
  <ItemList
509
- appendControl={appendControl}
560
+ appendControl={appendControl}
510
561
  root={rootRef.current}
511
562
  listContainerClassName="m-select__available m-select__options-contentlist"
512
563
  ref={availableListRef}
@@ -529,11 +580,11 @@ const MultipleSelect = forwardRef((props: MultipleSelectProps, externalRef: any)
529
580
  // ++++++++++++++++++++
530
581
  // Selected Container
531
582
  // ++++++++++++++++++++
532
- */}
583
+ */}
533
584
  <div className="m-select__selected__container">
534
585
  <div className="m-select__m-select__item-actions m-select__header">
535
- <span className="m-select__count" dangerouslySetInnerHTML={{__html: `${typeof selectedHeaderNote !== 'undefined' ? selectedHeaderNote.replace('{items_num}', valSelectedData.length as never) : ''}`}}></span>
536
- <span className="m-select__title" dangerouslySetInnerHTML={{__html: `${selectedHeaderTitle || ''}`}}></span>
586
+ <span className="m-select__count" dangerouslySetInnerHTML={{ __html: `${typeof selectedHeaderNote !== 'undefined' ? selectedHeaderNote.replace('{items_num}', valSelectedData.length as never) : ''}` }}></span>
587
+ <span className="m-select__title" dangerouslySetInnerHTML={{ __html: `${selectedHeaderTitle || ''}` }}></span>
537
588
  <a href="#" className="m-select__btn--remove-all" onClick={handleRemoveAll}>{removeAllBtnLabel || 'Remove all'}</a>
538
589
  </div>
539
590
 
@@ -109,6 +109,8 @@ const NativeSelect = forwardRef((props: NativeSelectProps, externalRef: any) =>
109
109
 
110
110
 
111
111
  const optionsFormatGroupOpt = (allData: any[]) => {
112
+ if (!Array.isArray(allData)) return;
113
+
112
114
  allData.forEach((item: any) => {
113
115
  if (typeof item.optgroup !== 'undefined') {
114
116
  item.value = String(Math.random());
@@ -11,6 +11,7 @@ import {
11
11
  import { clsWrite, combinedCls } from 'funda-utils/dist/cjs/cls';
12
12
 
13
13
 
14
+
14
15
  export interface OptionConfig {
15
16
  [propName: string]: string | number | React.ReactNode | boolean;
16
17
  }
@@ -29,7 +30,8 @@ export type RadioProps = {
29
30
  tableLayout?: boolean;
30
31
  tableLayoutClassName?: string;
31
32
  tableLayoutCellClassName?: string;
32
- value?: string;
33
+ defaultValue?: string | OptionConfig;
34
+ value?: string | OptionConfig;
33
35
  label?: React.ReactNode | string;
34
36
  name?: string;
35
37
  disabled?: any;
@@ -71,6 +73,7 @@ const Radio = forwardRef((props: RadioProps, externalRef: any) => {
71
73
  tableLayoutCellClassName,
72
74
  disabled,
73
75
  required,
76
+ defaultValue,
74
77
  value,
75
78
  label,
76
79
  name,
@@ -111,6 +114,32 @@ const Radio = forwardRef((props: RadioProps, externalRef: any) => {
111
114
  };
112
115
 
113
116
 
117
+ const isObject = (value: any) => {
118
+ return Object.prototype.toString.call(value) === '[object Object]';
119
+ };
120
+
121
+ const optionsFlat = (allData: any[]) => {
122
+
123
+ const flatItems: any[] = [];
124
+
125
+ allData.forEach((item: any) => {
126
+ if (typeof item.optgroup !== 'undefined') {
127
+ item.optgroup.forEach((opt: any) => {
128
+ flatItems.push(opt);
129
+ });
130
+ } else {
131
+ flatItems.push(item);
132
+ }
133
+ });
134
+
135
+ return flatItems;
136
+ };
137
+
138
+
139
+ const finalRes = (val: any) => {
140
+ return isObject(val) ? val.value : val;
141
+ };
142
+
114
143
 
115
144
  //
116
145
  const [dataInit, setDataInit] = useState<OptionConfig[]>(optionsDataInit);
@@ -176,10 +205,10 @@ const Radio = forwardRef((props: RadioProps, externalRef: any) => {
176
205
  }
177
206
 
178
207
 
179
- async function fetchData(params: any) {
208
+ async function fetchData(params: any, valueToInputDefault: any, inputDefault: any) {
180
209
 
181
210
  // set default value
182
- if (typeof value !== 'undefined' && value !== '') rootRef.current.dataset.value = value;
211
+ if (typeof valueToInputDefault !== 'undefined' && valueToInputDefault !== '') rootRef.current.dataset.value = valueToInputDefault;
183
212
 
184
213
 
185
214
  if (rootRef.current) {
@@ -212,15 +241,22 @@ const Radio = forwardRef((props: RadioProps, externalRef: any) => {
212
241
 
213
242
 
214
243
  // If the default value is label, match value
215
- let _realValue = value;
244
+ let _realValue = valueToInputDefault;
216
245
  let filterRes: any = [];
217
- const filterResQueryValue = _ORGIN_DATA.filter((item: any) => item.value == value);
218
- const filterResQueryLabel = _ORGIN_DATA.filter((item: any) => item.label == value);
246
+ const filterResQueryValue = _ORGIN_DATA.filter((item: any) => item.value == valueToInputDefault);
247
+ const filterResQueryLabel = _ORGIN_DATA.filter((item: any) => item.label == valueToInputDefault);
219
248
 
220
249
  if (filterResQueryValue.length === 0 && filterResQueryLabel.length > 0) {
221
250
  filterRes = filterResQueryValue;
222
251
  if (filterResQueryValue.length === 0) filterRes = filterResQueryLabel;
223
252
  if (filterRes.length > 0) _realValue = filterRes[0].value;
253
+
254
+ // if the default value is Object
255
+ if (isObject(inputDefault) && filterRes.length === 0) {
256
+ filterRes = [inputDefault];
257
+ }
258
+
259
+
224
260
  }
225
261
 
226
262
 
@@ -249,15 +285,21 @@ const Radio = forwardRef((props: RadioProps, externalRef: any) => {
249
285
 
250
286
 
251
287
  // If the default value is label, match value
252
- let _realValue = value;
288
+ let _realValue = valueToInputDefault;
253
289
  let filterRes: any = [];
254
- const filterResQueryValue = optionsDataInit.filter((item: any) => item.value == value);
255
- const filterResQueryLabel = optionsDataInit.filter((item: any) => item.label == value);
290
+ const filterResQueryValue = optionsDataInit.filter((item: any) => item.value == valueToInputDefault);
291
+ const filterResQueryLabel = optionsDataInit.filter((item: any) => item.label == valueToInputDefault);
256
292
 
257
293
  if (filterResQueryValue.length === 0 && filterResQueryLabel.length > 0) {
258
294
  filterRes = filterResQueryValue;
259
295
  if (filterResQueryValue.length === 0) filterRes = filterResQueryLabel;
260
296
  if (filterRes.length > 0) _realValue = filterRes[0].value;
297
+
298
+ // if the default value is Object
299
+ if (isObject(inputDefault) && filterRes.length === 0) {
300
+ filterRes = [inputDefault];
301
+ }
302
+
261
303
  }
262
304
 
263
305
 
@@ -298,22 +340,10 @@ const Radio = forwardRef((props: RadioProps, externalRef: any) => {
298
340
 
299
341
  function handleChange(event: any) {
300
342
  const val = event.target.value;
301
- let curData: any;
302
- let currentIndex = event.target.dataset.index;
303
-
304
- // if group
305
- if (currentIndex.indexOf('-') >= 0) {
306
- const groupIdArr = currentIndex.split('-');
307
- const groupIndex = groupIdArr[0];
308
- const groupItemIndex = groupIdArr[1];
309
- const groupOpts: any = dataInit[groupIndex].optgroup;
310
-
311
- curData = groupOpts[groupItemIndex];
312
- } else {
313
- curData = dataInit[currentIndex];
314
- }
315
-
343
+ const curData: Record<string, unknown> = optionsFlat(dataInit).find((v: any) => v.value == val);
344
+ const currentIndex = event.target.dataset.index;
316
345
 
346
+
317
347
  //----
318
348
  // update value
319
349
  setControlValue(val);
@@ -375,7 +405,7 @@ const Radio = forwardRef((props: RadioProps, externalRef: any) => {
375
405
  return Array.isArray(dataInit) ? dataInit.map((item: any, index: number) => {
376
406
  const requiredVal = index === 0 ? required || null : null;
377
407
 
378
- const _formatItem = {};
408
+ const _formatItem: Record<string, unknown> = {};
379
409
  Object.keys(item).forEach((key: string) => {
380
410
  if (key !== 'extends') _formatItem[key] = item[key];
381
411
  });
@@ -392,7 +422,7 @@ const Radio = forwardRef((props: RadioProps, externalRef: any) => {
392
422
 
393
423
  {item.optgroup.map((opt: any, optIndex: number) => {
394
424
 
395
- const _formatOpt = {};
425
+ const _formatOpt: Record<string, unknown> = {};
396
426
  Object.keys(item).forEach((key: string) => {
397
427
  if (key !== 'extends') _formatOpt[key] = item[key];
398
428
  });
@@ -563,13 +593,24 @@ const Radio = forwardRef((props: RadioProps, externalRef: any) => {
563
593
  // data init
564
594
  //--------------
565
595
  const _params: any[] = fetchFuncMethodParams || [];
566
- fetchData((_params).join(','));
596
+ fetchData((_params).join(','), finalRes(value), value);
567
597
 
568
598
 
569
599
 
570
600
  }, [value, options]);
571
601
 
572
602
 
603
+ useEffect(() => {
604
+
605
+ // update default value (It does not re-render the component because the incoming value changes.)
606
+ //--------------
607
+ if (typeof defaultValue !== 'undefined') { //REQUIRED
608
+ const _params: any[] = fetchFuncMethodParams || [];
609
+ fetchData((_params).join(','), finalRes(defaultValue), defaultValue);
610
+ }
611
+
612
+ }, []);
613
+
573
614
  return (
574
615
  <>
575
616