-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix backspace to shift focus to previous element in useDateSegment #5715
Fix backspace to shift focus to previous element in useDateSegment #5715
Conversation
@@ -98,6 +98,7 @@ export function useDateSegment(segment: DateSegment, state: DateFieldState, ref: | |||
let parsed = parser.parse(newValue); | |||
if (newValue.length === 0 || parsed === 0) { | |||
state.clearSegment(segment.type); | |||
focusManager.focusPrevious(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I feel like focus should remain in the field. I'm used to deleting the entire segment to type in my new input, but open to other opinions from the team
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if the segment is already cleared, it's improved UX if backspace moves focus to the previous segment. We just should ensure the focus doesn't wrap (i.e. don't move focus if focus is already on the first segment.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think I agree with Daniel on this one. Personally, if I backspace to clear out a segment, that means I want to enter a new value for that same segment, not go back to the previous segment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just sharing my thoughts here - in the current implementation of DateSegment, it automatically moves to the next field once the input is complete, but it doesn’t revert to the previous field when using the backspace key. This is aligned with Chrome’s behavior.
However, thinking about consistency, it somehow feels more intuitive if the backspace would actually take us back to the previous field.
In the case of Safari, the focus doesn’t automatically move to the next field after input, nor does it revert to the previous one with a backspace. This can be confirmed at the following link.
https://developer.mozilla.org/docs/Web/HTML/Element/input/date
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If everyone is on board with this behavior, I will make the adjustment as suggested by @reidbarber, ensuring that the focus doesn't move back if it's already on the first segment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like our current implementation is fine. It seems more natural for the focus to shift to the previous input field when the final value is deleted, not just when the current segment is empty. For instance, if the focus is on a placeholder and backspace moves the focus back, that feels like too much. What do you all think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO I feel like auto moving focus once the input segment is cleared is too much because it assumes the user is wants to move to the previous date segment when they could just be trying to input another value in the original input segment. Auto moving focus forward when a segment is filled in on the other hand feels fine because there isn't another operation that the user might be doing once the segment is filled in
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I concur with @LFDanLu +1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I certainly understand that reasoning. In that case, let's proceed with adjusting the behavior to move focus back with the Backspace key only when the date segment is empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have fixed it, please review when you have time!
GET_BUILD |
Build successful! 🎉 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR, looks like it's working well. Tested in Chrome/Safari/FF on MacOS
GET_BUILD |
let newValue = segment.text.slice(0, -1); | ||
newValue = newValue === '0' ? '' : newValue; | ||
let parsed = parser.parse(newValue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did some digging and I think we'll need to change this bit of logic to something like
let newValue = segment.text.slice(0, -1); | |
newValue = newValue === '0' ? '' : newValue; | |
let parsed = parser.parse(newValue); | |
let newValue = segment.text.slice(0, -1); | |
let parsed = parser.parse(newValue); | |
newValue = parsed === 0 ? '' : newValue; |
The reason for this is that the segment text is not always to be Arabic numerals (e.g. 0, 1, 2, 3 etc). If you go to this story locally and change the locale in the story to "Arabic (Egypt)" and log out what newValue is, you'll notice that it will return values like ٤
. If you then switch your keyboard into an Arabic format so pressing 1
would return ١
, you would then see the bug still exists where the focus auto advances after clearing the minutes time field. If we parse the value first, we can get around this issue
Lemme know if that makes sense or if you need help reproducing !
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. I had overlooked that perspective, thanks for pointing it out! I've tried it locally and I understand the issue now.
It looks like we can also fix the bug where input becomes impossible after using backspace to clear the minutes field in cases other than Arabic numerals, like with Arabic (Egypt).
I'll be adopting the commit you suggested. Thanks!
Co-authored-by: Daniel Lu <[email protected]>
GET_BUILD |
Build successful! 🎉 |
## API Changes
unknown top level export { type: 'any' } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for the PR!
Closes #5261 , #5223
✅ Pull Request Checklist:
📝 Test Instructions:
Test 1: Re-entering Data in the Time Field
Test 2: Backspace Focus Shift on Empty DateSegment
🧢 Your Project: