index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <template>
  2. <div :style="{width}">
  3. <el-cascader
  4. v-model="proxyValue"
  5. ref="cascader"
  6. :placeholder="placeholder"
  7. :options="options"
  8. :show-all-levels="showAllLevels"
  9. :props="optionProps"
  10. :disabled="disabled"
  11. :filterable="filterable"
  12. :style="{width}"
  13. popper-class="cip-popper-class"
  14. :clearable="clearable" />
  15. </div>
  16. </template>
  17. <script>
  18. import { ElCascader } from 'element-plus'
  19. import { useFormInput, useOptions } from '@cip/components/hooks/form-input'
  20. import { formInputProps, fromInputEmits } from '../../form-input-props'
  21. import { computed, ref } from 'vue'
  22. export default {
  23. components: { ElCascader },
  24. props: formInputProps,
  25. emits: [...fromInputEmits],
  26. setup (props, context) {
  27. const cascader = ref()
  28. const { securityConfig, proxyValue, updateStream, ...formInput } = useFormInput(props, context, {
  29. fromModelValue: (modelValue) => getValue(modelValue),
  30. toModelValue: (value) => {
  31. const modelValue = getModelValue(value)
  32. if (otherKey.value) {
  33. const node = cascader.value.getCheckedNodes()
  34. const otherValue = node.map(i => i[optionProps.value.label]).join(splitKey.value)
  35. updateStream.appendOtherValue(otherValue)
  36. updateStream.appendOtherValue(node, 2)
  37. }
  38. return modelValue
  39. },
  40. maxOtherKey: 2
  41. })
  42. // 是否多选,ElCascader的多选比较特殊,multiple是在props里面,所以特殊处理
  43. const multiple = computed(() => {
  44. return securityConfig.value.multiple ?? false
  45. })
  46. const { options, getValue, getModelValue } = useOptions(props, multiple, updateStream)
  47. const showAllLevels = computed(() => {
  48. return securityConfig.value.showAllLevels
  49. })
  50. const splitKey = computed(() => {
  51. return props.config?.splitKey ?? ','
  52. })
  53. // 另一个键
  54. const otherKey = computed(() => {
  55. return securityConfig.value.otherKey ?? ''
  56. })
  57. // 过滤
  58. const filterable = computed(() => {
  59. return securityConfig.value.filterable ?? false
  60. })
  61. // 懒加载在optionProps中配置
  62. const optionProps = computed(() => {
  63. return {
  64. value: 'value',
  65. label: 'label',
  66. children: 'children',
  67. multiple: multiple.value,
  68. ...securityConfig.value.optionProps
  69. }
  70. })
  71. return {
  72. ...formInput,
  73. proxyValue,
  74. options,
  75. filterable,
  76. optionProps,
  77. showAllLevels,
  78. cascader
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="less">
  84. .@{elNamespace}-cascader-panel{
  85. max-height: 50vh;
  86. overflow-y: scroll;
  87. }
  88. </style>