Skip to content

Parent nodes with empty array as children are checked by default #258

Closed
@elen1kl

Description

@elen1kl

Describe the bug
Now that the children property can be an empty array, when the tree renders, if a parent node has an empty array as children then it is rendered as a checked node by default and cannot be unchecked. The issue probably has to do with isEveryChildChecked function, as it will return true if the array provided is empty.

Reproduction steps
Having modified the below example to have a node that has no children (eg Neptune), in version 1.5.1, the node renders as an unchecked node with no option to expand it since there are no children to show. In version 1.6.0 however, the node renders as a checked node with the arrow on the left to expand it, even though there are no children to show. In addition, when you try to uncheck it that doesn't work.

Link to codesandbox example

import React from 'react';
import CheckboxTree from 'react-checkbox-tree';
import 'react-checkbox-tree/lib/react-checkbox-tree.css';

const nodes = [{
  value: 'mars',
  label: 'Mars',
  children: [
    { value: 'phobos', label: 'Phobos' },
    { value: 'deimos', label: 'Deimos' },
  ],
},
{
  value: 'neptune',
  label: 'Neptune',
  children: [],
}];

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

  onCheck = (checked) => {
    this.setState({ checked });
  };

  onExpand = (expanded) => {
    this.setState({ expanded });
  };

  render() {
    const { checked, expanded } = this.state;

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

export default Widget;

Expected behavior
The expected behaviour would be to render the parent component unchecked by default when there is an empty array provided as children.

Screenshots
Version 1.6.0 result - Parent node renders as checked by default if there are no children (current behaviour)
Screenshot 2021-03-05 at 09 41 20

Version 1.5.1 result - Parent node renders as unchecked by default with no option to expand it (expected behaviour)
Screenshot 2021-03-05 at 09 41 31

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions