Skip to content

Commit

Permalink
[fields] Fix field editing after closing the picker (#12675)
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasTy committed Apr 11, 2024
1 parent 9f6f609 commit aed8aeb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
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;
}

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 @@ -24,8 +24,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 @@ -558,6 +558,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 @@ -648,7 +665,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

0 comments on commit aed8aeb

Please sign in to comment.