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

[fields] Fix field editing after closing the picker #12675

Merged
merged 3 commits into from
Apr 11, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ const useMultiInputFieldSlotProps = <
event.stopPropagation();
onRangePositionChange('start');
if (!readOnly && !disableOpenPicker) {
actions.onOpen();
actions.onOpen(event);
}
};

const openRangeEndSelection: React.UIEventHandler = (event) => {
event.stopPropagation();
onRangePositionChange('end');
if (!readOnly && !disableOpenPicker) {
actions.onOpen();
actions.onOpen(event);
}
};

Expand Down Expand Up @@ -400,7 +400,7 @@ const useSingleInputFieldSlotProps = <
event.stopPropagation();

if (!readOnly && !disableOpenPicker) {
actions.onOpen();
actions.onOpen(event);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,15 @@ export const usePickerValue = <
});
});

const handleOpen = useEventCallback(() => setIsOpen(true));
const handleOpen = useEventCallback((event: React.UIEvent) => {
event.preventDefault();
setIsOpen(true);
});

const handleClose = useEventCallback(() => setIsOpen(false));
const handleClose = useEventCallback((event?: React.UIEvent) => {
event?.preventDefault();
setIsOpen(false);
});

const handleChange = useEventCallback(
(newValue: TValue, selectionState: PickerSelectionState = 'partial') =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ export interface UsePickerValueActions {
onDismiss: () => void;
onCancel: () => void;
onSetToday: () => void;
onOpen: () => void;
onClose: () => void;
onOpen: (event: React.UIEvent) => void;
onClose: (event?: React.UIEvent) => void;
Copy link
Member

Choose a reason for hiding this comment

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

Do we have places where we pass the event to onClose?
This PR only passes them to onOpen

Copy link
Member Author

Choose a reason for hiding this comment

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

Explicitly nope, but implicitly it is called by clicking on the open picker button and this is basically the reason why our selection indexes get screwed up... 🙈
Without this, the fix doesn't work as the input selection logic gets called.

Copy link
Member

Choose a reason for hiding this comment

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

OK make sense 👍

}

export type UsePickerValueFieldResponse<TValue, TSection extends FieldSection, TError> = Required<
Expand All @@ -316,7 +316,7 @@ export interface UsePickerValueViewsResponse<TValue> {
value: TValue;
onChange: (value: TValue, selectionState?: PickerSelectionState) => void;
open: boolean;
onClose: () => void;
onClose: (event?: React.MouseEvent) => void;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DesktopDatePicker } from '@mui/x-date-pickers/DesktopDatePicker';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';

export default function BasicDesktopDatePickerV6() {
export default function BasicClearableDesktopDatePicker() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DesktopDatePicker
Expand Down
23 changes: 20 additions & 3 deletions test/e2e/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ function sleep(timeoutMS: number): Promise<void> {
// A simplified version of https://github.com/testing-library/dom-testing-library/blob/main/src/wait-for.js
function waitFor(callback: () => Promise<void>): Promise<void> {
return new Promise((resolve, reject) => {
let intervalId: NodeJS.Timer | null = null;
let timeoutId: NodeJS.Timer | null = null;
let intervalId: NodeJS.Timeout | null = null;
let timeoutId: NodeJS.Timeout | null = null;
let lastError: any = null;

function handleTimeout() {
Expand Down Expand Up @@ -534,6 +534,23 @@ async function initializeEnvironment(
);
});

// assertion for: https://github.com/mui/mui-x/issues/12652
it('should allow field editing after opening and closing the picker', async () => {
await renderFixture('DatePicker/BasicClearableDesktopDatePicker');
// open picker
await page.getByRole('button').click();
await page.waitForSelector('[role="dialog"]', { state: 'attached' });
// close picker
await page.getByRole('button', { name: 'Choose date' }).click();
await page.waitForSelector('[role="dialog"]', { state: 'detached' });

// click on the input to focus it
await page.getByRole('textbox').click();

// test that the input value is set after focus
expect(await page.getByRole('textbox').inputValue()).to.equal('MM/DD/YYYY');
});

it('should allow filling in a value and clearing a value', async () => {
await renderFixture('DatePicker/BasicDesktopDatePicker');

Expand Down Expand Up @@ -624,7 +641,7 @@ async function initializeEnvironment(
});

it('should focus the first field section after clearing a value in v6 input', async () => {
await renderFixture('DatePicker/BasicDesktopDatePickerV6');
await renderFixture('DatePicker/BasicClearableDesktopDatePicker');

await page.getByRole('textbox').fill('2');
await page.getByRole('button', { name: 'Clear value' }).click();
Expand Down