Skip to content

Commit

Permalink
fix(tree): programmatic selection
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Sep 13, 2024
1 parent 8d4d226 commit cafa231
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-geckos-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zag-js/tree-view": patch
---

Fix issue where programmatic selection does not account for singular selection
13 changes: 11 additions & 2 deletions packages/machines/tree-view/src/tree-view.connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,17 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
send({ type: "SELECTED.ALL" })
return
}
const nextValue = new Set(selectedValue)
value.forEach((id) => nextValue.add(id))
const nextValue = new Set()
if (state.context.selectionMode === "single") {
// For single selection, only add the last item
if (value.length > 0) {
nextValue.add(value[value.length - 1])
}
} else {
// For multiple selection, add all items
value.forEach((id) => nextValue.add(id))
selectedValue.forEach((id) => nextValue.add(id))
}
send({ type: "SELECTED.SET", value: nextValue, src: "select" })
},
focusBranch(id) {
Expand Down

0 comments on commit cafa231

Please sign in to comment.