Skip to content

Comments

Grid : Enable sortable on individual columns#28633

Closed
kkakroo wants to merge 28 commits intomicrosoft:masterfrom
kkakroo:grid-sortable
Closed

Grid : Enable sortable on individual columns#28633
kkakroo wants to merge 28 commits intomicrosoft:masterfrom
kkakroo:grid-sortable

Conversation

@kkakroo
Copy link
Contributor

@kkakroo kkakroo commented Jul 24, 2023

Previous Behavior

The sortable property can be added to the Grid which makes each column of the Grid sortable.

The sorting happens on the compare function which is provided as part of columns prop.
If sortable is present and compare function 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 compare function to a particular column in the columns prop, the column becomes sortable automatically.
This helps enable sorting on specific columns without setting the sortable prop.

The previous sortable prop behaviors are as is.

Related Issue(s)

@fabricteam
Copy link
Collaborator

fabricteam commented Jul 24, 2023

Perf Analysis (@fluentui/react-components)

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

@fabricteam
Copy link
Collaborator

fabricteam commented Jul 24, 2023

📊 Bundle size report

Package & Exports Baseline (minified/GZIP) PR Change
react-table
DataGrid
156.416 kB
43.567 kB
156.78 kB
43.721 kB
364 B
154 B
react-table
Table (Primitives only)
42.463 kB
13.255 kB
42.48 kB
13.279 kB
17 B
24 B
react-table
Table as DataGrid
129.276 kB
34.739 kB
129.352 kB
34.801 kB
76 B
62 B
react-table
Table (Selection only)
74.56 kB
20.053 kB
74.636 kB
20.108 kB
76 B
55 B
react-table
Table (Sort only)
73.191 kB
19.649 kB
73.267 kB
19.704 kB
76 B
55 B
Unchanged fixtures
Package & Exports Size (minified/GZIP)
react-components
react-components: Button, FluentProvider & webLightTheme
69.328 kB
19.608 kB
react-components
react-components: Accordion, Button, FluentProvider, Image, Menu, Popover
208.141 kB
59.349 kB
react-components
react-components: FluentProvider & webLightTheme
40.713 kB
13.509 kB
react-portal-compat
PortalCompatProvider
6.541 kB
2.227 kB
🤖 This report was generated against 634979cbc69ae97e39baf2f4143dab6caa3a6968

@sopranopillow
Copy link
Contributor

To pass the error you need to run yarn buildto @fluentui/react-table to generate the api files.

@sopranopillow
Copy link
Contributor

Hi @kkakroo, just reaching out to see if you would like to keep working on this PR?

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 22, 2023

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:

Sandbox Source
@fluentui/react 8 starter Configuration
@fluentui/react-components 9 starter Configuration

@size-auditor
Copy link

size-auditor bot commented Aug 22, 2023

Asset size changes

Size Auditor did not detect a change in bundle size for any component!

Baseline commit: 634979cbc69ae97e39baf2f4143dab6caa3a6968 (build)

@sopranopillow
Copy link
Contributor

sopranopillow commented Aug 23, 2023

@kkakroo the change file and api files are missing, to fix this you have to:

  1. run yarn buildto @fluentui/react-components
  2. commit your changes
  3. run yarn change
  4. commit your change files
  5. push

Also, would you mind updating the description to describe the changes?

@kkakroo
Copy link
Contributor Author

kkakroo commented Aug 23, 2023

Hi @sopranopillow, made the requested changes as well, thanks for letting me know.
Please have a look at my comment if you have some time. I had a query to complete the missing piece of this feature.

If you think i am not going in the right direction or delaying the feature delivery, feel free to let me know.
I can close this PR and let you take over.

@sopranopillow sopranopillow changed the title Grid: enabling sortable on individual columns fix(react-table): Enable sortable on individual columns Aug 23, 2023
@sopranopillow sopranopillow marked this pull request as ready for review August 23, 2023 22:03
@sopranopillow sopranopillow requested a review from a team as a code owner August 23, 2023 22:03
@sopranopillow
Copy link
Contributor

Hi @sopranopillow, made the requested changes as well, thanks for letting me know. Please have a look at my comment if you have some time. I had a query to complete the missing piece of this feature.

If you think i am not going in the right direction or delaying the feature delivery, feel free to let me know. I can close this PR and let you take over.

No worries, looks close to mergeable. I'll let the team responsible for this give a final review.

@kkakroo kkakroo changed the title fix(react-table): Enable sortable on individual columns Grid : Enable sortable on individual columns Aug 24, 2023
@sopranopillow
Copy link
Contributor

@kkakroo looks like the snapshots are outdated, you need to run yarn workspace @fluentui/react-table test -u to update it and push those changes.

@kkakroo
Copy link
Contributor Author

kkakroo commented Aug 25, 2023

@sopranopillow , thanks for pointing it out. Done.
Saw some tests failing as well, let me have a look into those meanwhile.

@fabricteam
Copy link
Collaborator

fabricteam commented Aug 25, 2023

🕵 fluentuiv9 No visual regressions between this PR and main

): TableHeaderCellState => {
const { noNativeElements, sortable } = useTableContext();
const { noNativeElements, sortable: contextSortable } = useTableContext();
const { sortable = contextSortable } = props;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for the prop/context override in TableHeaderCell.test.tsx

Comment on lines +22 to +27
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;
Copy link
Contributor

@ling1726 ling1726 Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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,
  );

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this example, can you just update the current Sort example and remove the sortable prop?

<DataGrid items={items} columns={columns} sortable defaultSortState={defaultSortState}>

Then you can add the guidance here that column definitions without a compare function will not be sortable

@ling1726
Copy link
Contributor

ling1726 commented Sep 5, 2023

@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

@ling1726
Copy link
Contributor

Going to take over this work and publish it in #29196

@kkakroo
Copy link
Contributor Author

kkakroo commented Oct 3, 2023

Sorry for not being able to address the review comments on time, was on vacation.
Thanks for taking this up.

@kkakroo kkakroo closed this Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: DataGrid v9 - ability to set sortable property on individual columns

4 participants