@kne/super-select-plus 0.1.1 → 0.1.3

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({
@@ -416,6 +638,7 @@ const SelectFunctionInner = _ref => {
416
638
  defaultMessage: '请选择职能'
417
639
  }),
418
640
  isPopup: isPopup,
641
+ overlayWidth: overlayWidth,
419
642
  menuItemWidth: 200,
420
643
  style: _extends({
421
644
  width: overlayWidth
@@ -528,6 +751,38 @@ const SelectIndustryInner = _ref => {
528
751
  });
529
752
  }, []);
530
753
  const options = useMemo(() => transformToCascaderData(data, locale), [data, locale]);
754
+ const flatMapping = useMemo(() => {
755
+ const mapping = new Map();
756
+ data.filter(item => item.code !== '000').forEach(item => {
757
+ mapping.set(item.code, _extends({}, item, {
758
+ id: item.code,
759
+ name: getLabelForLocal$2(item, locale)
760
+ }));
761
+ });
762
+ return mapping;
763
+ }, [data, locale]);
764
+ const normalizedValue = useMemo(() => resolveCascaderValueFromMap(value, flatMapping, {
765
+ valueKey: 'id',
766
+ labelKey: 'name',
767
+ single
768
+ }), [value, flatMapping, single]);
769
+ useEffect(() => {
770
+ if (!onChange || !flatMapping.size) {
771
+ return;
772
+ }
773
+ const resolved = resolveCascaderValueFromMap(value, flatMapping, {
774
+ valueKey: 'id',
775
+ labelKey: 'name',
776
+ single
777
+ });
778
+ if (shouldSyncSelectValue(value, resolved, {
779
+ valueKey: 'id',
780
+ labelKey: 'name',
781
+ single
782
+ })) {
783
+ onChange(resolved);
784
+ }
785
+ }, [value, flatMapping, single, onChange]);
531
786
  const handleSearch = (searchText, {
532
787
  mapping
533
788
  }) => {
@@ -538,7 +793,7 @@ const SelectIndustryInner = _ref => {
538
793
  });
539
794
  };
540
795
  return /*#__PURE__*/jsx(SelectCascader, _extends({}, props, {
541
- value: value,
796
+ value: normalizedValue,
542
797
  onChange: onChange,
543
798
  single: single,
544
799
  placeholder: placeholder || formatMessage({
@@ -547,6 +802,7 @@ const SelectIndustryInner = _ref => {
547
802
  defaultMessage: '请选择行业'
548
803
  }),
549
804
  isPopup: isPopup,
805
+ overlayWidth: overlayWidth,
550
806
  menuItemWidth: 200,
551
807
  style: _extends({
552
808
  width: overlayWidth
@@ -561,8 +817,8 @@ const SelectIndustry = withLocale(SelectIndustryInner);
561
817
  SelectIndustry.defaultData = defaultIndustryData;
562
818
  SelectIndustry.Enum = IndustryEnum;
563
819
 
564
- const _excluded$1 = ["data", "name", "children", "displayParent"],
565
- _excluded2 = ["displayParent"];
820
+ const _excluded$1 = ["data", "name", "names", "children", "displayParent"],
821
+ _excluded2$1 = ["displayParent"];
566
822
  const getLabelForLocal$1 = (item, locale) => {
567
823
  if (locale === 'en-US') {
568
824
  return get(item, 'enName') || get(item, 'name');
@@ -590,6 +846,7 @@ const AddressEnumInner = withFetch(_ref => {
590
846
  let {
591
847
  data,
592
848
  name,
849
+ names,
593
850
  children,
594
851
  displayParent
595
852
  } = _ref,
@@ -598,6 +855,33 @@ const AddressEnumInner = withFetch(_ref => {
598
855
  locale
599
856
  } = useIntl();
600
857
  const addressApi = useMemo(() => createAddressApi(data), [data]);
858
+ const formatCityData = cityData => {
859
+ const {
860
+ city,
861
+ parent
862
+ } = cityData;
863
+ if (!city) {
864
+ return '';
865
+ }
866
+ if (displayParent && parent) {
867
+ return `${getLabelForLocal$1(parent, locale)}·${getLabelForLocal$1(city, locale)}`;
868
+ }
869
+ return getLabelForLocal$1(city, locale);
870
+ };
871
+ if (names != null && names.length) {
872
+ const outputs = names.map(code => addressApi.getCity(code));
873
+ const labels = outputs.map(formatCityData).filter(Boolean);
874
+ if (children) {
875
+ return children(outputs, _extends({
876
+ displayParent,
877
+ locale,
878
+ getLabelForLocal: getLabelForLocal$1,
879
+ names,
880
+ labels
881
+ }, props));
882
+ }
883
+ return labels.toString();
884
+ }
601
885
 
602
886
  // 获取城市数据
603
887
  const cityData = addressApi.getCity(name);
@@ -628,7 +912,7 @@ const AddressEnum = withLocale(props => {
628
912
  const {
629
913
  displayParent = false
630
914
  } = props,
631
- restProps = _objectWithoutPropertiesLoose(props, _excluded2);
915
+ restProps = _objectWithoutPropertiesLoose(props, _excluded2$1);
632
916
  return /*#__PURE__*/jsx(AddressEnumInner, _extends({}, addressDefaultApi, {
633
917
  displayParent: displayParent
634
918
  }, restProps));
@@ -717,7 +1001,8 @@ var classnames = createCommonjsModule(function (module) {
717
1001
 
718
1002
  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
1003
 
720
- const _excluded = ["single", "isPopup", "showChinaQuan", "showForeignQuan"];
1004
+ const _excluded = ["value", "single", "onChange", "overlayWidth"],
1005
+ _excluded2 = ["single", "isPopup", "showChinaQuan", "showForeignQuan"];
721
1006
  const getLabelForLocal = (item, locale) => {
722
1007
  if (locale === 'en-US') {
723
1008
  return get(item, 'enName') || get(item, 'name');
@@ -1032,21 +1317,55 @@ const SelectAddressInner = /*#__PURE__*/forwardRef((props, ref) => {
1032
1317
  const {
1033
1318
  formatMessage
1034
1319
  } = useIntl();
1320
+ const {
1321
+ value,
1322
+ single,
1323
+ onChange,
1324
+ overlayWidth = 500
1325
+ } = props,
1326
+ restProps = _objectWithoutPropertiesLoose(props, _excluded);
1327
+ const [cityData, setCityData] = useState(null);
1328
+ useEffect(() => {
1329
+ defaultCityData().then(setCityData);
1330
+ }, []);
1331
+ const addressApi = useMemo(() => cityData ? createAddressApi(cityData) : null, [cityData]);
1332
+ const normalizedValue = useMemo(() => resolveAddressValue(value, addressApi, {
1333
+ single
1334
+ }), [value, addressApi, single]);
1335
+ useEffect(() => {
1336
+ if (!onChange || !addressApi) {
1337
+ return;
1338
+ }
1339
+ const resolved = resolveAddressValue(value, addressApi, {
1340
+ single
1341
+ });
1342
+ if (shouldSyncSelectValue(value, resolved, {
1343
+ valueKey: 'value',
1344
+ labelKey: 'label',
1345
+ single
1346
+ })) {
1347
+ onChange(resolved);
1348
+ }
1349
+ }, [value, addressApi, single, onChange]);
1035
1350
  return /*#__PURE__*/jsx(SelectInput, _extends({
1036
1351
  ref: ref
1037
- }, props, {
1038
- placeholder: props.placeholder || formatMessage({
1352
+ }, restProps, {
1353
+ single: single,
1354
+ value: normalizedValue,
1355
+ onChange: onChange,
1356
+ overlayWidth: overlayWidth,
1357
+ placeholder: restProps.placeholder || formatMessage({
1039
1358
  id: 'addressPlaceholder'
1040
1359
  }, {
1041
1360
  defaultMessage: '请选择城市'
1042
1361
  }),
1043
1362
  children: contextProps => {
1044
1363
  const {
1045
- value,
1364
+ value: currentValue,
1046
1365
  setValue
1047
1366
  } = contextProps;
1048
1367
  return /*#__PURE__*/jsx(AddressInner, {
1049
- value: value,
1368
+ value: currentValue,
1050
1369
  setValue: setValue,
1051
1370
  props: props
1052
1371
  });
@@ -1060,7 +1379,7 @@ const SelectAddress = withLocale(_ref => {
1060
1379
  showChinaQuan = false,
1061
1380
  showForeignQuan = false
1062
1381
  } = _ref,
1063
- props = _objectWithoutPropertiesLoose(_ref, _excluded);
1382
+ props = _objectWithoutPropertiesLoose(_ref, _excluded2);
1064
1383
  return /*#__PURE__*/jsx(SelectAddressInner, _extends({
1065
1384
  single: single,
1066
1385
  isPopup: isPopup,
@@ -1080,8 +1399,12 @@ var index = {
1080
1399
  AddressEnum,
1081
1400
  FunctionEnum,
1082
1401
  IndustryEnum,
1083
- EnumDisplay
1402
+ EnumDisplay,
1403
+ enumItemsToSelectValue,
1404
+ enumItemToSelectValue,
1405
+ addressEnumToSelectValue,
1406
+ addressEnumToSelectValueSingle
1084
1407
  };
1085
1408
 
1086
- export { AddressEnum, EnumDisplay, FunctionEnum, IndustryEnum, SelectAddress, SelectFunction, SelectIndustry, createAddressApi, index as default };
1409
+ export { AddressEnum, EnumDisplay, FunctionEnum, IndustryEnum, SelectAddress, SelectFunction, SelectIndustry, addressEnumToSelectValue, addressEnumToSelectValueSingle, createAddressApi, index as default, enumItemToSelectValue, enumItemsToSelectValue };
1087
1410
  //# sourceMappingURL=index.modern.js.map