fluentui icon indicating copy to clipboard operation
fluentui copied to clipboard

Cursor jumps to the end on selecting and editing text on freeform combobox

Open syedinaqvi opened this issue 3 years ago • 9 comments

Environment Information

  • Package version(s): 8.10.0, Also tried it on 8.15.0 and still repro.
  • Browser and OS versions: (fill this out if relevant), not relevant, repro on Edge, Chrome and other browsers.

Please provide a reproduction of the bug in a codepen:

https://codepen.io/syedinaqvi/pen/abJaOyO

Actual behavior:

Go to codepen link here https://codepen.io/syedinaqvi/pen/abJaOyO. Select one option from the dropdown, e.g., select 11:00 PM (ensure that Allow freeform is enabled). Select the 00 part of time and enter 2 numbers to replace the selected value. After first number is pressed cursor jumps to the end.

Expected behavior:

Cursor should not jump to the end.

Priorities and help requested:

Are you willing to submit a PR to fix? No

Requested priority: (Blocking, High, Normal, Low), High, as it repro only on v8.

Products/sites affected: Outlook Web

syedinaqvi avatar Jun 09 '21 21:06 syedinaqvi

I can confirm this is a regression in v8 (repros in 8.18.0)!

miroslavstastny avatar Jun 14 '21 09:06 miroslavstastny

@microsoft/cxe-red, @ecraig12345 - may I ask you for your help here?

miroslavstastny avatar Jun 14 '21 12:06 miroslavstastny

Maybe @czearing can help look into this?

ecraig12345 avatar Jun 14 '21 19:06 ecraig12345

@ecraig12345 Sure! I'll take a look

czearing avatar Jun 14 '21 22:06 czearing

@czearing, I looked into this same issue a week ago, but was unable to repro it until this weekend when I realized that the problem does not repro if you use the backspace key or either of the arrow keys before typing into the combobox. Either of those actions disables autofill in the combobox and allows the freeform entry to continue as expected.

In the repro case, the user begins typing into the combobox at some point in the middle of the string (eg. editing the value) while autofill is still enabled.

I'm not familiar with this codebase, but it seems like autofill ought to be disabled whenever the cursor location is not at the end of the input. FWIW, I have a proposed fix out at https://github.com/KAnder425/fluentui/commit/96068fc58b70f109e17059cbc800fb1d76c41167

Let me know if that helps!

KAnder425 avatar Jun 14 '21 23:06 KAnder425

@KAnder425 Thanks for the investigation notes; that should help!

My first guess was that this was related to ComboBox being refactored from a class component to a function component as part of version 8 (also Autofill was modified to get rid of componentWillReceiveProps), but looking at the history, there was a more recent change #17475 which modified the cursor behavior. It looks like @TristanWatanabe has an open PR #18186 addressing a different cursor behavior issue resulting from that change, but it's possible that will fix this issue too.

ecraig12345 avatar Jun 15 '21 01:06 ecraig12345

Because this issue has not had activity for over 150 days, we're automatically closing it for house-keeping purposes.

Still require assistance? Please, create a new issue with up-to date details.

msft-fluent-ui-bot avatar Dec 25 '21 02:12 msft-fluent-ui-bot

@czearing We are still seeing this happening. Is there any chance it can be revisited? Can the PR you raised still be completed?

msnyder-msft avatar May 11 '22 20:05 msnyder-msft

Outlook users are still complaining about this problem, as of September, 2022

stevefr777 avatar Sep 15 '22 16:09 stevefr777

This does not repro for me anymore. Can you confirm that it is resolved?

micahgodbolt avatar Feb 10 '23 19:02 micahgodbolt

This issue has been automatically marked as stale because it has marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. Thank you for your contributions to Fluent UI!

msft-fluent-ui-bot avatar Feb 14 '23 20:02 msft-fluent-ui-bot

It still repro for the Outlook users. I was also able to repro it here: https://codepen.io/syedinaqvi/pen/abJaOyO Using these repro steps:

  • Select one option from the dropdown, e.g., select 11:00 PM (ensure that Allow freeform is enabled).
  • Select the 00 part of time and enter 2 numbers to replace the selected value. Result: After first number is pressed cursor jumps to the end.

syedinaqvi avatar Feb 16 '23 00:02 syedinaqvi

Ok, i see the difference. You need to select the two digits with your mouse and then try to replace them. If you select via keyboard it works as expected.

micahgodbolt avatar Feb 16 '23 15:02 micahgodbolt

Alright, we were able to triage the issue and locate the source of the bug. Here's a quick TL:dr

  1. In React, if you have an input with a fixed value, but no onChange event, on keystroke, your focus will be sent to the end of the input https://codepen.io/micahgodbolt/pen/MWqweNg?editors=0010 Sarah also created a great example of this happening in any controlled JS https://jsfiddle.net/6wxsbykm/
  2. Combobox uses a controlled Autofill component, but the Autofill also has its own internal state
  3. So, on keypress the Autofill re-renders, but since the value doesn't change, it moves the cursor to the end
  4. On the next render, Combobox passes down the new value, and the Autofill updates the displayed value (but not until after the cursor has moved)

Possible solution

I think the issue is calling onInputValueChange after calling the local setState

this.setState({ inputValue: newValue }, () => onInputValueChange?.(newValue, composing));

Moving the onInputValueChange up to before setState makes the displayed value update properly and the cursor is not moved.

I'm not sure if this has any other negative effects though, and will need to test a bit more.

micahgodbolt avatar Feb 16 '23 23:02 micahgodbolt

Re-activating this bug, as the fix for this bug is being reverted in PR https://github.com/microsoft/fluentui/pull/27323 because it caused a regression:

  • https://github.com/microsoft/fluentui/issues/27256

behowell avatar Mar 24 '23 20:03 behowell