Grid : Enable sortable on individual columns#28633
Grid : Enable sortable on individual columns#28633kkakroo wants to merge 28 commits intomicrosoft:masterfrom
Conversation
Perf Analysis (
|
| Scenario | Render type | Master Ticks | PR Ticks | Iterations | Status |
|---|---|---|---|---|---|
| InfoButton | mount | 16 | 14 | 5000 | Possible regression |
All results
| Scenario | Render type | Master Ticks | PR Ticks | Iterations | Status |
|---|---|---|---|---|---|
| Avatar | mount | 608 | 626 | 5000 | |
| Button | mount | 304 | 304 | 5000 | |
| Field | mount | 1093 | 1105 | 5000 | |
| FluentProvider | mount | 672 | 678 | 5000 | |
| FluentProviderWithTheme | mount | 81 | 84 | 10 | |
| FluentProviderWithTheme | virtual-rerender | 61 | 67 | 10 | |
| FluentProviderWithTheme | virtual-rerender-with-unmount | 74 | 76 | 10 | |
| InfoButton | mount | 16 | 14 | 5000 | Possible regression |
| MakeStyles | mount | 832 | 856 | 50000 | |
| Persona | mount | 1723 | 1658 | 5000 | |
| SpinButton | mount | 1378 | 1379 | 5000 |
📊 Bundle size reportUnchanged fixtures
|
packages/react-components/react-table/src/components/TableHeaderCell/useTableHeaderCell.tsx
Outdated
Show resolved
Hide resolved
packages/react-components/react-table/stories/DataGrid/Default.stories.tsx
Outdated
Show resolved
Hide resolved
packages/react-components/react-table/src/components/DataGridBody/useDataGridBody.tsx
Show resolved
Hide resolved
|
To pass the error you need to run |
|
Hi @kkakroo, just reaching out to see if you would like to keep working on this PR? |
|
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 1e2f2a3:
|
Asset size changesSize Auditor did not detect a change in bundle size for any component! Baseline commit: 634979cbc69ae97e39baf2f4143dab6caa3a6968 (build) |
|
@kkakroo the change file and api files are missing, to fix this you have to:
Also, would you mind updating the description to describe the changes? |
|
Hi @sopranopillow, made the requested changes as well, thanks for letting me know. If you think i am not going in the right direction or delaying the feature delivery, feel free to let me know. |
No worries, looks close to mergeable. I'll let the team responsible for this give a final review. |
|
@kkakroo looks like the snapshots are outdated, you need to run |
|
@sopranopillow , thanks for pointing it out. Done. |
🕵 fluentuiv9 No visual regressions between this PR and main |
| ): TableHeaderCellState => { | ||
| const { noNativeElements, sortable } = useTableContext(); | ||
| const { noNativeElements, sortable: contextSortable } = useTableContext(); | ||
| const { sortable = contextSortable } = props; |
There was a problem hiding this comment.
Can you add a Table example for using this prop on headers?
| ): TableHeaderCellState => { | ||
| const { noNativeElements, sortable } = useTableContext(); | ||
| const { noNativeElements, sortable: contextSortable } = useTableContext(); | ||
| const { sortable = contextSortable } = props; |
There was a problem hiding this comment.
Please add a test for the prop/context override in TableHeaderCell.test.tsx
| let { sortable = false } = props; | ||
| const columnId = useColumnIdContext(); | ||
| const { sortable } = useTableContext(); | ||
| const { sortable: gridSortable } = useTableContext(); | ||
| const toggleColumnSort = useDataGridContext_unstable(ctx => ctx.sort.toggleColumnSort); | ||
| const columns = useDataGridContext_unstable(ctx => ctx.columns); | ||
| sortable = gridSortable || columns.find(c => c.columnId === columnId)?.compare.length !== 0; |
There was a problem hiding this comment.
This would avoid returning a non-primitive from the DataGridContext (which avoids a performance regression)
const columnSortable = useDataGridContext_unstable(ctx =>
ctx.getSortableColumns().find(column => column.columnId === columnId),
);
const { sortable: gridSortable } = useTableContext();
const toggleColumnSort = useDataGridContext_unstable(ctx => ctx.sort.toggleColumnSort);
const { sortable = gridSortable ?? columnSortable } = props;
const sortDirection = useDataGridContext_unstable(ctx =>
sortable ? ctx.sort.getSortDirection(columnId) : undefined,
);There was a problem hiding this comment.
Playing aroud a bit with your PR this might be the best way to go
const isColumnSortable = (columns: TableColumnDefinition<any>[], columnId: TableColumnId) => {
return !!columns.filter(c => c.compare.length !== 0 && c.columnId === columnId).length;
};
const useColumnSortable = (sortableProp: boolean | undefined) => {
const columnId = useColumnIdContext();
const columnSortable = useDataGridContext_unstable(ctx =>
isColumnSortable(ctx.columns, columnId)
);
const { sortable: gridSortable } = useTableContext();
return sortableProp ?? gridSortable ?? columnSortable;
};
// useDataGridHeaderCell_unstable
useColumnSortable(props.sortable)| ): TableFeaturesState<TItem> { | ||
| const { items, getRowId, columns } = options; | ||
|
|
||
| const getSortableColumns = () => columns.filter(c => c.compare.length !== 0); |
There was a problem hiding this comment.
I don't think this needs to be a part of the public API, a simple utility function would do
const isColumnSortable = (columns: TableColumnDefinition<any>[], columnId: TableColumnId) => {
return !!columns.filter(c => c.compare.length !== 0 && c.columnId === columnId).length;
};I think any in this case is acceptable since the utility stays internal. I can't quite think of a solid scenario why we would need a way to do this in the public API yet, so I'd rather avoid making it public for now
| ); | ||
| }; | ||
|
|
||
| ColumnSort.parameters = { |
There was a problem hiding this comment.
Instead of this example, can you just update the current Sort example and remove the sortable prop?
Then you can add the guidance here that column definitions without a compare function will not be sortable
|
@kkakroo Thanks for contributing! sorry about the lead time to this review, I've got this firmly in my sights now, there are few things to address but nothing too major |
|
Going to take over this work and publish it in #29196 |
|
Sorry for not being able to address the review comments on time, was on vacation. |

Previous Behavior
The
sortableproperty can be added to the Grid which makes each column of the Grid sortable.The sorting happens on the
comparefunction which is provided as part ofcolumnsprop.If
sortableis present andcomparefunction is not provided for a column, the sorting UI changes occur(arrows and hover behavior) but the sorting does not happen for that column.There is no way currently to enable sorting on specific columns.
New Behavior
If we add a
comparefunction to a particular column in thecolumnsprop, the column becomes sortable automatically.This helps enable sorting on specific columns without setting the
sortableprop.The previous
sortableprop behaviors are as is.Related Issue(s)