Skip to content
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

fix(Table): SelectRow デフォルト値設定可能にする #1494

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lemon-peaches-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@4design/for-ui": patch
---

fix(Table): SelectRow デフォルト値設定可能にする
2 changes: 2 additions & 0 deletions packages/for-ui/src/table/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const WithSelect: Story = () => (
columns={columns}
data={StaticPersonData}
getRowId={(row) => row.id.toString()}
selectedRow="2"
onSelectRow={(row) => console.info('Selected row: ', row)}
/>
);
Expand All @@ -58,6 +59,7 @@ export const WithSelectMultiple: Story = () => (
columns={columns}
data={StaticPersonData}
getRowId={(row) => row.id.toString()}
selectedRows={['2', '3', '4']}
onSelectRows={(rows) => console.info('Selected rows: ', rows)}
/>
);
Expand Down
12 changes: 11 additions & 1 deletion packages/for-ui/src/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ export type TableProps<T extends RowData> = Pick<TableOptions<T>, 'data' | 'colu
} & (
| {
/** If wanting to use selectable table, specify _onSelectRow_ or _onSelectRows_ exclusively */
selectedRow?: string;
selectedRows?: never;
onSelectRow?: ((id: string | undefined) => void) | undefined;
onSelectRows?: never;
}
| {
selectedRow?: never;
selectedRows?: string[];
onSelectRow?: never;
/** If wanting to use selectable table, specify _onSelectRow_ or _onSelectRows_ exclusively */
onSelectRows?: ((ids: string[]) => void) | undefined;
Expand All @@ -63,6 +67,8 @@ export const Table = <T extends RowData>({
data,
disablePagination,
defaultSortColumn,
selectedRow,
selectedRows,
onSelectRow,
onSelectRows,
onRowClick,
Expand All @@ -76,8 +82,11 @@ export const Table = <T extends RowData>({
defaultPage = 1,
onChangePage,
}: TableProps<T>) => {
const defaultSelectedRows = selectedRow
? { [selectedRow]: true }
: selectedRows?.reduce((acc, id) => ({ ...acc, [id]: true }), {}) ?? {};
const [sorting, setSorting] = useState<SortingState>(defaultSortColumn ? [defaultSortColumn] : []);
const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
const [rowSelection, setRowSelection] = useState<RowSelectionState>(defaultSelectedRows);
const prevRowSelection = useRef<RowSelectionState>({});
const tableId = useId();

Expand All @@ -91,6 +100,7 @@ export const Table = <T extends RowData>({
if (prevRowSelection.current === row) {
return;
}
console.info(row);
setRowSelection(row);
prevRowSelection.current = row;
const selectedIds = Object.keys(row);
Expand Down