-
-
Save jimdwyerdev/edb1ab9b588f289a05ef075ef2762d72 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default function CheckboxGrid ({ columns, items, onChange }) { | |
// TODO assert that columns > 0 | |
const didChange = ({ checked, value }) => { | |
const nextItems = [...items] | |
const index = findIndex(nextItems, (item) => item.value === value) | |
nextItems[index] = { ...nextItems[index], checked } | |
onChange(nextItems) | |
} | |
const $columns = items.map(({ checked, label, value }) => | |
<Col key={`checkbox-col-${value}`} md={3}> | |
<Checkbox | |
checked={checked} | |
label={label} | |
onChange={({checked}) => didChange({ checked, value })} | |
value={value} | |
/> | |
</Col>) | |
let n = 0, i=0, $rows = [] | |
for(;i < $columns.length; i+= 1) { | |
$rows.push() | |
} | |
return chunk(items, columns).map((row, index) => | |
<Row key={`checkbox-row-${index}`}> | |
{row.map(({ checked, label, value }) => | |
<Col key={`checkbox-col-${value}`} md={3}> | |
<Checkbox | |
checked={checked} | |
label={label} | |
onChange={({checked}) => didChange({ checked, value })} | |
value={value} | |
/> | |
</Col>)} | |
</Row>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment