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] Select the first section instead of last when clicking right of content #9005

Merged
merged 4 commits into from
May 16, 2023
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 @@ -174,7 +174,7 @@ describe('<TimeField /> - Editing', () => {
format: adapter.formats.fullTime12h,
key: 'ArrowUp',
expectedValue: 'hh:mm AM',
cursorPosition: 14,
cursorPosition: 7,
});
});

Expand All @@ -184,7 +184,7 @@ describe('<TimeField /> - Editing', () => {
defaultValue: adapter.date(new Date(2022, 5, 15, 2, 12, 25)),
key: 'ArrowUp',
expectedValue: '02:12 PM',
cursorPosition: 14,
cursorPosition: 7,
});
});

Expand All @@ -194,7 +194,7 @@ describe('<TimeField /> - Editing', () => {
defaultValue: adapter.date(new Date(2022, 5, 15, 14, 12, 25)),
key: 'ArrowUp',
expectedValue: '02:12 AM',
cursorPosition: 14,
cursorPosition: 7,
});
});
});
Expand Down Expand Up @@ -304,7 +304,7 @@ describe('<TimeField /> - Editing', () => {
it('should set meridiem to AM when pressing "a" and no value is provided', () => {
testFieldChange({
format: adapter.formats.fullTime12h,
cursorPosition: 17,
cursorPosition: 7,
// Press "a"
keyStrokes: [{ value: 'hh:mm a', expected: 'hh:mm AM' }],
});
Expand All @@ -313,7 +313,7 @@ describe('<TimeField /> - Editing', () => {
it('should set meridiem to PM when pressing "p" and no value is provided', () => {
testFieldChange({
format: adapter.formats.fullTime12h,
cursorPosition: 17,
cursorPosition: 7,
// Press "p"
keyStrokes: [{ value: 'hh:mm p', expected: 'hh:mm PM' }],
});
Expand All @@ -323,7 +323,7 @@ describe('<TimeField /> - Editing', () => {
testFieldChange({
format: adapter.formats.fullTime12h,
defaultValue: adapter.date(new Date(2022, 5, 15, 14, 12, 25)),
cursorPosition: 17,
cursorPosition: 7,
// Press "a"
keyStrokes: [{ value: '02:12 a', expected: '02:12 AM' }],
});
Expand All @@ -333,7 +333,7 @@ describe('<TimeField /> - Editing', () => {
testFieldChange({
format: adapter.formats.fullTime12h,
defaultValue: adapter.date(new Date(2022, 5, 15, 14, 12, 25)),
cursorPosition: 17,
cursorPosition: 7,
// Press "p"
keyStrokes: [{ value: '02:12 p', expected: '02:12 PM' }],
});
Expand Down
18 changes: 12 additions & 6 deletions packages/x-date-pickers/src/internals/hooks/useField/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,18 @@ export const useField = <
return;
}
const browserStartIndex = inputRef.current!.selectionStart ?? 0;
const nextSectionIndex =
browserStartIndex <= state.sections[0].startInInput
? 1 // Special case if browser index is in invisible characters at the beginning.
: state.sections.findIndex(
(section) => section.startInInput - section.startSeparator.length > browserStartIndex,
);
let nextSectionIndex: number;
if (browserStartIndex <= state.sections[0].startInInput) {
// Special case if browser index is in invisible characters at the beginning
nextSectionIndex = 1;
} else if (browserStartIndex >= state.sections[state.sections.length - 1].endInInput) {
// If the click is after the last character of the input, then we want to select the 1st section.
nextSectionIndex = 1;
} else {
nextSectionIndex = state.sections.findIndex(
(section) => section.startInInput - section.startSeparator.length > browserStartIndex,
);
}
const sectionIndex = nextSectionIndex === -1 ? state.sections.length - 1 : nextSectionIndex - 1;

setSelectedSections(sectionIndex);
Expand Down