Files
element-plus/packages/components/tree-select/src/select.ts
虞金攀 904aa0e21b feat(components): add tree select component (#6843)
* feat(ElTreeSelect): add tree select base component

* refactor(ElTreeSelect): use render function and move select/tree props to them self module

* fix(ElTreeSelect): init value not checked

* fix(ElTreeSelect): `toArray` ignores valid values

* fix(ElTreeSelect): expose not working when defined on mounted

* fix(ElTreeSelect): watch `modelValue` deep

* test(ElTreeSelect): add base unit test

* perf(ElTreeSelect): default slot should be a function

* fix(ElTreeSelect): `onNodeClick` can not call,

* test(ElTreeSelect): update unit test

* fix(ElTreeSelect): `onNodeClick` can not call,

* fix(ElTreeSelect): remove folder node when `checkStrictly` is false

* feat(ElTreeSelect): export `ElTreeSelect`

* fix(ElTreeSelect): `filterMethod` conflicts with `filterNodeMethod`

* docs(ElTreeSelect): add component docs

* fix(ElTreeSelect): fix lint

* docs(ElTreeSelect): fix lazy loading requires non-leaf nodes, and change mock labels

* docs(ElTreeSelect): the link address of the attributes is incorrect

* docs(ElTreeSelect): `dropdown` doesn't need the `-` symbol

* refactor(ElTreeSelect): use alias path and make sure vue is above to components

* refactor(ElTreeSelect): use a unified namespace for styles

* docs(ElTreeSelect): change option labels in default slots

* refactor(ElTreeSelect): import `ElOption` using unified entry and change the way to override the select click event

* style(ElTreeSelect): sort imports

* docs(ElTreeSelect): update the documentation for special codes

* refactor(ElTreeSelect): keep it consistent with the select style

* refactor(ElTreeSelect): use `isFunction` from `@element-plus/utils`

* refactor(ElTreeSelect): use single closing tag when no subset

* docs(ElTreeSelect): set `TreeSelect` promotion as `2.1.8`
2022-04-02 15:15:33 +08:00

51 lines
1.3 KiB
TypeScript

import { computed, nextTick, toRefs } from 'vue'
import { pick } from 'lodash-unified'
import ElSelect from '@element-plus/components/select'
import { useNamespace } from '@element-plus/hooks'
import type { Ref } from 'vue'
import type ElTree from '@element-plus/components/tree'
export const useSelect = (
props,
{ attrs },
{
tree,
key,
}: {
select: Ref<InstanceType<typeof ElSelect> | undefined>
tree: Ref<InstanceType<typeof ElTree> | undefined>
key: Ref<string>
}
) => {
const ns = useNamespace('tree-select')
const result = {
...pick(toRefs(props), Object.keys(ElSelect.props)),
...attrs,
valueKey: key,
popperClass: computed(() => {
const classes = [ns.e('popper')]
if (props.popperClass) classes.push(props.popperClass)
return classes.join(' ')
}),
filterMethod: (keyword = '') => {
if (props.filterMethod) props.filterMethod(keyword)
nextTick(() => {
// let tree node expand only, same with tree filter
tree.value?.filter(keyword)
})
},
// clear filter text when visible change
onVisibleChange: (visible: boolean) => {
attrs.onVisibleChange?.(visible)
if (props.filterable && visible) {
result.filterMethod()
}
},
}
return result
}