I am generating an array of objects and then Outputting these into a list, each objects has a checkbox that then adds it to the changeableElements array if it is checked
I am having trouble getting it to show only objects that are added to the array to appear as checked, and also disabled all of the other checkboxes once 3 are selected, how can I do this?
const [items, setItems] = useState(
[...Array(301)].map(
(_, i) => ({ name: `element ${i}`, selected: false, id: `element_uid_${i}}` }),
),
)
const [changeableElements, setchangeableElements] = useState([])
const updatechangeableElements = (item) => {
setchangeableElements(changeableElements.concat(item))
}
<ElementsListWrap>
{items.slice(0, itemsLimit !== null ? itemsLimit : Infinity)
.filter((item) => new RegExp(`^${searchValue.toLowerCase()}`).test(item.name))
.map((item) => (
<div
key={item.name}
>
{console.log('bkakask',changeableElements.filter((i) => item.id === i.id))}
<ElementWrap>
<Checkbox
key={item.name}
onChange={() => updatechangeableElements({ ...item, selected: true })}
type="checkbox"
checked={changeableElements.filter(i => item.id === i.selected === true).length > 0}
disabled={items.filter((i) => i.selected).length > 2 && item.selected !== true}
/>
{console.log(changeableElements.map(bla => item.id === bla.id))}
{item.name}
</ElementWrap>
</div>
),
)}
</ElementsListWrap>