-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[data grid] Remove Unsort from the column menu if sortingOrder is ['asc', 'desc'] #7103
Comments
Hey @Valentin1918 By the way, we are working on the new version of the column menu #6619 |
For the current column menu implementation, the fix would look like this: diff --git a/packages/grid/x-data-grid/src/components/menu/columnMenu/SortGridMenuItems.tsx b/packages/grid/x-data-grid/src/components/menu/columnMenu/SortGridMenuItems.tsx
index ac345d2..d4380dc 100644
--- a/packages/grid/x-data-grid/src/components/menu/columnMenu/SortGridMenuItems.tsx
+++ b/packages/grid/x-data-grid/src/components/menu/columnMenu/SortGridMenuItems.tsx
@@ -6,11 +6,13 @@ import { gridSortModelSelector } from '../../../hooks/features/sorting/gridSorti
import { GridSortDirection } from '../../../models/gridSortModel';
import { useGridApiContext } from '../../../hooks/utils/useGridApiContext';
import { GridFilterItemProps } from './GridFilterItemProps';
+import { useGridRootProps } from '../../../hooks/utils/useGridRootProps';
function SortGridMenuItems(props: GridFilterItemProps) {
const { column, onClick } = props;
const apiRef = useGridApiContext();
const sortModel = useGridSelector(apiRef, gridSortModelSelector);
+ const rootProps = useGridRootProps();
const sortDirection = React.useMemo(() => {
if (!column) {
@@ -33,17 +35,42 @@ function SortGridMenuItems(props: GridFilterItemProps) {
return null;
}
+ const sortingOrder: GridSortDirection[] = column.sortingOrder ?? rootProps.sortingOrder;
+
return (
<React.Fragment>
- <MenuItem onClick={onSortMenuItemClick} disabled={sortDirection == null}>
- {apiRef.current.getLocaleText('columnMenuUnsort')}
- </MenuItem>
- <MenuItem onClick={onSortMenuItemClick} data-value="asc" disabled={sortDirection === 'asc'}>
- {apiRef.current.getLocaleText('columnMenuSortAsc')}
- </MenuItem>
- <MenuItem onClick={onSortMenuItemClick} data-value="desc" disabled={sortDirection === 'desc'}>
- {apiRef.current.getLocaleText('columnMenuSortDesc')}
- </MenuItem>
+ {sortingOrder.map((direction) => {
+ if (direction === 'asc') {
+ return (
+ <MenuItem
+ onClick={onSortMenuItemClick}
+ data-value="asc"
+ disabled={sortDirection === 'asc'}
+ >
+ {apiRef.current.getLocaleText('columnMenuSortAsc')}
+ </MenuItem>
+ );
+ }
+ if (direction === 'desc') {
+ return (
+ <MenuItem
+ onClick={onSortMenuItemClick}
+ data-value="desc"
+ disabled={sortDirection === 'desc'}
+ >
+ {apiRef.current.getLocaleText('columnMenuSortDesc')}
+ </MenuItem>
+ );
+ }
+ if (direction === null) {
+ return (
+ <MenuItem onClick={onSortMenuItemClick} disabled={sortDirection == null}>
+ {apiRef.current.getLocaleText('columnMenuUnsort')}
+ </MenuItem>
+ );
+ }
+ return null;
+ })}
</React.Fragment>
);
}
|
Hi! Seems like @cherniavskii already suggested fix for this issue, but could I create a pr for this issue with a bit of code refactoring? |
@hanbin9775 Sure, feel free to submit a PR! |
Hi @MBilalShafi, saw your commit about this issue's fix and have a question about it. |
Hello @hanbin9775 , according to this PR, looks like the order will be according sortingOrder. Order of IF statements inside a map is not important. So it looks good ) |
Sorry, I was speaking about this code snippet #7103 (comment) -- it looks good. Concerning PR -- it doesn't take into account an order of sortingOrder |
Yes, we need to have a specific order of the column menu items irrespective of the order in This should be the order of sort items in the simple column menu:
You can use something similar to this to achieve that. |
Duplicates
Latest version
Steps to reproduce 🕹
Link to live example:
Steps:
1.
2.
3.
Current behavior 😯
Column menu is not reacted on sortingOrder property passed to grid component
Expected behavior 🤔
Column menu sorting items (Unsort, Sort by ASC, Sort by DESC) should be filtered by sortingOrder passed in a data grid component as well as in each column separately
Context 🔦
No response
Your environment 🌎
npx @mui/envinfo
Order ID 💳 (optional)
No response
The text was updated successfully, but these errors were encountered: