@kne/super-select-plus 0.1.1 → 0.1.2

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.
@@ -82,6 +82,51 @@ const defaultChildren = output => {
82
82
  return get(output, 'label', '');
83
83
  };
84
84
  const getLabels = output => Array.isArray(output) ? output.map(item => get(item, 'label', '')).filter(Boolean) : output ? [get(output, 'label', '')].filter(Boolean) : [];
85
+
86
+ /**
87
+ * 将 EnumDisplay / FunctionEnum / IndustryEnum 解析结果转为选择器 value
88
+ * @param {Array|Object} items createEnumComponent children 第一个参数
89
+ * @param {Object} options valueKey / labelKey 字段映射
90
+ */
91
+ const enumItemsToSelectValue = (items, {
92
+ valueKey: _valueKey = 'id',
93
+ labelKey: _labelKey = 'name'
94
+ } = {}) => (Array.isArray(items) ? items : [items]).filter(Boolean).map(item => {
95
+ var _item$id, _ref, _item$label;
96
+ return {
97
+ [_valueKey]: (_item$id = item.id) != null ? _item$id : item.code,
98
+ [_labelKey]: (_ref = (_item$label = item.label) != null ? _item$label : item.name) != null ? _ref : item.chName
99
+ };
100
+ });
101
+
102
+ /**
103
+ * 将单个 Enum 解析结果转为选择器 value(单选)
104
+ */
105
+ const enumItemToSelectValue = (item, options) => {
106
+ var _enumItemsToSelectVal;
107
+ return (_enumItemsToSelectVal = enumItemsToSelectValue(item, options)[0]) != null ? _enumItemsToSelectVal : null;
108
+ };
109
+
110
+ /**
111
+ * 将 AddressEnum 解析结果转为 SelectAddress value(支持 name 单值或 names 数组)
112
+ */
113
+ const addressEnumToSelectValue = outputs => {
114
+ const list = Array.isArray(outputs) ? outputs : outputs ? [outputs] : [];
115
+ return list.filter(item => item == null ? void 0 : item.city).map(({
116
+ city
117
+ }) => _extends({
118
+ value: city.code,
119
+ label: city.name
120
+ }, city));
121
+ };
122
+
123
+ /**
124
+ * 将单个 AddressEnum 解析结果转为 SelectAddress value(单选)
125
+ */
126
+ const addressEnumToSelectValueSingle = output => {
127
+ var _addressEnumToSelectV;
128
+ return (_addressEnumToSelectV = addressEnumToSelectValue(output)[0]) != null ? _addressEnumToSelectV : null;
129
+ };
85
130
  const resolveOutput = (mapping, name, names) => {
86
131
  if (names) {
87
132
  return names.map(code => mapping.get(code));
@@ -158,7 +203,7 @@ const defaultTransformItem = (item, label) => _extends({}, item, {
158
203
  /**
159
204
  * 内部枚举显示组件
160
205
  */
161
- const EnumDisplayInner = withFetch(_ref => {
206
+ const EnumDisplayInner = withFetch(_ref2 => {
162
207
  let {
163
208
  data,
164
209
  name,
@@ -169,8 +214,8 @@ const EnumDisplayInner = withFetch(_ref => {
169
214
  getLabel,
170
215
  dataFormat,
171
216
  transformItem
172
- } = _ref,
173
- props = _objectWithoutPropertiesLoose(_ref, _excluded$4);
217
+ } = _ref2,
218
+ props = _objectWithoutPropertiesLoose(_ref2, _excluded$4);
174
219
  const {
175
220
  locale
176
221
  } = useIntl();
@@ -335,6 +380,151 @@ const FunctionEnum = createEnumComponent({
335
380
  defaultApi: defaultFunctionApi
336
381
  });
337
382
 
383
+ const getItemId = (item, valueKey = 'id') => {
384
+ var _ref, _ref2, _item$valueKey;
385
+ if (item == null) {
386
+ return null;
387
+ }
388
+ if (typeof item !== 'object') {
389
+ return item;
390
+ }
391
+ return (_ref = (_ref2 = (_item$valueKey = item[valueKey]) != null ? _item$valueKey : item.id) != null ? _ref2 : item.code) != null ? _ref : null;
392
+ };
393
+ const getItemLabel = (item, labelKey = 'name') => {
394
+ var _ref3, _ref4, _item$labelKey;
395
+ if (item == null || typeof item !== 'object') {
396
+ return null;
397
+ }
398
+ return (_ref3 = (_ref4 = (_item$labelKey = item[labelKey]) != null ? _item$labelKey : item.label) != null ? _ref4 : item.name) != null ? _ref3 : null;
399
+ };
400
+ const resolveItemFromNode = (item, node, {
401
+ valueKey: _valueKey = 'id',
402
+ labelKey: _labelKey = 'name'
403
+ } = {}) => {
404
+ const id = getItemId(item, _valueKey);
405
+ if (id == null) {
406
+ return item;
407
+ }
408
+ if (!node) {
409
+ const _label = getItemLabel(item, _labelKey);
410
+ if (typeof item === 'object') {
411
+ return _label != null && _label !== String(id) ? item : _extends({}, typeof item === 'object' ? item : {}, {
412
+ [_valueKey]: id,
413
+ [_labelKey]: _label || String(id)
414
+ });
415
+ }
416
+ return {
417
+ [_valueKey]: id,
418
+ [_labelKey]: String(id)
419
+ };
420
+ }
421
+ const label = getItemLabel(item, _labelKey) || node[_labelKey] || node.name;
422
+ return _extends({}, node, typeof item === 'object' ? item : {}, {
423
+ [_valueKey]: id,
424
+ [_labelKey]: label
425
+ });
426
+ };
427
+
428
+ /**
429
+ * 通过扁平 Map 解析 value(推荐,不依赖级联树结构)
430
+ */
431
+ const resolveCascaderValueFromMap = (value, mapping, {
432
+ valueKey: _valueKey2 = 'id',
433
+ labelKey: _labelKey2 = 'name',
434
+ single: _single = false
435
+ } = {}) => {
436
+ if (value == null || !(mapping != null && mapping.size)) {
437
+ return value;
438
+ }
439
+ const resolveItem = item => resolveItemFromNode(item, mapping.get(getItemId(item, _valueKey2)), {
440
+ valueKey: _valueKey2,
441
+ labelKey: _labelKey2
442
+ });
443
+ if (_single) {
444
+ const current = Array.isArray(value) ? value[0] : value;
445
+ return current != null ? resolveItem(current) : value;
446
+ }
447
+ return Array.isArray(value) ? value.map(resolveItem) : value;
448
+ };
449
+
450
+ /**
451
+ * 将仅含编码的 value 解析为含 label 的完整对象(城市选择器)
452
+ */
453
+ const resolveAddressValue = (value, addressApi, {
454
+ single: _single2 = false
455
+ } = {}) => {
456
+ if (value == null || !addressApi) {
457
+ return value;
458
+ }
459
+ const resolveItem = item => {
460
+ var _item$value, _item$label;
461
+ if (item == null) {
462
+ return item;
463
+ }
464
+ const code = typeof item === 'object' ? (_item$value = item.value) != null ? _item$value : item.code : item;
465
+ if (!code) {
466
+ return typeof item === 'object' ? item : null;
467
+ }
468
+ const label = typeof item === 'object' ? (_item$label = item.label) != null ? _item$label : item.name : null;
469
+ if (label && label !== String(code)) {
470
+ return typeof item === 'object' ? item : {
471
+ value: code,
472
+ label
473
+ };
474
+ }
475
+ const {
476
+ city
477
+ } = addressApi.getCity(code);
478
+ return city ? _extends({
479
+ value: code,
480
+ label: city.name
481
+ }, city) : {
482
+ value: code,
483
+ label: String(code)
484
+ };
485
+ };
486
+ if (_single2) {
487
+ const current = Array.isArray(value) ? value[0] : value;
488
+ return current != null ? resolveItem(current) : value;
489
+ }
490
+ return Array.isArray(value) ? value.map(resolveItem) : value;
491
+ };
492
+
493
+ /**
494
+ * 判断是否需要将解析后的 value 同步回 onChange(仅编码 -> 含 label)
495
+ */
496
+ const shouldSyncSelectValue = (value, resolved, {
497
+ valueKey: _valueKey3 = 'id',
498
+ labelKey: _labelKey3 = 'name',
499
+ single: _single3 = false
500
+ } = {}) => {
501
+ if (resolved == null || value == null) {
502
+ return false;
503
+ }
504
+ const needSyncItem = (original, resolvedItem) => {
505
+ const id = getItemId(original, _valueKey3);
506
+ const resolvedId = getItemId(resolvedItem, _valueKey3);
507
+ if (id == null || id !== resolvedId) {
508
+ return false;
509
+ }
510
+ const resolvedLabel = getItemLabel(resolvedItem, _labelKey3);
511
+ if (!resolvedLabel || resolvedLabel === String(id)) {
512
+ return false;
513
+ }
514
+ const valueLabel = getItemLabel(original, _labelKey3);
515
+ return !valueLabel || valueLabel === String(id);
516
+ };
517
+ if (_single3) {
518
+ const original = Array.isArray(value) ? value[0] : value;
519
+ const resolvedItem = Array.isArray(resolved) ? resolved[0] : resolved;
520
+ return needSyncItem(original, resolvedItem);
521
+ }
522
+ if (!Array.isArray(value) || !Array.isArray(resolved)) {
523
+ return false;
524
+ }
525
+ return resolved.some((item, index) => needSyncItem(value[index], item));
526
+ };
527
+
338
528
  const _excluded$3 = ["value", "onChange", "single", "placeholder", "isPopup", "overlayWidth", "apis", "onSearch"];
339
529
  const defaultFunctionData = () => {
340
530
  return import('./function-99219f66.js').then(module => module['__esModule'] ? module.default : module);
@@ -397,6 +587,38 @@ const SelectFunctionInner = _ref => {
397
587
  });
398
588
  }, []);
399
589
  const options = useMemo(() => transformToCascaderData$1(data, locale), [data, locale]);
590
+ const flatMapping = useMemo(() => {
591
+ const mapping = new Map();
592
+ data.forEach(item => {
593
+ mapping.set(item.code, _extends({}, item, {
594
+ id: item.code,
595
+ name: getLabelForLocal$3(item, locale)
596
+ }));
597
+ });
598
+ return mapping;
599
+ }, [data, locale]);
600
+ const normalizedValue = useMemo(() => resolveCascaderValueFromMap(value, flatMapping, {
601
+ valueKey: 'id',
602
+ labelKey: 'name',
603
+ single
604
+ }), [value, flatMapping, single]);
605
+ useEffect(() => {
606
+ if (!onChange || !flatMapping.size) {
607
+ return;
608
+ }
609
+ const resolved = resolveCascaderValueFromMap(value, flatMapping, {
610
+ valueKey: 'id',
611
+ labelKey: 'name',
612
+ single
613
+ });
614
+ if (shouldSyncSelectValue(value, resolved, {
615
+ valueKey: 'id',
616
+ labelKey: 'name',
617
+ single
618
+ })) {
619
+ onChange(resolved);
620
+ }
621
+ }, [value, flatMapping, single, onChange]);
400
622
  const handleSearch = (searchText, {
401
623
  mapping
402
624
  }) => {
@@ -407,7 +629,7 @@ const SelectFunctionInner = _ref => {
407
629
  });
408
630
  };
409
631
  return /*#__PURE__*/jsx(SelectCascader, _extends({}, props, {
410
- value: value,
632
+ value: normalizedValue,
411
633
  onChange: onChange,
412
634
  single: single,
413
635
  placeholder: placeholder || formatMessage({
@@ -528,6 +750,38 @@ const SelectIndustryInner = _ref => {
528
750
  });
529
751
  }, []);
530
752
  const options = useMemo(() => transformToCascaderData(data, locale), [data, locale]);
753
+ const flatMapping = useMemo(() => {
754
+ const mapping = new Map();
755
+ data.filter(item => item.code !== '000').forEach(item => {
756
+ mapping.set(item.code, _extends({}, item, {
757
+ id: item.code,
758
+ name: getLabelForLocal$2(item, locale)
759
+ }));
760
+ });
761
+ return mapping;
762
+ }, [data, locale]);
763
+ const normalizedValue = useMemo(() => resolveCascaderValueFromMap(value, flatMapping, {
764
+ valueKey: 'id',
765
+ labelKey: 'name',
766
+ single
767
+ }), [value, flatMapping, single]);
768
+ useEffect(() => {
769
+ if (!onChange || !flatMapping.size) {
770
+ return;
771
+ }
772
+ const resolved = resolveCascaderValueFromMap(value, flatMapping, {
773
+ valueKey: 'id',
774
+ labelKey: 'name',
775
+ single
776
+ });
777
+ if (shouldSyncSelectValue(value, resolved, {
778
+ valueKey: 'id',
779
+ labelKey: 'name',
780
+ single
781
+ })) {
782
+ onChange(resolved);
783
+ }
784
+ }, [value, flatMapping, single, onChange]);
531
785
  const handleSearch = (searchText, {
532
786
  mapping
533
787
  }) => {
@@ -538,7 +792,7 @@ const SelectIndustryInner = _ref => {
538
792
  });
539
793
  };
540
794
  return /*#__PURE__*/jsx(SelectCascader, _extends({}, props, {
541
- value: value,
795
+ value: normalizedValue,
542
796
  onChange: onChange,
543
797
  single: single,
544
798
  placeholder: placeholder || formatMessage({
@@ -561,8 +815,8 @@ const SelectIndustry = withLocale(SelectIndustryInner);
561
815
  SelectIndustry.defaultData = defaultIndustryData;
562
816
  SelectIndustry.Enum = IndustryEnum;
563
817
 
564
- const _excluded$1 = ["data", "name", "children", "displayParent"],
565
- _excluded2 = ["displayParent"];
818
+ const _excluded$1 = ["data", "name", "names", "children", "displayParent"],
819
+ _excluded2$1 = ["displayParent"];
566
820
  const getLabelForLocal$1 = (item, locale) => {
567
821
  if (locale === 'en-US') {
568
822
  return get(item, 'enName') || get(item, 'name');
@@ -590,6 +844,7 @@ const AddressEnumInner = withFetch(_ref => {
590
844
  let {
591
845
  data,
592
846
  name,
847
+ names,
593
848
  children,
594
849
  displayParent
595
850
  } = _ref,
@@ -598,6 +853,33 @@ const AddressEnumInner = withFetch(_ref => {
598
853
  locale
599
854
  } = useIntl();
600
855
  const addressApi = useMemo(() => createAddressApi(data), [data]);
856
+ const formatCityData = cityData => {
857
+ const {
858
+ city,
859
+ parent
860
+ } = cityData;
861
+ if (!city) {
862
+ return '';
863
+ }
864
+ if (displayParent && parent) {
865
+ return `${getLabelForLocal$1(parent, locale)}·${getLabelForLocal$1(city, locale)}`;
866
+ }
867
+ return getLabelForLocal$1(city, locale);
868
+ };
869
+ if (names != null && names.length) {
870
+ const outputs = names.map(code => addressApi.getCity(code));
871
+ const labels = outputs.map(formatCityData).filter(Boolean);
872
+ if (children) {
873
+ return children(outputs, _extends({
874
+ displayParent,
875
+ locale,
876
+ getLabelForLocal: getLabelForLocal$1,
877
+ names,
878
+ labels
879
+ }, props));
880
+ }
881
+ return labels.toString();
882
+ }
601
883
 
602
884
  // 获取城市数据
603
885
  const cityData = addressApi.getCity(name);
@@ -628,7 +910,7 @@ const AddressEnum = withLocale(props => {
628
910
  const {
629
911
  displayParent = false
630
912
  } = props,
631
- restProps = _objectWithoutPropertiesLoose(props, _excluded2);
913
+ restProps = _objectWithoutPropertiesLoose(props, _excluded2$1);
632
914
  return /*#__PURE__*/jsx(AddressEnumInner, _extends({}, addressDefaultApi, {
633
915
  displayParent: displayParent
634
916
  }, restProps));
@@ -717,7 +999,8 @@ var classnames = createCommonjsModule(function (module) {
717
999
 
718
1000
  var style = {"address":"kne-super-select-plus_MMoPf","is-popup":"kne-super-select-plus_bYKMf","title":"kne-super-select-plus_pKOHI","scroll-box":"kne-super-select-plus_MObpF","scroll-plus-box":"kne-super-select-plus_iKm8i","content":"kne-super-select-plus_BfCap","col-left":"kne-super-select-plus_BEChX","menu-list":"kne-super-select-plus_gXzFG","menu-item":"kne-super-select-plus_LasXn","col-right":"kne-super-select-plus_1jQmu","search-input":"kne-super-select-plus_YB45l","list":"kne-super-select-plus_Y-MIq","list-item":"kne-super-select-plus_6JEMZ","item-label":"kne-super-select-plus_nlduJ"};
719
1001
 
720
- const _excluded = ["single", "isPopup", "showChinaQuan", "showForeignQuan"];
1002
+ const _excluded = ["value", "single", "onChange"],
1003
+ _excluded2 = ["single", "isPopup", "showChinaQuan", "showForeignQuan"];
721
1004
  const getLabelForLocal = (item, locale) => {
722
1005
  if (locale === 'en-US') {
723
1006
  return get(item, 'enName') || get(item, 'name');
@@ -1032,21 +1315,53 @@ const SelectAddressInner = /*#__PURE__*/forwardRef((props, ref) => {
1032
1315
  const {
1033
1316
  formatMessage
1034
1317
  } = useIntl();
1318
+ const {
1319
+ value,
1320
+ single,
1321
+ onChange
1322
+ } = props,
1323
+ restProps = _objectWithoutPropertiesLoose(props, _excluded);
1324
+ const [cityData, setCityData] = useState(null);
1325
+ useEffect(() => {
1326
+ defaultCityData().then(setCityData);
1327
+ }, []);
1328
+ const addressApi = useMemo(() => cityData ? createAddressApi(cityData) : null, [cityData]);
1329
+ const normalizedValue = useMemo(() => resolveAddressValue(value, addressApi, {
1330
+ single
1331
+ }), [value, addressApi, single]);
1332
+ useEffect(() => {
1333
+ if (!onChange || !addressApi) {
1334
+ return;
1335
+ }
1336
+ const resolved = resolveAddressValue(value, addressApi, {
1337
+ single
1338
+ });
1339
+ if (shouldSyncSelectValue(value, resolved, {
1340
+ valueKey: 'value',
1341
+ labelKey: 'label',
1342
+ single
1343
+ })) {
1344
+ onChange(resolved);
1345
+ }
1346
+ }, [value, addressApi, single, onChange]);
1035
1347
  return /*#__PURE__*/jsx(SelectInput, _extends({
1036
1348
  ref: ref
1037
- }, props, {
1038
- placeholder: props.placeholder || formatMessage({
1349
+ }, restProps, {
1350
+ single: single,
1351
+ value: normalizedValue,
1352
+ onChange: onChange,
1353
+ placeholder: restProps.placeholder || formatMessage({
1039
1354
  id: 'addressPlaceholder'
1040
1355
  }, {
1041
1356
  defaultMessage: '请选择城市'
1042
1357
  }),
1043
1358
  children: contextProps => {
1044
1359
  const {
1045
- value,
1360
+ value: currentValue,
1046
1361
  setValue
1047
1362
  } = contextProps;
1048
1363
  return /*#__PURE__*/jsx(AddressInner, {
1049
- value: value,
1364
+ value: currentValue,
1050
1365
  setValue: setValue,
1051
1366
  props: props
1052
1367
  });
@@ -1060,7 +1375,7 @@ const SelectAddress = withLocale(_ref => {
1060
1375
  showChinaQuan = false,
1061
1376
  showForeignQuan = false
1062
1377
  } = _ref,
1063
- props = _objectWithoutPropertiesLoose(_ref, _excluded);
1378
+ props = _objectWithoutPropertiesLoose(_ref, _excluded2);
1064
1379
  return /*#__PURE__*/jsx(SelectAddressInner, _extends({
1065
1380
  single: single,
1066
1381
  isPopup: isPopup,
@@ -1080,8 +1395,12 @@ var index = {
1080
1395
  AddressEnum,
1081
1396
  FunctionEnum,
1082
1397
  IndustryEnum,
1083
- EnumDisplay
1398
+ EnumDisplay,
1399
+ enumItemsToSelectValue,
1400
+ enumItemToSelectValue,
1401
+ addressEnumToSelectValue,
1402
+ addressEnumToSelectValueSingle
1084
1403
  };
1085
1404
 
1086
- export { AddressEnum, EnumDisplay, FunctionEnum, IndustryEnum, SelectAddress, SelectFunction, SelectIndustry, createAddressApi, index as default };
1405
+ export { AddressEnum, EnumDisplay, FunctionEnum, IndustryEnum, SelectAddress, SelectFunction, SelectIndustry, addressEnumToSelectValue, addressEnumToSelectValueSingle, createAddressApi, index as default, enumItemToSelectValue, enumItemsToSelectValue };
1087
1406
  //# sourceMappingURL=index.modern.js.map