tree-select.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <script lang="ts">
  2. // @ts-nocheck
  3. import { computed, defineComponent, h, onMounted, reactive, ref } from 'vue'
  4. import { pick } from 'lodash-unified'
  5. import ElSelect from '@element-plus/components/select'
  6. import ElTree from '@element-plus/components/tree'
  7. import { useSelect } from './select'
  8. import { useTree } from './tree'
  9. import CacheOptions from './cache-options'
  10. export default defineComponent({
  11. name: 'ElTreeSelect',
  12. // disable `ElSelect` inherit current attrs
  13. inheritAttrs: false,
  14. props: {
  15. ...ElSelect.props,
  16. ...ElTree.props,
  17. cacheData: {
  18. type: Array,
  19. default: () => [],
  20. },
  21. },
  22. setup(props, context) {
  23. const { slots, expose } = context
  24. const select = ref<InstanceType<typeof ElSelect>>()
  25. const tree = ref<InstanceType<typeof ElTree>>()
  26. const key = computed(() => props.nodeKey || props.valueKey || 'value')
  27. const selectProps = useSelect(props, context, { select, tree, key })
  28. const { cacheOptions, ...treeProps } = useTree(props, context, {
  29. select,
  30. tree,
  31. key,
  32. })
  33. // expose ElTree/ElSelect methods
  34. const methods = reactive({})
  35. expose(methods)
  36. onMounted(() => {
  37. Object.assign(methods, {
  38. ...pick(tree.value, [
  39. 'filter',
  40. 'updateKeyChildren',
  41. 'getCheckedNodes',
  42. 'setCheckedNodes',
  43. 'getCheckedKeys',
  44. 'setCheckedKeys',
  45. 'setChecked',
  46. 'getHalfCheckedNodes',
  47. 'getHalfCheckedKeys',
  48. 'getCurrentKey',
  49. 'getCurrentNode',
  50. 'setCurrentKey',
  51. 'setCurrentNode',
  52. 'getNode',
  53. 'remove',
  54. 'append',
  55. 'insertBefore',
  56. 'insertAfter',
  57. ]),
  58. ...pick(select.value, ['focus', 'blur']),
  59. })
  60. })
  61. return () =>
  62. h(
  63. ElSelect,
  64. /**
  65. * 1. The `props` is processed into `Refs`, but `v-bind` and
  66. * render function props cannot read `Refs`, so use `reactive`
  67. * unwrap the `Refs` and keep reactive.
  68. * 2. The keyword `ref` requires `Ref`, but `reactive` broke it,
  69. * so use function.
  70. */
  71. reactive({
  72. ...selectProps,
  73. ref: (ref) => (select.value = ref),
  74. }),
  75. {
  76. ...slots,
  77. default: () => [
  78. h(CacheOptions, { data: cacheOptions.value }),
  79. h(
  80. ElTree,
  81. reactive({
  82. ...treeProps,
  83. ref: (ref) => (tree.value = ref),
  84. })
  85. ),
  86. ],
  87. }
  88. )
  89. },
  90. })
  91. </script>