l-cascade.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <l-picker
  3. ref="pickerRef"
  4. v-model="innerValue"
  5. :cancelBtn="cancelBtn"
  6. :cancelStyle="cancelStyle"
  7. :confirmBtn="confirmBtn"
  8. :confirmStyle="confirmStyle"
  9. :title="title"
  10. :titleStyle="titleStyle"
  11. :loading="loading"
  12. :loadingColor="loadingColor"
  13. :loadingMaskColor="loadingMaskColor"
  14. :loadingSize="loadingSize"
  15. :itemHeight="itemHeight"
  16. :itemColor="itemColor"
  17. :itemFontSize="itemFontSize"
  18. :itemActiveColor="itemActiveColor"
  19. :indicatorStyle="indicatorStyle"
  20. :bgColor="bgColor"
  21. :groupHeight="groupHeight"
  22. :radius="radius"
  23. :resetIndex="resetIndex"
  24. :columns="innerColumns"
  25. @cancel="onCancel"
  26. @confirm="onConfirm"
  27. @pick="onPick">
  28. </l-picker>
  29. </template>
  30. <script lang="ts">
  31. // @ts-nocheck
  32. /**
  33. * Cascade 级联选择器组件
  34. * @description 支持多层级联数据选择,适用于地址选择、分类选择等场景
  35. * <br>插件类型:LCascadeComponentPublicInstance
  36. * @tutorial https://ext.dcloud.net.cn/plugin?name=lime-cascade
  37. *
  38. * @property {string} cancelBtn 取消按钮文字(默认:"取消")
  39. * @property {string} cancelStyle 取消按钮样式(支持CSS字符串)
  40. * @property {string} confirmBtn 确定按钮文字(默认:"确定")
  41. * @property {string} confirmStyle] 确定按钮样式(支持CSS字符串)
  42. * @property {string} title 标题文字
  43. * @property {string} titleStyle 标题样式(支持CSS字符串)
  44. * @property {UTSJSONObject} keys 字段别名配置(参考KeysType结构)
  45. * @property {UTSJSONObject]} columns 级联数据源(必填)
  46. * @property {PickerValue]} modelValue 选中值(支持v-model)
  47. * @property {PickerValue]} defaultValue 默认选中值
  48. * @property {PickerValue]} value 选中值(兼容旧版)
  49. * @property {boolean} loading 是否显示加载状态
  50. * @property {string} loadingColor 加载图标颜色(默认:主题色)
  51. * @property {string} loadingMaskColor 加载遮罩颜色(默认:rgba(255,255,255,0.8))
  52. * @property {string} loadingSize 加载图标尺寸(支持CSS单位)
  53. * @property {string} itemHeight 选项行高(默认:44px)
  54. * @property {string} itemColor 选项文字颜色(默认:#333)
  55. * @property {string} itemFontSize 选项文字大小(默认:16px)
  56. * @property {string} itemActiveColor 选中项高亮颜色(默认:主题色)
  57. * @property {string} indicatorStyle 指示器样式(支持CSS字符串)
  58. * @property {string} bgColor 背景颜色(默认:#ffffff)
  59. * @property {string} groupHeight 选项组高度(默认:240px)
  60. * @property {string} radius 圆角半径(支持CSS单位)
  61. * @property {boolean} resetIndex 是否重置选中索引(用于动态更新数据时)
  62. * @event {Function} change 选项变化时触发(返回当前选中路径)
  63. * @event {Function} cancel 点击关闭时触发
  64. * @event {Function} confirm 点击确定时触发(返回最终选中值)
  65. */
  66. import { defineComponent, ref, computed} from '@/uni_modules/lime-shared/vue';
  67. import type { PickerValue, PickerColumn, PickerColumnItem, PickerPickEvent} from '../l-picker/type';
  68. import { CascadeProps } from './type';
  69. import { parseKeys, formatCascadeColumns } from './utils';
  70. import cascadeProps from '../l-picker/props';
  71. export default defineComponent({
  72. name: 'l-cascade',
  73. props:cascadeProps,
  74. emits: ['change', 'cancel', 'pick', 'confirm', 'update:modelValue', 'update:value', 'input'],
  75. setup(props, {emit, expose}) {
  76. type UTSJSONObject = Record<string, any>
  77. const keys = parseKeys(props.keys)
  78. const curValueArray = ref(props.value || props.modelValue || props.defaultValue||[]);
  79. const innerValue = computed({
  80. set(value: PickerValue[]) {
  81. curValueArray.value = value;
  82. emit('update:modelValue', value)
  83. // #ifdef VUE2
  84. emit('input', value)
  85. // #endif
  86. },
  87. get(): PickerValue[]{
  88. return props.value || props.modelValue || curValueArray.value
  89. }
  90. } as WritableComputedOptions<PickerValue[]>)
  91. const innerColumns = computed((): PickerColumn[] => {
  92. return formatCascadeColumns(props.columns, keys, innerValue)
  93. })
  94. const onPick = ({values, column, index} : PickerPickEvent) => {
  95. innerValue.value = values
  96. }
  97. const onConfirm = (options: PickerConfirmEvent) => {
  98. emit('confirm', options)
  99. }
  100. const onCancel = () => {
  101. emit('cancel')
  102. }
  103. const pickerRef = ref<ComponentPublicInstance|null>(null)
  104. const confirm = () => {
  105. pickerRef.value?.confirm()
  106. }
  107. const getSelectedOptions = ():PickerConfirmEvent => {
  108. return pickerRef.value!.getSelectedOptions()
  109. }
  110. // #ifdef VUE3
  111. expose({
  112. confirm,
  113. getSelectedOptions
  114. })
  115. // #endif
  116. return {
  117. innerValue,
  118. innerColumns,
  119. onPick,
  120. onConfirm,
  121. onCancel,
  122. // #ifdef VUE2
  123. confirm,
  124. getSelectedOptions
  125. // #endif
  126. }
  127. }
  128. })
  129. </script>
  130. <style>
  131. </style>