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

[XGrid] Add ability to disable reorder on some columns #2085

Merged
merged 15 commits into from
Jul 21, 2021

Conversation

flaviendelangle
Copy link
Member

@flaviendelangle flaviendelangle commented Jul 6, 2021

Closes #1161
Closes #1768

Implemented behavior :

  • A column with disableReorder: true can't be drag&dropped
  • If the first visible column of the grid has disableReorder: true, we can't drag&drop a column left of it
  • If the last visible column of the grid has disableReorder: true, we can't drag&drop a column right of it

Right now the behavior is at the drag&drop level, I do not block the updates made with apiRef.
Do we want to do it?

Do you prefer?

  /**
   * If `true`, this column can not be reordered
   * @default false
   */
  disableReorder?: boolean;

OR

  /**
   * If `true`, this column can be reordered
   * @default true
   */
  orderable?: boolean;

We have both (disableExport but resizable), which is not great from a user perspective I think.

I did not implement the behavior on the column panel yet. Preview: https://deploy-preview-2085--material-ui-x.netlify.app/components/data-grid/columns/#column-reorder.

@flaviendelangle flaviendelangle changed the title Disable reorder [XGrid] Add ability to disable reorder on some columns Jul 6, 2021
@flaviendelangle flaviendelangle marked this pull request as draft July 6, 2021 14:38
@flaviendelangle flaviendelangle self-assigned this Jul 6, 2021
@oliviertassinari
Copy link
Member

For the name of the property, I would refer to https://material-ui.com/guides/api/#property-naming

@flaviendelangle
Copy link
Member Author

For the name of the property, I would refer to https://material-ui.com/guides/api/#property-naming

@oliviertassinari

This page is for props, I don't think the shorthand argument really applies here.
But I am 100% in favor of consistency.

Both approaches can make sense

  • Always favor default: false (meaning that we should have disableResize instead of resizable)
    or
  • Always favor the affirmative form (meaning that we should have exportable instead of disableExport)

Most options are in the affirmative form so I think we should rename disableExport into exportable (my PR has not yet been merged) and on this PR orderable ?

@flaviendelangle
Copy link
Member Author

@mui-org/x

I have two behavior points to discuss

AG Grid differentiate

  1. the columns that can't be moved and that are automatically placed at the beginning of the grid (lockPosition: true)
  2. the column that can't be moved but for which we can move columns around it (suppressMovable: true)
The Age column is locked as the first column in the scrollable area of the grid. It is not possible to move this column, or have other columns moved over it to impact its position. As a result the Age column marks the beginning of the scrollable area regardless of its position.
The Athlete column has moving suppressed. It is not possible to move this column, but it is possible to move other columns around it.

Do you think it's something we want to implement ?
I did a single solution with a bit more of magic since I'm checking the position of the target column to decide if the user is allowed to move the drag column on it.


Do we want to also block the re-ordering when it is done with apiRef.current.setColumnIndex ?
Right now I am only blocking the drag&drop.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jul 7, 2021

Both approaches can make sense

@flaviendelangle Yeah, right, the shorthand advantage is specific to the props. I will update this section to mention the second advantage: as a developer, you always know what the default value is by looking at the name of the prop.

Do you think it's something we want to implement ?

I think that the difference is not negligeable. If we wanted to be really low-level and maximize flexibility, we could say to the developers:

  1. suppressMovable: true: add event handlers on the column headers and use [RFC] Use event.defaultMuiPrevented to prevent default events #1403.
  2. lockPosition: true it could be implemented with a controlled API and the prevent event.

I guess that for 2. to work, it also needs 1.

Maybe we can implement 2, and in the future, expose 1 in the API if we see a real use case? So far the problem we want to solve is to force the selection checkbox to stay on the starting position.

@flaviendelangle flaviendelangle marked this pull request as ready for review July 7, 2021 13:47
@flaviendelangle
Copy link
Member Author

flaviendelangle commented Jul 9, 2021

#2085 (comment) @oliviertassinari

What do you mean by "implemented with a controlled API" ?
For instance if we want to avoid dragging a column left of the checkbox column (or drag the checkbox column right of anything).

docs/src/pages/components/data-grid/columns/columns.md Outdated Show resolved Hide resolved
packages/grid/_modules_/grid/models/colDef/gridColDef.ts Outdated Show resolved Hide resolved
const targetCol = getColumnHeaderCell(0).firstChild!;

fireEvent.dragStart(dragCol);
fireEvent.dragEnter(targetCol);
Copy link
Member

Choose a reason for hiding this comment

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

If the column can't be reordered we don't even allow the user to start dragging it. We could check if the draggable attribute is false or if GRID_COLUMN_HEADER_DRAGGING_CSS_CLASS was not added.

packages/grid/x-grid/src/tests/reorder.XGrid.test.tsx Outdated Show resolved Hide resolved
!targetCol.disableReorder ||
(targetColVisibleIndex > 0 && targetColVisibleIndex < visibleColumnAmount - 1);

const cursorMoveDirectionX = getCursorMoveDirectionX(cursorPosition.current, coordinates);
Copy link
Member

Choose a reason for hiding this comment

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

For another PR:

This cursorMoveDirectionX is wrong. It's always "right" the first time. We need to save the cursor position when the user starts dragging

const dragOverEvent = createEvent.dragOver(target);
// Safari 13 doesn't have DragEvent.
// RTL fallbacks to Event which doesn't allow to set these fields during initialization.
Object.defineProperty(dragOverEvent, 'clientX', { value: 1 });
Copy link
Member

Choose a reason for hiding this comment

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

For another PR:

Add an argument to allow to change the clientX. We use this value to check if the user is moving to the left or right.

Copy link
Member

@oliviertassinari oliviertassinari left a comment

Choose a reason for hiding this comment

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

I have tested the behavior with a disableReorder on a column in the middle: https://codesandbox.io/s/material-demo-forked-2c26p?file=/demo.js. It's weird. But 👍 for ignoring this for now. I think that it would be more efficient & effective to ship and wait for feedback.

I mean, the new prop allows preventing end-users to start the drag and drop from the column, it's already great. It comes with a bonus behavior (that is likely outside of the scope of disableReorder) of preventing other columns to go past it.

Copy link
Member

@m4theushw m4theushw left a comment

Choose a reason for hiding this comment

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

Good job!

packages/grid/x-grid/src/tests/reorder.XGrid.test.tsx Outdated Show resolved Hide resolved
@flaviendelangle flaviendelangle merged commit 7e93d3f into mui:master Jul 21, 2021
@flaviendelangle flaviendelangle deleted the disable-reorder branch July 21, 2021 07:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[XGrid] Disable checkbox column draggable [XGrid] Disable reordering on a specific column
4 participants