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

Fix backspace to shift focus to previous element in useDateSegment #5715

Merged
merged 10 commits into from
Feb 20, 2024

Conversation

ryo-manba
Copy link
Contributor

@ryo-manba ryo-manba commented Jan 18, 2024

Closes #5261 , #5223

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

Test 1: Re-entering Data in the Time Field

  1. Visit the Date Field Example in Storybook.
  2. Enter "45" into the Number Field for minutes.
  3. Clear the entry using the backspace key.
  4. Enter a new value, such as "30", for minutes.
  5. Confirm that you can correctly re-enter the minutes.

Test 2: Backspace Focus Shift on Empty DateSegment

  1. Visit the Date Field Example in Storybook.
  2. Enter "45" into the Number Field for minutes.
  3. Use the backspace key to clear the minutes entered.
  4. Press backspace again when the field is empty (showing placeholder).
  5. Confirm that the focus shifts to the previous field.
  6. Refocus on the minutes field and enter a new value, such as "30".
  7. Confirm that the minutes can be correctly re-entered.

🧢 Your Project:

@ryo-manba ryo-manba changed the title Fix focus shift on clearing minute fields Fix focus shift on useDateSegment Jan 18, 2024
@ryo-manba ryo-manba changed the title Fix focus shift on useDateSegment Fix backspace to shift focus to previous element in useDateSegment Jan 18, 2024
@@ -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();
Copy link
Member

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

Copy link
Member

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.)

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

@ryo-manba ryo-manba Jan 30, 2024

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?

Copy link
Member

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

Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Contributor Author

@ryo-manba ryo-manba Feb 9, 2024

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!

@snowystinger
Copy link
Member

GET_BUILD

@rspbot
Copy link

rspbot commented Feb 11, 2024

snowystinger
snowystinger previously approved these changes Feb 11, 2024
Copy link
Member

@snowystinger snowystinger left a 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

@LFDanLu
Copy link
Member

LFDanLu commented Feb 16, 2024

GET_BUILD

Comment on lines 100 to 102
let newValue = segment.text.slice(0, -1);
newValue = newValue === '0' ? '' : newValue;
let parsed = parser.parse(newValue);
Copy link
Member

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

Suggested change
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 !

Copy link
Contributor Author

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 <danilu@adobe.com>
@LFDanLu
Copy link
Member

LFDanLu commented Feb 20, 2024

GET_BUILD

@rspbot
Copy link

rspbot commented Feb 20, 2024

@rspbot
Copy link

rspbot commented Feb 20, 2024

## API Changes

unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any', access: 'private' }
unknown top level export { type: 'any', access: 'private' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'identifier', name: 'Column' }
unknown top level export { type: 'identifier', name: 'Column' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown type { type: 'link' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }
unknown top level export { type: 'any' }

Copy link
Member

@LFDanLu LFDanLu left a 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!

@snowystinger snowystinger merged commit fd3f09c into adobe:main Feb 20, 2024
26 checks passed
@ryo-manba ryo-manba deleted the fix/date-segment-backspace branch February 21, 2024 00:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Backspace on an empty DateSegment should focusPrevious
6 participants