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

[DataGrid] Refactor useGridColumnReorder #1299

Merged
merged 4 commits into from
Mar 31, 2021

Conversation

m4theushw
Copy link
Member

@m4theushw m4theushw commented Mar 25, 2021

Breaking changes

  • [DataGrid] Renamed all events related to column reordering, e.g. GRID_COL_REORDER_START -> GRID_COLUMN_REORDER_START
  • [DataGrid] Methods onColItemDragStart, onColHeaderDragOver, onColItemDragOver, onColItemDragEnter removed from the grid API. Prefer listening to column reordering events
  • [DataGrid] Call to apiRef.current.getColumnHeaderParams returns a GridColumnHeaderParams instead of GridColParams
  • [DataGrid] Events GRID_COLUMN_HEADER_ will be called with a GridColumnHeaderParams instead of GridColParams
  • [DataGrid] The renderHeader method will be called with a GridColumnHeaderParams instead of GridColParams
  • [DataGrid] The apiRef.current.moveColumn API was renamed to apiRef.current.setColumnIndex

Closes #1201.

}),
[apiRef, column],
);

const publish = React.useCallback(
(eventName: string) => (event: React.MouseEvent) =>
apiRef!.current.publishEvent(
Copy link
Member

Choose a reason for hiding this comment

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

You need to publish the event with params. For cell, we have GridCellParams, could you add GridColumnHeaderParams please

@oliviertassinari
Copy link
Member

@m4theushw If you could update the issue's description to include all the breaking changes so we can include them in the next release changelog, it would be perfect.

@dtassone
Copy link
Member

FYI more about events in #1301

@m4theushw m4theushw requested a review from dtassone March 26, 2021 17:39
- `COL_REORDER_DRAG_OVER`: emitted when dragging a header cell over another header cell.
- `COL_REORDER_DRAG_OVER_HEADER`: emitted when dragging a header cell over the `ColumnsHeader` component.
- `COL_REORDER_STOP`: emitted when dragging of a header cell stops.
- `GRID_COLUMN_REORDER_START`: emitted when dragging of a header cell starts.
Copy link
Member

Choose a reason for hiding this comment

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

We should discuss how to present events in the doc.
This is the constant name not its value. Would users use the constant or its value?
I think we should show both, in a separate event page that we would link maybe at the bottom of this block

Copy link
Member

@oliviertassinari oliviertassinari Mar 29, 2021

Choose a reason for hiding this comment

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

+1 for:

  • making all the event const private
  • only using the string values
  • improve the types to only accept valid event strings

This way, we get the benefit of:

  • Only one way to do things (we have to choose anyway what we encourage in the demos)
  • No pain of importing variables to subscribe to events (e.g. document.addEventerListener('resize'))
  • Retain type safety

Copy link
Member Author

@m4theushw m4theushw Mar 29, 2021

Choose a reason for hiding this comment

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

DataTables and AG Grid only present the events as strings in their documentations.

Firebase present the const and the value.

Reasons in favor of exporting event consts:

  • Avoid hardcoded strings
  • Refactoring is easier

I would go with not exporting consts for now, since they are changing. In the future, they could be exported for those users who prefer to use a const instead of strings.

Copy link
Member

@oliviertassinari oliviertassinari Mar 31, 2021

Choose a reason for hiding this comment

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

I guess I don't get how doing

import { GRID_COLUMN_REORDER_START } from '@material-ui/x-grid';

apiRef.current.on(GRID_COLUMN_REORDER_START, () => {});

is better than:

apiRef.current.on('gridColumnReoederStart', () => {});

It seems to yield the same features, without the import pain:

  • if you did a typo, type safety get you covered
  • if we release a breaking change, type safety get you covered
  • if you want to refactor, the string is long enough to run search and replace

I personally think that the const is only interesting for JavaScript users, that don't get type errors.


But I think that what's most important is the documentation. What do we use? For the demos, we can only use one variant. For the markdown, we could potentially describe the two but it would create confusion among developers (which one should I use ?!). So I think that we should first answer the following:

Assuming we only document one variant. Which one do we use in the documentation?

How about we only use the const at the cost of making it harder to use, but at the pros of making it better for JavaScript users? Developers that prefer string, can log the content of the constant, and we can explain to them how the camelCase convention work.

Copy link
Member Author

Choose a reason for hiding this comment

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

Assuming we only document one variant. Which one do we use in the documentation?

+1 for eventName in the demos and markdown pages. The only exception to show both would be in the API page. There could have a "Events" section with all strings and their equivalent constants, potentially showing the arguments like Kendo UI does. BTW they use save and SaveEvent in the demos because of the way Angular binds events.

I think something we're missing here is a stable naming convention for the events. Having columnReordering:dragStart and columnHeaderClick may create some confusion.

Copy link
Member

@oliviertassinari oliviertassinari Mar 31, 2021

Choose a reason for hiding this comment

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

@m4theushw eventName over EVENT_NAME?

Should we do a vote? :)

  • 🎉 eventName
  • 🚀 EVENT_NAME

@m4theushw m4theushw force-pushed the useGridColumnReorder-refactoring branch from ecea0b8 to 5da9029 Compare March 29, 2021 14:25

logger.debug(`Moving column ${field} to index ${targetIndexPosition}`);

apiRef.current.publishEvent(GRID_COLUMN_ORDER_CHANGE, {
Copy link
Member

Choose a reason for hiding this comment

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

small one. Could we type the params before publishing. So if we change the params then it breaks

Copy link
Member

Choose a reason for hiding this comment

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

Also where is options.onColumnOrderChange hooked to the event?

Copy link
Member

Choose a reason for hiding this comment

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

useGridApiOptionHandler(apiRef, GRID_COLUMN_ORDER_CHANGE, options.onColumnOrderChange) ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Also where is options.onColumnOrderChange hooked to the event?

Do we need to call it with the event? Users can get it by subscribing to GRID_COLUMN_REORDER_DRAG_END.

import { GridCellParams } from '../models/params/gridCellParams';
import { GridApiContext } from './GridApiContext';

export const GridHeaderCheckbox: React.FC<GridColParams> = () => {
export const GridHeaderCheckbox: React.FC<GridColumnHeaderParams> = () => {
Copy link
Member

Choose a reason for hiding this comment

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

A note for the future self (outside of the scope of this PR) and Matheus, React.FC is wrong, the component has no children prop

Copy link
Member Author

Choose a reason for hiding this comment

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

True, another PR to update all components that don't use children would be good.

Copy link
Member

@oliviertassinari oliviertassinari Mar 31, 2021

Choose a reason for hiding this comment

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

@m4theushw Do you want to open a new issue for keeping track of it? I'm not saying it's a priority but, that it would be great to handle it, at some point. Some of these components are public, hence hurt the developers using them.

Copy link
Member Author

Choose a reason for hiding this comment

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

@m4theushw m4theushw force-pushed the useGridColumnReorder-refactoring branch from d0a2fd9 to edbd9ea Compare March 31, 2021 14:50
@dtassone
Copy link
Member

Nice one 👍

@m4theushw m4theushw merged commit ca286ad into mui:master Mar 31, 2021
@m4theushw m4theushw deleted the useGridColumnReorder-refactoring branch March 31, 2021 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[DataGrid] Refactor/Remove ColumnReorderApi
4 participants