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] Correctly handle events with a complete value insertion #9896

Merged
merged 7 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 9 additions & 4 deletions packages/x-date-pickers/src/internals/hooks/useField/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,18 @@ export const useField = <
return;
}

const valueStr = event.target.value;
const targetValue = event.target.value;
const eventData = (event.nativeEvent as InputEvent).data;
// Calling `.fill(04/11/2022)` in playwright will trigger a change event with the requested content to insert in `event.nativeEvent.data`
// usual changes have only the currently typed character in the `event.nativeEvent.data`
const useEventData = eventData && eventData.length > 1;
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure it can not be more than one character? I'm thinking about special keyboards or some auto-completion

I don't see issues, it's just to share a point of attention. I first thought about #9465 but it's not the same problem since IME keyboard calls multiple times the onChange

Copy link
Member Author

Choose a reason for hiding this comment

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

Are we sure it can not be more than one character? I'm thinking about special keyboards or some auto-completion

I'm definitely not sure that there wouldn't be any edge cases, but autocompletion is turned off, although, if any extension hacks it's way into this, I'd imagine that this change could only fix it and not break because it would probably use a similar API of emitting a change event with the full value string. 🤔

LukasTy marked this conversation as resolved.
Show resolved Hide resolved
const valueStr = useEventData ? eventData : targetValue;
LukasTy marked this conversation as resolved.
Show resolved Hide resolved
const cleanValueStr = cleanString(valueStr);

// If no section is selected, we just try to parse the new value
// If no section is selected or eventData should be used, we just try to parse the new value
// This line is mostly triggered by imperative code / application tests.
if (selectedSectionIndexes == null) {
updateValueFromValueStr(cleanValueStr);
if (selectedSectionIndexes == null || useEventData) {
LukasTy marked this conversation as resolved.
Show resolved Hide resolved
updateValueFromValueStr(useEventData ? eventData : cleanValueStr);
LukasTy marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand Down
24 changes: 24 additions & 0 deletions test/e2e/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,30 @@ const fakeNow = new Date('2022-04-17T13:37:11').valueOf();
await page.waitForSelector('[role="tooltip"]', { state: 'detached' });
expect(await page.getByRole('textbox').inputValue()).to.equal('04/11/2022');
});

it('should allow filling in a value and clearing a value', async () => {
await renderFixture('DatePicker/BasicDesktopDatePicker');
const input = page.getByRole('textbox');

await input.fill('04/11/2022');

expect(await input.inputValue()).to.equal('04/11/2022');

await input.blur();
await input.fill('');

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

it('should allow typing in a value', async () => {
await renderFixture('DatePicker/BasicDesktopDatePicker');
const input = page.getByRole('textbox');

await input.focus();
await input.type('04/11/2022');

expect(await input.inputValue()).to.equal('04/11/2022');
});
});
describe('<MobileDatePicker />', () => {
it('should allow selecting a value', async () => {
Expand Down