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

[DateRangePicker] Avoid unnecessary field section focusing (@LukasTy) #16569

Merged
merged 2 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ const useMultiInputFieldSlotProps = <
}

// bring back focus to the field
// currentView is present on DateTimeRangePicker
currentFieldRef.current.setSelectedSections(
// use the current view or `0` when the range position has just been swapped
previousRangePosition.current === rangePosition ? currentView : 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ export const useRangePosition = (

const sections = singleInputFieldRef.current.getSections();
const targetActiveSectionIndex = newRangePosition === 'start' ? 0 : sections.length / 2;
const activeSectionIndex = singleInputFieldRef.current.getActiveSectionIndex();
// if the active section is already within the target range, we don't need to update it.
if (
activeSectionIndex &&
activeSectionIndex >= targetActiveSectionIndex &&
activeSectionIndex < targetActiveSectionIndex + sections.length / 2
) {
return;
}
singleInputFieldRef.current.setSelectedSections(targetActiveSectionIndex);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
return nextSectionIndex === -1 ? sections.length - 1 : nextSectionIndex - 1;
},
focusField: (newSelectedSection = 0) => {
if (getActiveElement(document) === inputRef.current) {
return;
}
inputRef.current?.focus();
setSelectedSections(newSelectedSection);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ export const useFieldV7TextField: UseFieldTextField<true> = (params) => {
return sectionListRef.current.getSectionIndexFromDOMElement(activeElement);
},
focusField: (newSelectedSections = 0) => {
if (!sectionListRef.current) {
if (
!sectionListRef.current ||
// if the field is already focused, we don't need to focus it again
interactions.getActiveSectionIndexFromDOM() != null
) {
return;
}

Expand Down
16 changes: 16 additions & 0 deletions test/e2e/fixtures/DatePicker/DesktopDateRangePickerWithValue.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as React from 'react';
import dayjs from 'dayjs';
import { DateRangePicker } from '@mui/x-date-pickers-pro/DateRangePicker';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';

export default function BasicDesktopDateRangePicker() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DateRangePicker
defaultValue={[dayjs('2024-04-12'), dayjs('2024-04-14')]}
enableAccessibleFieldDOMStructure
/>
</LocalizationProvider>
);
}
32 changes: 32 additions & 0 deletions test/e2e/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,38 @@ async function initializeEnvironment(
'MUI X: The timezone of the start and the end date should be the same.',
);
});

it('should keep the focus on the clicked section', async () => {
// firefox in CI is not happy with this test
if (browserType.name() === 'firefox') {
return;
}
await renderFixture('DatePicker/DesktopDateRangePickerWithValue');

const startDaySection = page.getByRole('spinbutton', { name: 'Day' }).first();
await startDaySection.click();
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('12');

const endYearSection = page.getByRole('spinbutton', { name: 'Year' }).last();
await endYearSection.click();
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('2024');
});

it('should keep the focus on the clicked section with single input field', async () => {
// firefox in CI is not happy with this test
if (browserType.name() === 'firefox') {
return;
}
await renderFixture('DatePicker/SingleDesktopDateRangePickerWithTZ');

const startDaySection = page.getByRole('spinbutton', { name: 'Day' }).first();
await startDaySection.click();
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('12');

const endYearSection = page.getByRole('spinbutton', { name: 'Year' }).last();
await endYearSection.click();
expect(await page.evaluate(() => document.activeElement?.textContent)).to.equal('2024');
});
});
});
});
Expand Down