react-select icon indicating copy to clipboard operation
react-select copied to clipboard

Option to Blur input after clear button click/ removing option in multi-select

Open dav-sap opened this issue 8 years ago • 5 comments

Currently when clicking on the clear button, to clear multiple values selected, or removing a selected option in the multi options, the control is automatically focused, this is very annoying, user might want to just clear/remove and keep doing his work, but it's automatically being focused.

The blurInputOnSelect works great but it's only when you select a new item not when you clear.

I saw all the hacky ways of using Refs and stuff, but using refs can always be a bit risky and can cause unexpected behavior (there's a reason it's deprecated). Any way to create a simple prop like this? I can help with a PR of my own as well

dav-sap avatar Oct 09 '18 13:10 dav-sap

Any news on this? Is that still not possible? I find that it only happens on mobile, but not on desktop. Is that on purpose?

xddz9 avatar Nov 28 '19 07:11 xddz9

Still a problem..

arnelamo avatar Oct 09 '20 22:10 arnelamo

~~I've started a fork of react-select. Feel free to resubmit this issue on the fork and/or submit a PR to resolve this issue on the fork and we can get it merged and released.~~

EDIT: :tada: I've archived the fork now that we've got some momentum in this repo and Jed is involved again. Sorry for the disturbance!

Methuselah96 avatar Nov 14 '20 16:11 Methuselah96

Greetings,

Much of the logic has been moved to the onChange handler instead of having so many props. Using the action meta api, the clear event can be listened for.

The hacky part of this unfortunately seems to be that blurring the input immediately has no effect, so a brief setTimeout is required. I'm hoping this is something that a major refactor into hooks will allow us to better control the rendering without timeouts, but time being, this will do the trick.

Working demo: codesandbox

  const selectRef = useRef();

  const onChange = (option, { action }) => {
    if (action === "clear") || (action === 'remove-value') {
      setTimeout(() => selectRef.current.select.blur(), 1);
    }
  };

  return <Select isClearable ref={selectRef} onChange={onChange} options={options} />

ebonow avatar Dec 19 '20 06:12 ebonow

I will close this issue for now. If anyone has any questions or concerns, please feel free to reply and we can re-open this if you feel it is still unresolved and relevant.

ebonow avatar Dec 19 '20 06:12 ebonow