-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(PHC-4380): row selection to TableModule #333
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,14 @@ import { TableHeaderCell } from './TableHeaderCell'; | |
import { TableModuleRow } from './TableModuleRow'; | ||
import { warning } from '../../utils'; | ||
import { | ||
RowSelectionState, | ||
RowSelectionRow, | ||
TableSortState, | ||
TableHeader, | ||
TableCell, | ||
TableConfiguration, | ||
TableSortClickProps, | ||
TableState, | ||
} from './types'; | ||
import * as React from 'react'; | ||
import clsx from 'clsx'; | ||
|
@@ -227,6 +230,10 @@ export interface TableModuleProps<Item = any> | |
maxCellWidth?: 1 | 2; | ||
rowActions?: (row: any) => React.ReactNode; | ||
rowClickLabel?: string; | ||
state?: Partial<TableState>; | ||
enableRowSelection?: boolean; | ||
getRowId?: (row: Item, index: number) => string; | ||
onRowSelectionChange?: (state: RowSelectionState) => void; | ||
} | ||
|
||
/** | ||
|
@@ -263,6 +270,10 @@ export const TableModule = React.memo( | |
maxCellWidth, | ||
rowActions, | ||
rowClickLabel, | ||
state, | ||
enableRowSelection = false, | ||
getRowId = (_, index) => index.toString(), | ||
onRowSelectionChange, | ||
...rootProps | ||
}, | ||
forwardedRef | ||
|
@@ -278,6 +289,10 @@ export const TableModule = React.memo( | |
|
||
const [sort, setSort] = React.useState<TableSortState>(sortState); | ||
|
||
const [rowSelection, setRowSelection] = React.useState<RowSelectionState>( | ||
state?.rowSelection || {} | ||
); | ||
|
||
const [headings, setHeadings] = React.useState<Array<TableHeader>>( | ||
config?.map((c) => c.header) || [] | ||
); | ||
|
@@ -412,6 +427,51 @@ export const TableModule = React.memo( | |
} | ||
}; | ||
|
||
const isRowSelected = (row: any, index: number): boolean => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, double checking that this any (and the following one) are definitely what we want |
||
return rowSelection[getRowId(row, index)] ?? false; | ||
}; | ||
|
||
const createRow = React.useCallback( | ||
(row: any, index: number): RowSelectionRow => ({ | ||
getIsSelected: () => { | ||
return isRowSelected(row, index); | ||
}, | ||
getCanSelect: () => enableRowSelection, | ||
toggleSelected: (value?: boolean) => { | ||
const isSelected = isRowSelected(row, index); | ||
value = typeof value !== 'undefined' ? value : !isSelected; | ||
|
||
if (isSelected === value) { | ||
return; | ||
} | ||
|
||
const newRowSelection = { ...rowSelection }; | ||
|
||
if (isSelected) { | ||
delete newRowSelection[getRowId(row, index)]; | ||
} else { | ||
newRowSelection[getRowId(row, index)] = true; | ||
} | ||
|
||
setRowSelection(newRowSelection); | ||
|
||
if (onRowSelectionChange) { | ||
onRowSelectionChange(newRowSelection); | ||
} | ||
}, | ||
getToggleSelectedHandler: () => { | ||
return (e: unknown) => { | ||
if (enableRowSelection) { | ||
row.toggleSelected( | ||
((e as MouseEvent).target as HTMLInputElement).checked | ||
); | ||
} | ||
}; | ||
}, | ||
}), | ||
[setRowSelection, isRowSelected, onRowSelectionChange] | ||
); | ||
|
||
return ( | ||
<table | ||
role="table" | ||
|
@@ -483,14 +543,15 @@ export const TableModule = React.memo( | |
</tr> | ||
)} | ||
{data?.map((row, rowIndex) => { | ||
const rowData = createRow(row, rowIndex); | ||
return ( | ||
<TableModuleRow | ||
key={`tableRow-${rowIndex}`} | ||
data={row} | ||
onRowClick={onRowClick} | ||
rowRole={rowRole} | ||
maxCellWidth={maxCellWidth} | ||
row={row} | ||
row={Object.assign(row, rowData)} | ||
headingsLength={headings?.length} | ||
cells={cells} | ||
rowActions={rowActions} | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you think it's not too much for this PR, could you add a class to the checkbox here that makes its click area larger for improved UX? Here it is for your reference:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to add something to the
TableModule.stories.tsx
but there's notheme
so just need to figure out how to use this snippetThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see some other components that leverage a theme in
/src/styles.
IMO, every table should have the same spacing here as a default and maybe it should be extensible, but I'm guessing most cases will want the basic spacing (or we may want to just enforce this)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a temp solution before deprecating the table in favor of one that leverages tanStack's we can go without this for now