I've got a list of items, each with their own checkboxes and I've decided to try add a 'select all' checkbox to make it easier for the user to select them all at once.
Unfortunately I'm finding it hard to work out the logic in a 'React' kinda way.
I found a JSBIN of how I would like the rendered result to work - https://jsbin.com/jetalaxaha/edit?html,js,output note: this is setup in a different way to how I would like it to.
My current code is here:
import React, { Component } from "react";
import ReactDOM from "react-dom";
class Items extends Component {
state = {
categories: [
{
id: 1,
name: "category 1",
items: [
{ name: "item 1", id: Math.floor(Math.random() * 99999) },
{ name: "item 2", id: Math.floor(Math.random() * 99999) }
]
},
{
id: 2,
name: "category 2",
items: [
{ name: "item 3", id: Math.floor(Math.random() * 99999) },
{ name: "item 4", id: Math.floor(Math.random() * 99999) }
]
},
{
id: 3,
name: "category 3",
items: [
{ name: "item 5", id: Math.floor(Math.random() * 99999) }
]
}
],
checkedListAll: [],
ItemsChecked: false
};
selectedItems(e) {
const { value, checked } = e.target;
let { checkedListAll } = this.state;
if (checked) {
checkedListAll = [...checkedListAll, value];
} else {
checkedListAll = checkedListAll.filter(el => el !== value);
if (this.state.ItemsChecked) {
this.setState({
ItemsChecked: !this.state.ItemsChecked
});
}
}
this.setState({ checkedListAll });
}
selectItem(e) {
const { checked } = e.target;
const { categories } = this.state;
const collection = [];
if (checked) {
this.setState(
{
checkedListAll: []
},
() => {
for (const cat of categories) {
for (const item of cat.items) {
collection.push(item.id);
}
}
this.setState({
checkedListAll: collection
});
}
);
} else {
this.setState({
checkedListAll: []
});
}
this.setState({
ItemsChecked: !this.state.ItemsChecked
});
}
render() {
const { categories, checkedListAll, ItemsChecked } = this.state;
return (
<div>
<header>
<label>
<input
type="checkbox"
checked={ItemsChecked}
onClick={this.selectItem.bind(this)}
/>Select all
</label>
</header>
{categories.map(cat => {
return (
<ItemCategory
{...cat}
key={cat.id}
click={this.openModal}
selectedItems={this.selectedItems.bind(this)}
ItemsChecked={ItemsChecked}
/>
);
})}
{
<pre>
All Selected: {JSON.stringify(ItemsChecked, null, 2)}
</pre>
}
{
<pre>
Selected List: {JSON.stringify(checkedListAll, null, 2)}
</pre>
}
</div>
);
}
}
class ItemCategory extends Component {
render() {
const { items, name, selectedItems, ItemsChecked } = this.props;
const getItems = items.map(item => {
return item;
});
return (
<div>
<div>-{name}</div>
<ul>
{getItems.map(item => {
return (
<li key={item.id}>
<Checkbox
item={item}
selectedItems={selectedItems}
ItemsChecked={ItemsChecked}
/>
</li>
);
})}
</ul>
</div>
);
}
}
class Checkbox extends Component {
state = {
isChecked: false
};
componentDidUpdate(prevProps) {
if (prevProps.ItemsChecked !== this.props.ItemsChecked) {
this.setState({
isChecked: !this.state.isChecked
});
}
}
handleClick(e) {
e.persist();
if (this.props.ItemsChecked) {
console.log(true);
}
this.setState(
{
isChecked: !this.state.isChecked
},
() => {
this.props.selectedItems(e);
}
);
}
render() {
const { item } = this.props;
const { isChecked } = this.state;
console.log(this.props.ItemsChecked);
return (
<label>
<input
type="checkbox"
value={item.id}
checked={isChecked}
onClick={this.handleClick.bind(this)}
/>
{item.name}
</label>
);
}
}
function App() {
return <Items />;
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Also available as a codesandbox - https://codesandbox.io/s/r44yn2rwm4
Current outstanding issues/extra functionality to be added:
- Clicking on the 'select all' checkbox, then unticking one of the item checkboxes deselects the rest.
- Selecting all items individually should tick the 'select all' checkbox
- Fix 'Selected List' ID collection
Any help with this would be greatly appreciated, been working on this for a while and exhausted all avenues!
ItemsChecked: !this.state.ItemsChecked- go fortrue/false. your current implementation is 'toggle' - not check.