jakezatecky / react-checkbox-tree

A simple and elegant checkbox tree for React.

Home Page:https://jakezatecky.github.io/react-checkbox-tree/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Won't cascade down during serialization

ZhangHanwen96 opened this issue · comments

Describe the bug
The root checkbox won't be fully checked if it is in [checked] list .

Reproduction steps
If i were to put 'mars' in checked list, instead of get fully checked state , it is not checked at all.
const nodes = [
{
value: "mars",
label: "Mars",
children: [
{ value: "phobos", label: "Phobos" },
{ value: "deimos", label: "Deimos" },
{
value: "mars-1",
label: "Mars",
children: [
{ value: "phobos-1", label: "Phobos" },
{ value: "deimos-1", label: "Deimos" }
]
}
]
}
];

class Widget extends React.Component {
state = {
checked: ["mars"],
expanded: []
};

render() {
return (
<>
<CheckboxTree
nodes={nodes}
checked={this.state.checked}
expanded={this.state.expanded}
onCheck={(checked) => this.setState({ checked })}
onExpand={(expanded) => this.setState({ expanded })}
/>

{JSON.stringify(this.state.checked)}

</>
);
}
}



**Expected behavior**
.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**My solution**
```typescript
 // Deserialize values and set their nodes to true
        listKeys.forEach((listKey) => {
            lists[listKey].forEach((value) => {
                if (this.flatNodes[value] !== undefined) {
                    this.flatNodes[value][listKey] = true;
                }
                if(listKey === 'checked') {
                    const {noCascade, checkModel} = this.props;
// ----- add cascade down process here -----
                    this.flatNodes[value].children.forEach((child) => {
                        this.toggleChecked(child, true, checkModel, noCascade, false);
                    });
                }
            });
        });

deplicated