-
Notifications
You must be signed in to change notification settings - Fork 15
Control sorting through props for iTwin and iModel tables #223
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
base: main
Are you sure you want to change the base?
Changes from 8 commits
8cc3097
137e2f6
5eb72db
553f764
e279c84
9342a06
7896e66
13484ef
69f0806
72a569d
367c765
bb84e3c
8742b39
98a338f
d4f134e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@itwin/imodel-browser-react", | ||
| "comment": "Controlled table sorting for iTwin/iModel tables", | ||
| "type": "minor" | ||
| } | ||
| ], | ||
| "packageName": "@itwin/imodel-browser-react" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
| * See LICENSE.md in the project root for license terms and full copyright notice. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
| module.exports = { | ||
| managerEntries: [require.resolve("./manager.jsx")], | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,9 +5,11 @@ | |
| import { | ||
| type IModelFull, | ||
| type IModelGridProps as IModelGridMUIProps, | ||
| type IModelTableSortModel, | ||
| DataStatus, | ||
| IModelCellColumn, | ||
| IModelGrid as ExternalComponent, | ||
| IModelSortOptions, | ||
| } from "@itwin/imodel-browser-react/mui"; | ||
| import Avatar from "@mui/material/Avatar"; | ||
| import AvatarGroup from "@mui/material/AvatarGroup"; | ||
|
|
@@ -153,6 +155,88 @@ export const TableViewWithOverrides: StoryObj<typeof IModelGridMUI> = { | |
| }, | ||
| }; | ||
|
|
||
| const TableWithControlledSortRender = (args: IModelGridMUIProps) => { | ||
| const [sortModel, setSortModel] = React.useState<IModelSortOptions>({ | ||
| sortType: "name", | ||
| descending: true, | ||
| }); | ||
|
|
||
| return ( | ||
| <div> | ||
| <div | ||
| style={{ | ||
| display: "flex", | ||
| gap: 8, | ||
| marginBottom: 8, | ||
| alignItems: "center", | ||
| }} | ||
| > | ||
| <Typography variant="body2">Sort by:</Typography> | ||
| <Chip | ||
| label="Name" | ||
| clickable | ||
| variant={sortModel.sortType === "name" ? "filled" : "outlined"} | ||
| onClick={() => | ||
| setSortModel((prev) => ({ ...prev, sortType: "name" })) | ||
| } | ||
| /> | ||
| <Chip | ||
| label="Last Modified" | ||
| clickable | ||
| variant={ | ||
| sortModel.sortType === "lastChangesetPushDateTime" | ||
| ? "filled" | ||
| : "outlined" | ||
| } | ||
| onClick={() => | ||
| setSortModel((prev) => ({ | ||
| ...prev, | ||
| sortType: "lastChangesetPushDateTime", | ||
| })) | ||
| } | ||
| /> | ||
| <Chip | ||
| label={sortModel.descending ? "↓ Descending" : "↑ Ascending"} | ||
| clickable | ||
| variant="outlined" | ||
| onClick={() => | ||
| setSortModel((prev) => ({ | ||
| ...prev, | ||
| descending: !prev.descending, | ||
| })) | ||
| } | ||
| /> | ||
| </div> | ||
| <ExternalComponent | ||
| {...args} | ||
| sortOptions={sortModel} | ||
| onSortModelChange={(newSortModel: IModelTableSortModel) => { | ||
| if (newSortModel.length > 0) { | ||
|
Contributor
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. nit: this should have an
Contributor
Author
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. added the action as suggested, the action fires when you click on the table column headers. The idea is to be able to sync the table sorting with external sorting used in the app. |
||
| const newSort = newSortModel[0]; | ||
| setSortModel({ | ||
|
Contributor
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. Is there any reason to call
Contributor
Author
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. as described in my other comment, the click handlers (chips at the top of the table) are not part of the iModelGridMUI, theyre just there in the story to simulate sorting done outside of the iModelGrid (similar to the sorting dropdown available in studio). So an app using iModelGrid should sync its sorting logic when the sort is changed using the table header sorting. |
||
| sortType: newSort.field, | ||
| descending: newSort.sort === "desc", | ||
| }); | ||
| } | ||
| }} | ||
| /> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const TableWithControlledSort: StoryObj<typeof IModelGridMUI> = { | ||
|
arome marked this conversation as resolved.
|
||
| render: (args) => <TableWithControlledSortRender {...args} />, | ||
| args: { ...baseArgs }, | ||
| parameters: { | ||
| docs: { | ||
| description: { | ||
| story: | ||
| "The sort state is fully controlled by the parent via `sortModel` and `onSortModelChange`, so it can be saved anywhere the consumer wants.", | ||
| }, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| const OverrideApiDataWithLoadMoreRender = (args: IModelGridMUIProps) => { | ||
| const [data, setData] = React.useState<IModelFull[]>(initialData); | ||
| const [isLoading, setIsLoading] = React.useState(false); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.