l-cascade.uvue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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="uts" setup>
  31. /**
  32. * Cascade 级联选择器组件
  33. * @description 支持多层级联数据选择,适用于地址选择、分类选择等场景
  34. * <br>插件类型:LCascadeComponentPublicInstance
  35. * @tutorial https://ext.dcloud.net.cn/plugin?name=lime-cascade
  36. *
  37. * @property {string} cancelBtn 取消按钮文字(默认:"取消")
  38. * @property {string} cancelStyle 取消按钮样式(支持CSS字符串)
  39. * @property {string} confirmBtn 确定按钮文字(默认:"确定")
  40. * @property {string} confirmStyle] 确定按钮样式(支持CSS字符串)
  41. * @property {string} title 标题文字
  42. * @property {string} titleStyle 标题样式(支持CSS字符串)
  43. * @property {UTSJSONObject} keys 字段别名配置(参考KeysType结构)
  44. * @property {UTSJSONObject]} columns 级联数据源(必填)
  45. * @property {PickerValue]} modelValue 选中值(支持v-model)
  46. * @property {PickerValue]} defaultValue 默认选中值
  47. * @property {PickerValue]} value 选中值(兼容旧版)
  48. * @property {boolean} loading 是否显示加载状态
  49. * @property {string} loadingColor 加载图标颜色(默认:主题色)
  50. * @property {string} loadingMaskColor 加载遮罩颜色(默认:rgba(255,255,255,0.8))
  51. * @property {string} loadingSize 加载图标尺寸(支持CSS单位)
  52. * @property {string} itemHeight 选项行高(默认:44px)
  53. * @property {string} itemColor 选项文字颜色(默认:#333)
  54. * @property {string} itemFontSize 选项文字大小(默认:16px)
  55. * @property {string} itemActiveColor 选中项高亮颜色(默认:主题色)
  56. * @property {string} indicatorStyle 指示器样式(支持CSS字符串)
  57. * @property {string} bgColor 背景颜色(默认:#ffffff)
  58. * @property {string} groupHeight 选项组高度(默认:240px)
  59. * @property {string} radius 圆角半径(支持CSS单位)
  60. * @property {boolean} resetIndex 是否重置选中索引(用于动态更新数据时)
  61. * @event {Function} change 选项变化时触发(返回当前选中路径)
  62. * @event {Function} cancel 点击关闭时触发
  63. * @event {Function} confirm 点击确定时触发(返回最终选中值)
  64. */
  65. import type { PickerValue, PickerColumn, PickerColumnItem, PickerPickEvent, PickerConfirmEvent } from '../l-picker/type';
  66. import type { CascadeProps } from './type';
  67. import { parseKeys, formatCascadeColumns } from './utils';
  68. const emit = defineEmits(['change', 'cancel', 'pick', 'confirm', 'update:modelValue', 'update:value'])
  69. const props = withDefaults(defineProps<CascadeProps>(), {
  70. columns: [] as PickerColumnItem[],
  71. loading: false,
  72. resetIndex: false,
  73. loadingSize: '64rpx'
  74. })
  75. const keys = parseKeys(props.keys)
  76. const innerValue = ref<PickerValue[]>(props.value ?? props.modelValue ?? props.defaultValue ?? []);
  77. // const innerValue = computed({
  78. // set(value : PickerValue[]) {
  79. // curValueArray.value = value;
  80. // emit('update:modelValue', value)
  81. // },
  82. // get() : PickerValue[] {
  83. // return props.value ?? props.modelValue ?? curValueArray.value
  84. // }
  85. // } as WritableComputedOptions<PickerValue[]>)
  86. const innerColumns = computed(() : PickerColumn[] => {
  87. return formatCascadeColumns(props.columns, keys, innerValue)
  88. })
  89. const onPick = (options : PickerPickEvent) => {
  90. const { values, column, index } = options
  91. innerValue.value = [...values]
  92. }
  93. const onConfirm = (options : PickerConfirmEvent) => {
  94. emit('confirm', options)
  95. }
  96. const onCancel = () => {
  97. emit('cancel')
  98. }
  99. watchEffect(()=>{
  100. emit('update:modelValue', innerValue.value)
  101. })
  102. const pickerRef = ref<ComponentPublicInstance|null>(null)
  103. defineExpose({
  104. confirm: () => {
  105. pickerRef.value?.$callMethod('confirm')
  106. },
  107. getSelectedOptions: ():PickerConfirmEvent|null => {
  108. return pickerRef.value?.$callMethod('getSelectedOptions') as PickerConfirmEvent|null
  109. }
  110. })
  111. </script>
  112. <style>
  113. </style>