Skip to content

Commit

Permalink
feat(SelectTree, SelectCascader): expandedKeys 支持 v-model
Browse files Browse the repository at this point in the history
  • Loading branch information
1zumii committed Apr 15, 2024
1 parent 83150c2 commit 91a355b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
25 changes: 16 additions & 9 deletions components/select-cascader/selectCascader.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
<Cascader
v-show="!filterText"
ref="cascaderRef"
v-model:expandedKeys="expandedKeys"
:selectedKeys="selectedKeys"
:checkedKeys="checkedKeys"
:initLoadKeys="initLoadKeys"
:data="data"
:emptyText="listEmptyText"
:expandedKeys="expandedKeys"
:selectable="cascaderSelectable"
:checkable="cascaderCheckable"
:checkStrictly="checkStrictly"
Expand Down Expand Up @@ -161,6 +161,7 @@ export default defineComponent({
emits: [
UPDATE_MODEL_EVENT,
CHANGE_EVENT,
'update:expandedKeys',
'removeTag',
'visibleChange',
'focus',
Expand All @@ -173,6 +174,7 @@ export default defineComponent({
valueType: computed(() => (props.multiple ? 'array' : 'string')),
});
const isOpened = ref(false);
const [currentValue, updateCurrentValue] = props.multiple
? // 与 props 中 modelValue 类型保持一致
(useArrayModel(props, emit) as unknown as UseArrayModelReturn<
Expand Down Expand Up @@ -248,14 +250,19 @@ export default defineComponent({
}
return [];
});
const expandedKeys = computed(() => {
if (!props.multiple) {
return getExpandedKeysBySelectedKeys(
nodeList.value,
selectedKeys.value as CascaderNodeKey[],
);
}
return [];
const expandedKeys = computed({
get: () => {
if (!props.multiple) {
return getExpandedKeysBySelectedKeys(
nodeList.value,
selectedKeys.value as CascaderNodeKey[],
);
}
return [];
},
set: (nextValue) => {
emit('update:expandedKeys', nextValue);
},
});
const checkedKeys = computed(() => {
if (props.multiple) {
Expand Down
11 changes: 9 additions & 2 deletions components/select-tree/selectTree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
<Tree
v-show="data.length"
ref="refTree"
v-model:expandedKeys="currentExpandedKeys"
:selectedKeys="selectedKeys"
:checkedKeys="checkedKeys"
:data="data"
:defaultExpandAll="defaultExpandAll"
:expandedKeys="expandedKeys"
:accordion="accordion"
:selectable="treeSelectable"
:checkable="treeCheckable"
Expand Down Expand Up @@ -83,11 +83,11 @@
<Tree
v-show="data.length"
ref="refTree"
v-model:expandedKeys="currentExpandedKeys"
:selectedKeys="selectedKeys"
:checkedKeys="checkedKeys"
:data="data"
:defaultExpandAll="defaultExpandAll"
:expandedKeys="expandedKeys"
:accordion="accordion"
:selectable="treeSelectable"
:checkable="treeCheckable"
Expand Down Expand Up @@ -194,6 +194,7 @@ export default defineComponent({
emits: [
UPDATE_MODEL_EVENT,
CHANGE_EVENT,
'update:expandedKeys',
'removeTag',
'visibleChange',
'focus',
Expand All @@ -206,12 +207,17 @@ export default defineComponent({
valueType: computed(() => (props.multiple ? 'array' : 'string')),
});
const isOpened = ref(false);
const [currentValue, updateCurrentValue] = props.multiple
? // 与 props 中 modelValue 类型保持一致
(useArrayModel(props, emit) as unknown as UseArrayModelReturn<
Array<TreeNodeKey> | Array<Array<TreeNodeKey>>
>)
: useNormalModel(props, emit);
const [currentExpandedKeys] = useNormalModel(props, emit, {
prop: 'expandedKeys',
});
const filterText = ref('');
const { t } = useLocale();
Expand Down Expand Up @@ -434,6 +440,7 @@ export default defineComponent({
prefixCls,
isOpened,
currentValue,
currentExpandedKeys,
handleRemove,
handleClear,
selectedOptions,
Expand Down
19 changes: 6 additions & 13 deletions docs/.vitepress/components/selectTree/common.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<FSelectTree :modelValue="'40'" :data="data"></FSelectTree>
<FSelectTree :modelValue="'40'" :data="data" />
</template>
<script>
<script setup>
import { ref, h } from 'vue';
// eslint-disable-next-line import/no-unresolved
import { PictureOutlined, PlusCircleOutlined } from '@fesjs/fes-design/icon';
Expand All @@ -27,17 +27,10 @@ function createLabel(level) {
if (level === 1) return '三生万物';
}
export default {
setup() {
const data = ref([]);
setTimeout(() => {
data.value = createData(4);
});
return {
data,
};
},
};
const data = ref([]);
setTimeout(() => {
data.value = createData(4);
});
</script>
<style scoped>
.fes-select-tree {
Expand Down

0 comments on commit 91a355b

Please sign in to comment.