-
-
Save tmcnab/c4d86e7ce92d8e24dc76f203c1725e1b 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 = [], $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