Skip to content

Instantly share code, notes, and snippets.

@jimdwyerdev
Forked from tmcnab/transverseChunk.js
Last active January 19, 2021 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jimdwyerdev/edb1ab9b588f289a05ef075ef2762d72 to your computer and use it in GitHub Desktop.
Save jimdwyerdev/edb1ab9b588f289a05ef075ef2762d72 to your computer and use it in GitHub Desktop.
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