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] Unify fields behavior regardless of readOnly flag #13688

Merged
merged 4 commits into from
Jul 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.
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 @@ -182,8 +182,7 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
getActiveSectionIndexFromDOM: () => {
const browserStartIndex = inputRef.current!.selectionStart ?? 0;
const browserEndIndex = inputRef.current!.selectionEnd ?? 0;
const isInputReadOnly = !!inputRef.current?.readOnly;
if ((browserStartIndex === 0 && browserEndIndex === 0) || isInputReadOnly) {
if (browserStartIndex === 0 && browserEndIndex === 0) {
return null;
}

Expand All @@ -207,10 +206,6 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
);

const syncSelectionFromDOM = () => {
if (readOnly) {
setSelectedSections(null);
return;
}
const browserStartIndex = inputRef.current!.selectionStart ?? 0;
let nextSectionIndex: number;
if (browserStartIndex <= sections[0].startInInput) {
Expand Down Expand Up @@ -240,7 +235,7 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
return;
}

if (activeSectionIndex != null || readOnly) {
if (activeSectionIndex != null) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export const useFieldV7TextField: UseFieldTextField<true> = (params) => {
(sectionIndex: number) => (event: React.MouseEvent<HTMLDivElement>) => {
// The click event on the clear button would propagate to the input, trigger this handler and result in a wrong section selection.
// We avoid this by checking if the call to this function is actually intended, or a side effect.
if (event.isDefaultPrevented() || readOnly) {
if (event.isDefaultPrevented()) {
return;
}

Expand All @@ -303,10 +303,6 @@ export const useFieldV7TextField: UseFieldTextField<true> = (params) => {
});

const getInputContentFocusHandler = useEventCallback((sectionIndex: number) => () => {
if (readOnly) {
return;
}

setSelectedSections(sectionIndex);
});

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

export default function MobileDatePickerV6WithClearAction() {
return (
<LocalizationProvider dateAdapter={AdapterDayjs}>
<MobileDatePicker
label="Mobile Date Picker"
defaultValue={dayjs('2022-04-17')}
slotProps={{
actionBar: {
actions: ['clear', 'cancel', 'accept'],
},
}}
/>
</LocalizationProvider>
);
}
13 changes: 13 additions & 0 deletions test/e2e/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,19 @@ async function initializeEnvironment(
'04/11/2022',
);
});

it('should have consistent `placeholder` and `value` behavior', async () => {
await renderFixture('DatePicker/MobileDatePickerV6WithClearAction');

const input = page.getByRole('textbox');

await input.click({ position: { x: 10, y: 2 } });
await page.getByRole('button', { name: 'Clear' }).click();

await input.blur();
expect(await input.getAttribute('placeholder')).to.equal('MM/DD/YYYY');
expect(await input.inputValue()).to.equal('');
});
});
});

Expand Down