Coder Social home page Coder Social logo

Comments (14)

Jaraujo6 avatar Jaraujo6 commented on April 28, 2024 4

I think this issue should be re-opened as well. I am currently rendering a React-Select component in a Formik form, and was running into issues because there wasn't an obvious way to run the React-Select onBlur after onChange.

To be more specific, the form has a validation that says the input field is a required field. To ensure this, my onBlur handler sets the Formik field to touched which triggers the validation handler to run and surface the error:

<Select
  ...
  onBlur={() => setFieldTouched()}
/>

When I do select an option I am setting the Formik field manually in the Select onChange:

<Select
  ...
  onChange={({ value }: { value: string; label: string }) => setFieldValue(value)}
/>

Lastly, I also have a design requirement that the dropdown loses focus on selection, which is easily handled with the blurInputOnSelect flag.

My issue is that enabling the blurInputOnSelect forces the onBlur handler to run before the onChange handler, so the validation error was surfacing even though the field was valid.
After much trial and error, the only solution I could think of was to use setTimeout to force the Formik handler to get added to behind the onChange handler in the event queue.

<Select
  ...
  onChange={({ value }: { value: string; label: string }) => setFieldValue(value)}
  onBlur={() => {
       setTimeout(() => setFieldTouched(), 1);
   }}
  blurInputOnSelect
/>

This solution is not ideal as it is best practice to avoid async functions in react outside of hooks. Is it possible to introduce a blurOnChange flag or modify the blurOnInputSelect flag to mimic this behavior? Is there a reason the onBlur has to occur before onChange?

@JedWatson if you have a moment I would greatly appreciate your insight on this matter.

from react-select.

jorrit avatar jorrit commented on April 28, 2024 1

Why was this issue closed?

In the docs I read that there is a focus() method on the react-select element, wouldn't a complementary blur() be in order?

from react-select.

swiharta avatar swiharta commented on April 28, 2024 1

The setting autoBlur helped in my case.

from react-select.

Vandell63 avatar Vandell63 commented on April 28, 2024 1

Hey there! maybe it can help for material-ui 1.. user but can work for others too.
I use an Input and add the React-Select component in inputComponent of Input props.
Then add your ref to your select.
You can now access it with :
this.refs.select.input.input.blur() ( twice input here, cause the first one ref the AutosizeInput and the second one is the input Element)
Hope it help someone.

from react-select.

loganfuller avatar loganfuller commented on April 28, 2024

+1

This would be very useful.

from react-select.

mdressman avatar mdressman commented on April 28, 2024

+1

from react-select.

seansfkelley avatar seansfkelley commented on April 28, 2024

I wrote 0bfba99 for myself to fix this problem, but it's not configurable nor thoroughly tested. I don't like the copy-paste but it seems to work fine in all cases (multi-select included). I can issue a PR if @JedWatson thinks it's fine.

from react-select.

dcousens avatar dcousens commented on April 28, 2024

@jedverity use a ref?

Then you would just do ref.value.blur() AFAIK.

See here for more information on ref.

from react-select.

glortho avatar glortho commented on April 28, 2024

@dcousens The reason for querySelector is that putting a ref on <Select /> just returns the div.Select wrapper, when in fact the <input> that needs blurring is nested a few generations below.

So I can fork the lib and put a ref on that input and then do React.findDOMNode( myInputRef ).blur() but if I'm forking the lib (which I've done), I'm just changing the behavior to my specs, having it blur by default.

Let me know if I misunderstood your suggestion!

from react-select.

smontlouis avatar smontlouis commented on April 28, 2024

+1

from react-select.

tomaszlukasik avatar tomaszlukasik commented on April 28, 2024

@glortho It was a long time ago, but it might be helpful for others.
Looks like input is rendered with ref="input",=, If you have your Select with ref="select", you can call this.refs.select.refs.input.blur().

from react-select.

moneydance avatar moneydance commented on April 28, 2024

Ability to call blur would be nice.

from react-select.

lukfol avatar lukfol commented on April 28, 2024

My workaround:
<input style={{width:"0px",border:"none",padding:"0"}} ref={r=>this.dummyInput=r}/>
then in Select element:
onChange={ev=>{setTimeout(()=>{ if(this.dummyInput) this.dummyInput.focus(); },200);}}
timeout is needed

from react-select.

Alacho2 avatar Alacho2 commented on April 28, 2024

Coming back to this now because it's still relevant. How do you propose solving this, @JedWatson ?

EDIT: The standard .blur(), .focus() and so forth can found under this.refs.yournamehere.select.

from react-select.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.