Skip to content

Have Only one node (chekbox) selected at a time #44

Open
@c623700

Description

@c623700

I try to force the list of nodes to only have one "isChecked" node by calling a function that would update the data from the onCheckToggleCb but to no avail.
here is the code:
<SuperTreeview
....
onCheckToggleCb={(arrayOfNodes, depth)=>{
this.handleCheck(arrayOfNodes);
....
/>

handleCheck(arrayOfNodes) {
    const isChecked = arrayOfNodes[0].isChecked;
    const nodeId = arrayOfNodes[0].id;
    var data = this.state.data;

    // if the node is being unchecked, leave data alone (it will update itself)
    if (isChecked) {
        // if the node is being checked, "wipe out" all the "checked" node in this.state.data (should be only one)
        applyCheckStateTo(data, nodeId);

        // Set the updated data in this.state.data
        this.setState({data: data});
    }

    function applyCheckStateTo(data, nodeId){
        data.forEach((node)=>{
            if (node.id != nodeId)
                node.isChecked = false;
            else
                node.isChecked = true;
            if(node.children){
                applyCheckStateTo(node.children, nodeId);
            }
        })
    }
}

Previous response suggested that is any properties need to be modified, it can be done on the onUpdateCb function, but how do I know (from onUpdateCb) which node to leave check and which node to uncheck as "arrayOfNodes is Not pass to onUpdateCb.

Appreciate any help

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