Skip to content

Commit 553d22b

Browse files
committed
Merge remote-tracking branch 'upstream/master' into map-zoom
2 parents a0304e8 + 6276bc4 commit 553d22b

6 files changed

Lines changed: 197 additions & 3 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import * as React from 'react';
2+
import {
3+
DataGridPro,
4+
Toolbar,
5+
ToolbarButton,
6+
FilterPanelTrigger,
7+
gridFilterActiveItemsSelector,
8+
useGridApiContext,
9+
useGridSelector,
10+
} from '@mui/x-data-grid-pro';
11+
import { useDemoData } from '@mui/x-data-grid-generator';
12+
import Tooltip from '@mui/material/Tooltip';
13+
import FilterListIcon from '@mui/icons-material/FilterList';
14+
15+
function useActiveFiltersDescription() {
16+
const apiRef = useGridApiContext();
17+
const activeFilters = useGridSelector(apiRef, gridFilterActiveItemsSelector);
18+
19+
if (activeFilters.length === 0) {
20+
return 'Filters';
21+
}
22+
23+
return activeFilters
24+
.map((item) => {
25+
const column = apiRef.current.getColumn(item.field);
26+
const operator = column.filterOperators?.find(
27+
(filterOperator) => filterOperator.value === item.operator,
28+
);
29+
return `${column.headerName ?? column.field} ${operator?.label ?? item.operator} ${item.value ?? ''}`.trim();
30+
})
31+
.join(', ');
32+
}
33+
34+
function CustomToolbar() {
35+
const description = useActiveFiltersDescription();
36+
37+
return (
38+
<Toolbar>
39+
<Tooltip title={description}>
40+
<FilterPanelTrigger render={<ToolbarButton />}>
41+
<FilterListIcon fontSize="small" />
42+
</FilterPanelTrigger>
43+
</Tooltip>
44+
</Toolbar>
45+
);
46+
}
47+
48+
export default function FilterPanelTriggerDescription() {
49+
const { data, loading } = useDemoData({
50+
dataSet: 'Commodity',
51+
rowLength: 10,
52+
maxColumns: 10,
53+
});
54+
55+
return (
56+
<div style={{ height: 400, width: '100%' }}>
57+
<DataGridPro
58+
{...data}
59+
loading={loading}
60+
slots={{ toolbar: CustomToolbar }}
61+
showToolbar
62+
initialState={{
63+
...data.initialState,
64+
filter: {
65+
filterModel: {
66+
items: [{ field: 'quantity', operator: '>', value: '50000' }],
67+
},
68+
},
69+
}}
70+
/>
71+
</div>
72+
);
73+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import * as React from 'react';
2+
import {
3+
DataGridPro,
4+
Toolbar,
5+
ToolbarButton,
6+
FilterPanelTrigger,
7+
gridFilterActiveItemsSelector,
8+
useGridApiContext,
9+
useGridSelector,
10+
} from '@mui/x-data-grid-pro';
11+
import { useDemoData } from '@mui/x-data-grid-generator';
12+
import Tooltip from '@mui/material/Tooltip';
13+
import FilterListIcon from '@mui/icons-material/FilterList';
14+
15+
function useActiveFiltersDescription() {
16+
const apiRef = useGridApiContext();
17+
const activeFilters = useGridSelector(apiRef, gridFilterActiveItemsSelector);
18+
19+
if (activeFilters.length === 0) {
20+
return 'Filters';
21+
}
22+
23+
return activeFilters
24+
.map((item) => {
25+
const column = apiRef.current.getColumn(item.field);
26+
const operator = column.filterOperators?.find(
27+
(filterOperator) => filterOperator.value === item.operator,
28+
);
29+
return `${column.headerName ?? column.field} ${operator?.label ?? item.operator} ${item.value ?? ''}`.trim();
30+
})
31+
.join(', ');
32+
}
33+
34+
function CustomToolbar() {
35+
const description = useActiveFiltersDescription();
36+
37+
return (
38+
<Toolbar>
39+
<Tooltip title={description}>
40+
<FilterPanelTrigger render={<ToolbarButton />}>
41+
<FilterListIcon fontSize="small" />
42+
</FilterPanelTrigger>
43+
</Tooltip>
44+
</Toolbar>
45+
);
46+
}
47+
48+
export default function FilterPanelTriggerDescription() {
49+
const { data, loading } = useDemoData({
50+
dataSet: 'Commodity',
51+
rowLength: 10,
52+
maxColumns: 10,
53+
});
54+
55+
return (
56+
<div style={{ height: 400, width: '100%' }}>
57+
<DataGridPro
58+
{...data}
59+
loading={loading}
60+
slots={{ toolbar: CustomToolbar }}
61+
showToolbar
62+
initialState={{
63+
...data.initialState,
64+
filter: {
65+
filterModel: {
66+
items: [{ field: 'quantity', operator: '>', value: '50000' }],
67+
},
68+
},
69+
}}
70+
/>
71+
</div>
72+
);
73+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<DataGridPro
2+
{...data}
3+
loading={loading}
4+
slots={{ toolbar: CustomToolbar }}
5+
showToolbar
6+
initialState={{
7+
...data.initialState,
8+
filter: {
9+
filterModel: {
10+
items: [{ field: 'quantity', operator: '>', value: '50000' }],
11+
},
12+
},
13+
}}
14+
/>

docs/data/data-grid/components/filter-panel/filter-panel.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ import { FilterPanelTrigger } from '@mui/x-data-grid';
4545
`<FilterPanelTrigger />` is a button that opens and closes the filter panel.
4646
It renders the `baseButton` slot.
4747

48+
## Describing the active filters
49+
50+
`FilterPanelTrigger` only reports `filterCount` to its `render` callback. It doesn't generate a description of the active filters.
51+
To show one (for example, in a tooltip), build it yourself from `gridFilterActiveItemsSelector`, which lists the currently active `GridFilterItem` objects:
52+
53+
{{"demo": "FilterPanelTriggerDescription.js", "bg": "inline", "defaultCodeOpen": false}}
54+
4855
## Custom elements
4956

5057
Use the `render` prop to replace default elements.

packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.test.tsx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,31 @@ describe('<DateRangeCalendar />', () => {
645645
});
646646
});
647647

648+
it('should not switch the visible months when the requested month is already visible across a year boundary', async () => {
649+
const { setProps } = render(
650+
<DateRangeCalendar
651+
value={[adapterToUse.date('2023-12-01'), adapterToUse.date('2023-12-01')]}
652+
/>,
653+
);
654+
655+
// The two calendars initially show December 2023 and January 2024.
656+
expect(screen.getByRole('grid', { name: 'December 2023' })).not.to.equal(null);
657+
expect(screen.getByRole('grid', { name: 'January 2024' })).not.to.equal(null);
658+
659+
// Move the start to a day already visible in the second calendar (January 2024).
660+
setProps({
661+
value: [adapterToUse.date('2024-01-15'), adapterToUse.date('2024-01-15')],
662+
});
663+
664+
// The new start is now selected, confirming the auto month-switch effect ran.
665+
await screen.findByRole('gridcell', { name: '15', selected: true });
666+
667+
// The visible months must not jump: December 2023 and January 2024 stay, February 2024 does not appear.
668+
expect(screen.getByRole('grid', { name: 'December 2023' })).not.to.equal(null);
669+
expect(screen.getByRole('grid', { name: 'January 2024' })).not.to.equal(null);
670+
expect(screen.queryByRole('grid', { name: 'February 2024' })).to.equal(null);
671+
});
672+
648673
describe('prop: currentMonthCalendarPosition', () => {
649674
it('should switch to the selected month when changing value from the outside', async () => {
650675
const { setProps } = render(

packages/x-date-pickers-pro/src/DateRangeCalendar/DateRangeCalendar.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,13 @@ const DateRangeCalendar = React.forwardRef(function DateRangeCalendar(
417417
}
418418

419419
const displayingMonthRange = calendars - currentMonthCalendarPosition;
420-
const currentMonthNumber = adapter.getMonth(calendarState.currentMonth);
421-
const requestedMonthNumber = adapter.getMonth(date);
420+
// Use absolute months (year * 12 + month) so the comparison stays correct across year boundaries.
421+
const currentMonthNumber =
422+
adapter.getYear(calendarState.currentMonth) * 12 +
423+
adapter.getMonth(calendarState.currentMonth);
424+
const requestedMonthNumber = adapter.getYear(date) * 12 + adapter.getMonth(date);
422425

423426
if (
424-
!adapter.isSameYear(calendarState.currentMonth, date) ||
425427
requestedMonthNumber < currentMonthNumber ||
426428
requestedMonthNumber > currentMonthNumber + displayingMonthRange
427429
) {

0 commit comments

Comments
 (0)