-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[DataGrid] Refactor useGridColumnReorder #1299
Conversation
}), | ||
[apiRef, column], | ||
); | ||
|
||
const publish = React.useCallback( | ||
(eventName: string) => (event: React.MouseEvent) => | ||
apiRef!.current.publishEvent( |
There was a problem hiding this comment.
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
packages/grid/_modules_/grid/components/columnHeaders/GridColumnHeaders.tsx
Outdated
Show resolved
Hide resolved
@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. |
FYI more about events in #1301 |
- `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. |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
packages/grid/_modules_/grid/hooks/features/columns/useGridColumns.ts
Outdated
Show resolved
Hide resolved
ecea0b8
to
5da9029
Compare
|
||
logger.debug(`Moving column ${field} to index ${targetIndexPosition}`); | ||
|
||
apiRef.current.publishEvent(GRID_COLUMN_ORDER_CHANGE, { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
?
There was a problem hiding this comment.
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> = () => { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
packages/grid/_modules_/grid/models/params/gridColumnOrderChangeParams.ts
Outdated
Show resolved
Hide resolved
packages/grid/_modules_/grid/hooks/root/useGridApiOptionHandler.ts
Outdated
Show resolved
Hide resolved
d0a2fd9
to
edbd9ea
Compare
Nice one 👍 |
Breaking changes
GRID_COL_REORDER_START
->GRID_COLUMN_REORDER_START
onColItemDragStart
,onColHeaderDragOver
,onColItemDragOver
,onColItemDragEnter
removed from the grid API. Prefer listening to column reordering eventsapiRef.current.getColumnHeaderParams
returns aGridColumnHeaderParams
instead ofGridColParams
GRID_COLUMN_HEADER_
will be called with aGridColumnHeaderParams
instead ofGridColParams
renderHeader
method will be called with aGridColumnHeaderParams
instead ofGridColParams
apiRef.current.moveColumn
API was renamed toapiRef.current.setColumnIndex
Closes #1201.