Skip to content

Commit

Permalink
fix: some code improvements in the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
moon-ds committed Oct 17, 2023
1 parent ac18dd1 commit 064c642
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 24 deletions.
5 changes: 2 additions & 3 deletions next-docs/public/examples/table/ExpandableCheckboxes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ const Example = () => {
</span>}
</div>
),
Cell: ({ row, rowsById }: any) => {
return (
Cell: ({ row, rowsById }: any) => (
<div className={mergeClassnames(
"flex items-center gap-x-1",
columnShift(row.depth),
Expand All @@ -92,7 +91,7 @@ const Example = () => {
</span>
) : null }
</div>
)}
)
},
],
},
Expand Down
26 changes: 5 additions & 21 deletions next-docs/public/examples/table/SelectableCheckboxes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,6 @@ const Example = () => {

const [selected, setSelected] = useState<{ [key: string]: boolean }>({});

const checkIfSelected = (id: string, canExpand: boolean, rowsById: { [key: string]: boolean }) => {
return canExpand
? Object.keys(rowsById)
.filter((rowId) => rowId.indexOf(id) === 0 && rowId !== id)
.every((rowId) => selected[rowId] === true)
: selected[id] === true;
}

const checkIfIndeterminate = (id: string, rowsById: { [key: string]: boolean }) => {
const matches = Object.keys(rowsById)
.filter((rowId) => rowId.indexOf(id) === 0 && rowId !== id);
return !matches.every((rowId) => selected[rowId] === true) && matches.some((rowId) => selected[rowId] === true);
}

const columnsInitial = [
{
'Header': 'Select',
Expand All @@ -40,24 +26,22 @@ const Example = () => {
}: HeaderProps) => (
<div className="flex items-center h-full">
<Checkbox
id={ PREFIX && PREFIX.length ? `${PREFIX}_root` : 'root'}
id={ PREFIX && PREFIX.length ? `${PREFIX}_root` : 'root' }
checked={(Object.keys(rowsById).length === Object.keys(selected).length)}
indeterminate={!!Object.keys(selected).length && Object.keys(selected).length < Object.keys(rowsById).length}
onClick={(e: any) => { e.stopPropagation() }}
/>
</div>
),
Cell: ({ row, rowsById }: any) => {
return (
Cell: ({ row, rowsById }: any) => (
<div className="flex items-center h-full">
<Checkbox
id={ PREFIX && PREFIX.length ? `${PREFIX}_${row.id}` : row.id}
checked={checkIfSelected(row.id, row.canExpand, rowsById)}
indeterminate={false}
id={ PREFIX && PREFIX.length ? `${PREFIX}_${row.id}` : row.id }
checked={selected[row.id] === true}
onClick={(e: any) => e.stopPropagation()}
/>
</div>
)},
),
Footer: '',
},
],
Expand Down

0 comments on commit 064c642

Please sign in to comment.