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 section clearing behavior on Android #13652

Merged
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 @@ -2030,13 +2030,13 @@ describe('<DateField /> - Editing', () => {
fireEvent.change(input, { target: { value: initialValueStr.replace('23', '1') } });
});

expectFieldValueV6(input, '11/21/2022');
expectFieldValueV6(input, '11/01/2022');
});

it('should support letter editing', () => {
// Test with v6 input
const v6Response = renderWithProps({
defaultValue: adapter.date('2022-05-16'),
defaultValue: adapter.date('2022-01-16'),
format: `${adapter.formats.month} ${adapter.formats.year}`,
enableAccessibleFieldDOMStructure: false,
});
Expand All @@ -2057,10 +2057,10 @@ describe('<DateField /> - Editing', () => {
fireEvent.change(input, { target: { value: ' 2022' } });

// Set the key pressed in the selected section
fireEvent.change(input, { target: { value: 'u 2022' } });
fireEvent.change(input, { target: { value: 'a 2022' } });
});

expectFieldValueV6(input, 'June 2022');
expectFieldValueV6(input, 'April 2022');
});
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
inputRef.current.setSelectionRange(selectionStart, selectionEnd);
}
}
setTimeout(() => {
// handle case when the selection is not updated correctly
// could happen on Android
if (
inputRef.current &&
inputRef.current === getActiveElement(document) &&
(inputRef.current.selectionStart !== selectionStart ||
inputRef.current.selectionEnd !== selectionEnd)
) {
interactions.syncSelectionToDOM();
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm afraid of a possible endless recursion here. 🤔
WDYT, do we need an additional safeguard against it here?

Copy link
Member

Choose a reason for hiding this comment

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

Do you have a clearer idea of a scenario when this recursion might happen?
Tbh I don't really know how to add safeguards here 😬

Copy link
Member Author

Choose a reason for hiding this comment

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

I can't think of any. 🤔

}
});
}

// Even reading this variable seems to do the trick, but also setting it just to make use of it
Expand Down Expand Up @@ -377,10 +389,9 @@ export const useFieldV6TextField: UseFieldTextField<false> = (params) => {
if (keyPressed.length === 0) {
if (isAndroid()) {
setTempAndroidValueStr(valueStr);
} else {
resetCharacterQuery();
clearActiveSection();
}
resetCharacterQuery();
clearActiveSection();

return;
}
Expand Down