Skip to content

Instantly share code, notes, and snippets.

@tmcnab
Forked from jimdwyerdev/transverseChunk.js
Created January 19, 2021 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmcnab/c4d86e7ce92d8e24dc76f203c1725e1b to your computer and use it in GitHub Desktop.
Save tmcnab/c4d86e7ce92d8e24dc76f203c1725e1b 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 = [], $columnsInRow = [], rowLength = columns
while (i < $columns.length) {
$columnsInRow = []
while (n < rowLength) {
$columnsInRow.push($columns[i])
n += 1
}
$rows.push(<Row>{$columnsInRow}</Row>)
i += rowLength
n = 0
}
return $rows
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment