diff --git a/packages/renderless/src/tree-select/index.ts b/packages/renderless/src/tree-select/index.ts index 908ca85bdb..41dc4f3351 100644 --- a/packages/renderless/src/tree-select/index.ts +++ b/packages/renderless/src/tree-select/index.ts @@ -115,14 +115,14 @@ export const getPluginOption = */ export const getCheckedData = ({ props, state }) => - () => { + (selected) => { const checkedKey = [] - if (!Array.isArray(state.selected)) { - return props.modelValue ? [props.modelValue] : [state.selected[props.valueField]] + if (!Array.isArray(selected)) { + return props.modelValue ? [props.modelValue] : [selected[props.valueField]] } else { - state.selected.length > 0 && - state.selected.forEach((item) => { + selected.length > 0 && + selected.forEach((item) => { checkedKey.push(item[props.valueField]) }) @@ -133,31 +133,31 @@ export const getCheckedData = export const mounted = ({ api, state, props, vm }) => () => { - if (!state.value || state.value.length === 0) return + if (!state.modelValue || state.modelValue.length === 0) return if (props.multiple) { let initialNodes = [] - if (Array.isArray(state.value)) { - state.value.forEach((value) => { + if (Array.isArray(state.modelValue)) { + state.modelValue.forEach((value) => { const option = api.getPluginOption(value) initialNodes = initialNodes.concat(option) }) } - vm.$refs.baseSelectRef.updateSelectedData( - initialNodes.map((node) => { - return { - ...node, - currentLabel: node[props.textField], - value: node[props.valueField], - isTree: true - } - }) - ) + const selected = initialNodes.map((node) => { + return { + ...node, + currentLabel: node[props.textField], + value: node[props.valueField], + isTree: true + } + }) + + vm.$refs.baseSelectRef.updateSelectedData(selected) - state.defaultCheckedKeys = api.getCheckedData()[0] + state.defaultCheckedKeys = api.getCheckedData(selected) } else { - const data = api.getPluginOption(state.value)[0] + const data = api.getPluginOption(state.modelValue)[0] vm.$refs.baseSelectRef.updateSelectedData({ ...data, currentLabel: data[props.textField], @@ -170,3 +170,30 @@ export const mounted = state.currentKey = data[props.valueField] } } + +export const watchValue = + ({ api, props, vm, state }) => + (newValue, oldValue) => { + if (props.multiple) { + let initialNodes = [] + if (Array.isArray(newValue)) { + newValue.forEach((value) => { + const option = api.getPluginOption(value) + initialNodes = initialNodes.concat(option) + }) + } + + const selected = initialNodes.map((node) => { + return { + ...node, + currentLabel: node[props.textField], + value: node[props.valueField], + isTree: true + } + }) + + vm.$refs.baseSelectRef.updateSelectedData(selected) + + vm.$refs.treeRef.setCheckedKeys(newValue) + } + } diff --git a/packages/renderless/src/tree-select/vue.ts b/packages/renderless/src/tree-select/vue.ts index 838df28186..e602dbed23 100644 --- a/packages/renderless/src/tree-select/vue.ts +++ b/packages/renderless/src/tree-select/vue.ts @@ -1,4 +1,4 @@ -import { check, filter, getCheckedData, getPluginOption, getTreeData, mounted, nodeClick } from './index' +import { check, filter, getCheckedData, getPluginOption, getTreeData, mounted, nodeClick, watchValue } from './index' export const api = ['state', 'check', 'filter', 'nodeClick'] @@ -11,7 +11,7 @@ export const renderless = (props, { reactive, computed, watch, onMounted }, { vm defaultCheckedKeys: [], remoteData: [], treeData: props.treeOp.data, - value: computed(() => props.modelValue) + modelValue: [] }) Object.assign(api, { @@ -22,7 +22,8 @@ export const renderless = (props, { reactive, computed, watch, onMounted }, { vm getPluginOption: getPluginOption({ api, props, state }), getTreeData: getTreeData({ props, state }), mounted: mounted({ api, state, props, vm }), - nodeClick: nodeClick({ props, vm, emit }) + nodeClick: nodeClick({ props, vm, emit }), + watchValue: watchValue({ api, props, vm, state }) }) watch( @@ -31,6 +32,20 @@ export const renderless = (props, { reactive, computed, watch, onMounted }, { vm { immediate: true, deep: true } ) + watch( + () => props.modelValue, + () => { + if (props.multiple && Array.isArray(props.modelValue)) { + state.modelValue = [...props.modelValue] + } else { + state.modelValue = props.modelValue + } + }, + { immediate: true, deep: true } + ) + + watch(() => state.modelValue, api.watchValue) + onMounted(api.mounted) return api diff --git a/packages/vue/src/tree-select/src/pc.vue b/packages/vue/src/tree-select/src/pc.vue index a0c7ee505e..b70618f02d 100644 --- a/packages/vue/src/tree-select/src/pc.vue +++ b/packages/vue/src/tree-select/src/pc.vue @@ -2,7 +2,7 @@