Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tree-select): [tree-select] fix deleteTag not working #2543

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 47 additions & 20 deletions packages/renderless/src/tree-select/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
})

Expand All @@ -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],
Expand All @@ -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)
}
}
Comment on lines +174 to +199
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle single selection mode and value removal

The current implementation has several limitations:

  1. Only handles multiple selection mode
  2. Doesn't properly handle value removal (the original bug)
  3. Potential race condition between updateSelectedData and setCheckedKeys

Consider this enhanced implementation:

 export const watchValue =
   ({ api, props, vm, state }) =>
   (newValue, oldValue) => {
+    // Handle value removal
+    if (!newValue || (Array.isArray(newValue) && newValue.length === 0)) {
+      vm.$refs.baseSelectRef.updateSelectedData([])
+      vm.$refs.treeRef.setCheckedKeys([])
+      return
+    }
+
     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)
+    } else {
+      const data = api.getPluginOption(newValue)[0]
+      if (!data) {
+        vm.$refs.baseSelectRef.updateSelectedData(null)
+        return
+      }
+      vm.$refs.baseSelectRef.updateSelectedData({
+        ...data,
+        currentLabel: data[props.textField],
+        value: data[props.valueField],
+        state: {
+          currentLabel: data[props.textField]
+        }
+      })
     }
   }

This implementation:

  • Properly handles value removal (fixes the deleteTag bug)
  • Adds support for single selection mode
  • Adds null checks
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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)
}
}
export const watchValue =
({ api, props, vm, state }) =>
(newValue, oldValue) => {
// Handle value removal
if (!newValue || (Array.isArray(newValue) && newValue.length === 0)) {
vm.$refs.baseSelectRef.updateSelectedData([])
vm.$refs.treeRef.setCheckedKeys([])
return
}
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)
} else {
const data = api.getPluginOption(newValue)[0]
if (!data) {
vm.$refs.baseSelectRef.updateSelectedData(null)
return
}
vm.$refs.baseSelectRef.updateSelectedData({
...data,
currentLabel: data[props.textField],
value: data[props.valueField],
state: {
currentLabel: data[props.textField]
}
})
}
}

21 changes: 18 additions & 3 deletions packages/renderless/src/tree-select/vue.ts
Original file line number Diff line number Diff line change
@@ -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']

Expand All @@ -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, {
Expand All @@ -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(
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/vue/src/tree-select/src/pc.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<tiny-base-select
ref="baseSelectRef"
class="tiny-tree-select"
v-model="state.value"
v-model="state.modelValue"
:clearable="clearable"
:filterable="filterable"
:filter-method="filter"
Expand All @@ -11,6 +11,7 @@
<template #panel>
<tiny-tree
ref="treeRef"
:check-strictly="treeOp.checkStrictly"
:current-node-key="!multiple ? state.currentKey : ''"
:data="state.treeData"
:default-checked-keys="multiple ? state.defaultCheckedKeys : treeOp.defaultCheckedKeys || []"
Expand Down
Loading