downshift icon indicating copy to clipboard operation
downshift copied to clipboard

Controlling isOpen state removes initial highlightedIndex when opened via keyboard

Open austingreendev opened this issue 7 years ago • 2 comments

Relevant code or config

export default () => {
  const [isOpen, setIsOpen] = useState(false)

  return (
    <Downshift
      isOpen={isOpen} // This is line which causes the issue
      onStateChange={(changes, stateAndHelpers) => {
        if (Object.prototype.hasOwnProperty.call(changes, 'isOpen')) {
          setIsOpen(changes.isOpen)
        }
      }}>
      ...
  </Downshift>

What you did:

When using downshift in its fully uncontrolled state, the highlightedIndex is updated to either the first or last index depending on which key was used to trigger the open action.

Working Uncontrolled Example: uncontrolled

I would like to only control the isOpen prop and still retain the default highlightedIndex functionality when opening via a keyboard.

What I expected:

I would expect the isOpen prop to be the only state affected if I were to override it.

What happened:

When controlling the isOpen prop the highlightedIndex property is only updated after the second interaction.

Incorrect Controlled Example: controlled

Reproduction repository:

Here is a working example in codesandbox src/ordered-examples/01-basic-autocomplete.js

Edit downshift-examples

Problem description:

It appears that in the uncontrolled mode there are 2 changes triggered:

  • { isOpen: true, type: "__autocomplete_keydown_arrow_down__" }
  • { highlightedIndex: 0, type: "__autocomplete_keydown_arrow_down__" }

and in the controlled mode only a single change occurs:

  • { isOpen: true, type: "__autocomplete_keydown_arrow_down__" }

Suggested solution:

I'm not sure if this is a feature or a bug on how onStateChanges is handled normally 😄So any insight would be helpful here. If this is considered a bug I can dig into why there is only a single action being dispatched.

Thanks!

austingreendev avatar Apr 10 '19 14:04 austingreendev

I think the issue is that you are updating your isOpen variable after Downshift already set its state.

The flow now is like this:

  • you trigger Keydown.
  • Downshift gets that and tries to: set isOpen to true and set highlightedIndex to a value if you are using arrow up or down.
  • Downshift fails to do both of them, because you are controlling isOpen and because when it tries to update highlightedIndex, isOpen is still regarded as false.
  • Downshift did not change in any way, but it triggered the onStateChange for you.
  • you pick up onStateChange and set the isOpen variable to true.
  • Dowshift picks that up and opens the menu.

What you can do:

  1. control the highlightedIndex in this scenario as well.
  2. use stateReducer instead of onStateChange. The reducer tells you the changes that are about to happen, while onStateChange tells you about the changes that already have happened.

I would go with the second one. You can still control the isOpen but now you can also maintain the default highlightedIndex opening on 0 / last item.

Let me know if you succeed in fixing your scenario.

silviuaavram avatar Apr 10 '19 15:04 silviuaavram

Thanks for the tips @silviuavram, using stateReducer got me closer, but I was still running into issues of it not making it past this internalSetState call https://github.com/downshift-js/downshift/blob/master/src/downshift.js#L543-L547

After some troubleshooting I found that the reason it wasn't being called with my scenario was that my component had an itemCount of 0. This was due to me not rendering getMenuProps() and getItemProps() based on the isOpen state. (i.e. isOpen && <div {...getMenuProps()}>)

I now see in the docs that this can have adverse affects, including some accessibility failings. By removing that ternary and just hiding the menu visual it fixed my issue. 🎉

Thanks for your help!

austingreendev avatar Apr 15 '19 16:04 austingreendev