-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDS-738 Table & Checkbox Integration (#2434)
* feat: a new table with expanded selectable rows and checkboxes has been created * fix: table control structure * fix: row and branch selection and highlighting * fix: row and branch selection and highlighting * fix: burst selection/deselection * fix: burst select/deselect * feat: the Expandable table with Checkboxes * fix: rows are being selected via checkbox only * fix: some code improvements in the examples * fmt * fix: code improvement by the remarks * fix: code improvement after the remarks * fix: another code correction * fix: type of the variable has been removed * fix: a tiny fix at the Table component * fmt
- Loading branch information
1 parent
1d465d9
commit 6968938
Showing
13 changed files
with
17,178 additions
and
8,222 deletions.
There are no files selected for viewing
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
317 changes: 317 additions & 0 deletions
317
next-docs/public/examples/table/ExpandableCheckboxes.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,317 @@ | ||
import { Checkbox, Chip, Tooltip, mergeClassnames } from "@heathmont/moon-core-tw"; | ||
import { OtherFrame } from "@heathmont/moon-icons-tw"; | ||
import { Table } from "@heathmont/moon-table-tw"; | ||
import React, { useState } from "react"; | ||
|
||
interface HeaderProps { | ||
rows: []; | ||
rowsById: { [key: string]: boolean }; | ||
isAllRowsExpanded: boolean; | ||
getToggleAllRowsExpandedProps: () => React.HTMLAttributes<HTMLSpanElement>; | ||
} | ||
|
||
const Example = () => { | ||
/** | ||
* The PREFIX is necessary if you use several different tables with checkboxes on the same page. | ||
* Each table should have its own unique PREFIX to avoid assigning identical indexes to elements. | ||
* When using only one table, the PREFIX can be omitted. | ||
*/ | ||
const PREFIX = "any__unique__string__for__each__table"; | ||
|
||
const [selected, setSelected] = useState<{ [key: string]: boolean }>({}); | ||
|
||
const columnShift = (depth: number) => { | ||
const shiftMap: { [key: number]: string } = [ | ||
'ps-0', | ||
'ps-6', | ||
'ps-12', | ||
]; | ||
|
||
return shiftMap[depth]; | ||
} | ||
|
||
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': 'Expand/Select', | ||
'sticky': 'left', | ||
columns: [ | ||
{ | ||
'id': 'any_unique_string_required', | ||
Header: ({ | ||
rows, | ||
rowsById, | ||
getToggleAllRowsExpandedProps, | ||
isAllRowsExpanded, | ||
}: HeaderProps) => ( | ||
<div className="flex items-center gap-x-1"> | ||
<div className="flex items-center h-full"> | ||
<Checkbox | ||
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) => { e.stopPropagation() }} | ||
/> | ||
</div> | ||
{<span {...getToggleAllRowsExpandedProps()}> | ||
{isAllRowsExpanded ? '👇' : '👉'} | ||
</span>} | ||
</div> | ||
), | ||
Cell: ({ row, rowsById }: any) => ( | ||
<div className={mergeClassnames( | ||
"flex items-center gap-x-1", | ||
columnShift(row.depth), | ||
)} | ||
onClick={(e) => { | ||
if ((e.target as unknown as HTMLElement).tagName === 'SPAN') e.stopPropagation(); | ||
}} | ||
> | ||
<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={checkIfIndeterminate(row.id, rowsById)} | ||
onClick={(e) => e.stopPropagation()} | ||
/> | ||
</div> | ||
{row.canExpand ? ( | ||
<span {...row.getToggleRowExpandedProps()}> | ||
{row.isExpanded ? '👇' : '👉'} | ||
</span> | ||
) : null} | ||
</div> | ||
) | ||
}, | ||
], | ||
}, | ||
{ | ||
Header: 'Name', | ||
sticky: 'left', | ||
columns: [ | ||
{ | ||
Header: 'First Name', | ||
accessor: 'firstName', | ||
}, | ||
], | ||
}, | ||
{ | ||
Header: 'Info', | ||
columns: [ | ||
{ | ||
Header: 'Age', | ||
accessor: 'age', | ||
width: 50, | ||
}, | ||
{ | ||
Header: 'Visits', | ||
accessor: 'visits', | ||
}, | ||
{ | ||
Header: 'Activity', | ||
accessor: 'activity', | ||
}, | ||
{ | ||
Header: 'Status', | ||
width: 60, | ||
accessor: 'status', | ||
}, | ||
{ | ||
Header: 'Profile Progress', | ||
Footer: 'Profile Progress', | ||
accessor: 'progress', | ||
}, | ||
], | ||
}, | ||
{ | ||
Header: 'Actions', | ||
sticky: 'right', | ||
columns: [ | ||
{ | ||
Header: 'Actions', | ||
accessor: 'actions', | ||
Footer: '', | ||
}, | ||
], | ||
}, | ||
]; | ||
|
||
const tooltip = () => ( | ||
<Tooltip> | ||
<Tooltip.Trigger className="max-h-6"> | ||
<Chip | ||
variant="ghost" | ||
iconOnly={<OtherFrame className="text-moon-24 max-h-6" />} | ||
/> | ||
</Tooltip.Trigger> | ||
<Tooltip.Content position="top-start"> | ||
Round details | ||
<Tooltip.Arrow /> | ||
</Tooltip.Content> | ||
</Tooltip> | ||
); | ||
|
||
const makeData = () => [ | ||
{ | ||
firstName: 'Test lvl1', | ||
age: <span>36</span>, | ||
visits: <span>50</span>, | ||
progress: <span>20</span>, | ||
status: 19, | ||
activity: 54, | ||
actions: tooltip(), | ||
subRows: [ | ||
{ | ||
firstName: 'Sub test lvl2', | ||
age: <span>96</span>, | ||
visits: <span>8</span>, | ||
progress: <span>2</span>, | ||
status: 97, | ||
activity: 23, | ||
actions: tooltip(), | ||
subRows: [ | ||
{ | ||
firstName: 'Sub test lvl3', | ||
age: <span>63</span>, | ||
visits: <span>82</span>, | ||
progress: <span>59</span>, | ||
status: 52, | ||
activity: 46, | ||
actions: tooltip(), | ||
}, | ||
{ | ||
firstName: 'Sub test lvl3', | ||
age: <span>64</span>, | ||
visits: <span>35</span>, | ||
progress: <span>78</span>, | ||
status: 65, | ||
activity: 5, | ||
actions: tooltip(), | ||
}, | ||
{ | ||
firstName: 'Sub test lvl3', | ||
age: <span>12</span>, | ||
visits: <span>4</span>, | ||
progress: <span>44</span>, | ||
status: 98, | ||
activity: 5, | ||
actions: tooltip(), | ||
}, | ||
], | ||
}, | ||
{ | ||
firstName: 'Sub test lvl2', | ||
age: <span>74</span>, | ||
visits: <span>5</span>, | ||
progress: <span>1</span>, | ||
status: 86, | ||
activity: 2, | ||
actions: tooltip(), | ||
subRows: [ | ||
{ | ||
firstName: 'Sub test lvl3', | ||
age: <span>89</span>, | ||
visits: <span>98</span>, | ||
progress: <span>54</span>, | ||
status: 24, | ||
activity: 43, | ||
actions: tooltip(), | ||
}, | ||
{ | ||
firstName: 'Sub test lvl3', | ||
age: <span>52</span>, | ||
visits: <span>25</span>, | ||
progress: <span>25</span>, | ||
status: 97, | ||
activity: 35, | ||
actions: tooltip(), | ||
}, | ||
{ | ||
firstName: 'Sub test lvl3', | ||
age: <span>55</span>, | ||
visits: <span>54</span>, | ||
progress: <span>24</span>, | ||
status: 56, | ||
activity: 33, | ||
actions: tooltip(), | ||
}, | ||
], | ||
}, | ||
{ | ||
firstName: 'Sub test lvl2', | ||
age: <span>53</span>, | ||
visits: <span>63</span>, | ||
progress: <span>24</span>, | ||
status: 48, | ||
activity: 3, | ||
actions: tooltip(), | ||
subRows: [ | ||
{ | ||
firstName: 'Sub test lvl3', | ||
age: <span>4</span>, | ||
visits: <span>653</span>, | ||
progress: <span>36</span>, | ||
status: 44, | ||
activity: 43, | ||
actions: tooltip(), | ||
}, | ||
{ | ||
firstName: 'Sub test lvl3', | ||
age: <span>49</span>, | ||
visits: <span>45</span>, | ||
progress: <span>454</span>, | ||
status: 35, | ||
activity: 4, | ||
actions: tooltip(), | ||
}, | ||
], | ||
}, | ||
], | ||
} | ||
]; | ||
|
||
const columns = React.useMemo(() => columnsInitial, [selected]); | ||
const data = React.useMemo(() => makeData(), []); | ||
|
||
const defaultColumn = React.useMemo( | ||
() => ({ | ||
minWidth: 100, | ||
width: 150, | ||
maxWidth: 400, | ||
}), []); | ||
|
||
return ( | ||
<Table | ||
columns={columns} | ||
defaultColumn={defaultColumn} | ||
data={data} | ||
width={800} | ||
height={400} | ||
selectable={true} | ||
useCheckbox={true} | ||
expandedByDefault={true} | ||
getOnRowSelect={() => (rows) => { | ||
setSelected(rows.reduce((acc: { [key: string]: boolean }, item) => { | ||
acc[item.id] = true; | ||
return acc; | ||
}, {}) | ||
); | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
export default Example; |
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
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
Oops, something went wrong.