index.vue 2.5 KB

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