utils.mjs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const AREA_EMPTY_CODE = "000000";
  2. const INHERIT_SLOTS = [
  3. "title",
  4. "cancel",
  5. "confirm",
  6. "toolbar",
  7. "columns-top",
  8. "columns-bottom"
  9. ];
  10. const INHERIT_PROPS = [
  11. "title",
  12. "loading",
  13. "readonly",
  14. "optionHeight",
  15. "swipeDuration",
  16. "visibleOptionNum",
  17. "cancelButtonText",
  18. "confirmButtonText"
  19. ];
  20. const makeOption = (text = "", value = AREA_EMPTY_CODE, children = void 0) => ({
  21. text,
  22. value,
  23. children
  24. });
  25. function formatDataForCascade({
  26. areaList,
  27. columnsNum,
  28. columnsPlaceholder: placeholder
  29. }) {
  30. const {
  31. city_list: city = {},
  32. county_list: county = {},
  33. province_list: province = {}
  34. } = areaList;
  35. const showCity = +columnsNum > 1;
  36. const showCounty = +columnsNum > 2;
  37. const getProvinceChildren = () => {
  38. if (showCity) {
  39. return placeholder.length > 1 ? [
  40. makeOption(
  41. placeholder[1],
  42. AREA_EMPTY_CODE,
  43. showCounty ? [] : void 0
  44. )
  45. ] : [];
  46. }
  47. };
  48. const provinceMap = /* @__PURE__ */ new Map();
  49. Object.keys(province).forEach((code) => {
  50. provinceMap.set(
  51. code.slice(0, 2),
  52. makeOption(province[code], code, getProvinceChildren())
  53. );
  54. });
  55. const cityMap = /* @__PURE__ */ new Map();
  56. if (showCity) {
  57. const getCityChildren = () => {
  58. if (showCounty) {
  59. return placeholder.length > 2 ? [makeOption(placeholder[2])] : [];
  60. }
  61. };
  62. Object.keys(city).forEach((code) => {
  63. const option = makeOption(city[code], code, getCityChildren());
  64. cityMap.set(code.slice(0, 4), option);
  65. const province2 = provinceMap.get(code.slice(0, 2));
  66. if (province2) {
  67. province2.children.push(option);
  68. }
  69. });
  70. }
  71. if (showCounty) {
  72. Object.keys(county).forEach((code) => {
  73. const city2 = cityMap.get(code.slice(0, 4));
  74. if (city2) {
  75. city2.children.push(makeOption(county[code], code));
  76. }
  77. });
  78. }
  79. const options = Array.from(provinceMap.values());
  80. if (placeholder.length) {
  81. const county2 = showCounty ? [makeOption(placeholder[2])] : void 0;
  82. const city2 = showCity ? [makeOption(placeholder[1], AREA_EMPTY_CODE, county2)] : void 0;
  83. options.unshift(makeOption(placeholder[0], AREA_EMPTY_CODE, city2));
  84. }
  85. return options;
  86. }
  87. export {
  88. AREA_EMPTY_CODE,
  89. INHERIT_PROPS,
  90. INHERIT_SLOTS,
  91. formatDataForCascade
  92. };