furqanZafar / react-selectize

http://furqanzafar.github.io/react-selectize/
Apache License 2.0
703 stars 138 forks source link

Escape-key clears values? #115

Open MalcolmDwyer opened 8 years ago

MalcolmDwyer commented 8 years ago

Escape key is clearing values out of MultiSelect.

It makes sense that the first hit of the escape key will close the dropdown. But if you hit escape again, it's clearing the values. I see onValuesChange getting called with [ ].

Is this expected behavior? Any nice way to override this?

I want clearing of values to only happen with a more deliberate action like clicking the 'X'. Some users will hit escape casually just move out of the form element.

(You can see it here in the top Multi-Select example, or the Tags example http://furqanzafar.github.io/react-selectize/#/ ... add a few new values, then hit escape twice and they all get cleared out).

Overall, great library though! Thanks!

marekhvolka commented 2 years ago

In case somebody faces the same issue, I have this code:

React.useEffect(() => {
    if (selectRef.current) {
      // @ts-ignore
      const func = selectRef.current?.refs.select.handleKeydown;

      // @ts-ignore
      selectRef.current.refs.select.handleKeydown = (args, e) => {
        if (e.which !== 27 || selectRef.current?.state.open) {
          func.call(selectRef.current?.refs.select, args, e);
        }
      };
    }
  }, [selectRef.current]);