Skip to content

Controlled mode is broken #190

@urrri-redis

Description

@urrri-redis

Describe the bug
when I want to control selection, I add external state for selectedIds and pass them to the tree.
this is working, when I apply ids for the first time, like in uncontrolled mode.
When I'm trying to apply ids, based on onSelect event, then it behaves incorrect and weird

Code That Causes The Issue

function App(){    
  const [sel, setSel] = useState<NodeId[]>([]);
  return (
    <TreeView
          data={nodes} // any hierarchical nodes (at least 1 level with branches and leaves)
          multiSelect
          propagateSelect
          propagateSelectUpwards
          togglableSelect
          selectedIds={sel}
          onSelect={({ element, treeState, ...state }) => {
            // following only to show the problem
            if (!props.isBranch) setSel([element.id]);
          }}
   />
  )
}

To Reproduce
Steps to reproduce the behavior:

  1. Add the code to any tree example
  2. Click on any leaf node
  3. Then click on any other leaf node
  4. ! See endless switching between that nodes
  5. to make problem even more visual, wrap setSel call with setTimeout
  6. select some leafs/branches one by one
  7. ! see flickering among all the selections

Note: I also tried to use following setSel([...treeState.selectedIds]), but it also makes flickering, when I select last leaf in branch, when other leaves already selected

Expected behavior
if selectedIds prop is not undefined (controlled mode), then component must show passed selection, instead of internal state, even if user doesn't changes selection. Only after change events (onSelect, onNodeSelect) are treated and selectedIds updated, tree should represent new state, based only on values in the prop

Screen capture
https://github.com/user-attachments/assets/5f4d0651-7844-493a-bfa7-4933793a041e

Desktop (please complete the following information):

  • OS: [Windows 10]
  • Browser [chrome]
  • Version [126]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions