Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added onBlurResetsInput property to clear input onBlur or leave parti… #3242

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

redheadedstep
Copy link

Updated Select.js with onBlurResetsInput prop to not clear input onBlur
Updated tests to check that onBlurResetsInput does not call onInputChange if set to false
Updated documentation with onBlurResetsInput property

…al input

Updated tests to check that onBlurResetsInput does not call onInputChange if set to false
Updated documentation with onBlurResetsInput property
@redheadedstep
Copy link
Author

Committed for PR #3189

@redheadedstep
Copy link
Author

When will this be released? It's been almost 2 months?

@dfuruya
Copy link

dfuruya commented Jan 22, 2019

^^^ +1

I tried #3189 (comment) but that still didn't work for me....

@redheadedstep
Copy link
Author

@JedWatson any update on this update?

@trixtar
Copy link

trixtar commented Mar 1, 2019

@JedWatson it would be great if you reviewed and merged this one

@Rall3n
Copy link
Collaborator

Rall3n commented Mar 8, 2019

I don´t think this PR is necessary because you can achieve the same result with the current state of react-select. Please see #3189 (comment) for an example.

@trixtar
Copy link

trixtar commented Mar 8, 2019 via email

@follower90
Copy link

It seems that solution in #3189 (comment) has problems with last react-select (2.4.1), cursor position stays in the end of input and doesn't move on click.

But it works fine on 2.1.1 (follow code sandbox link from the comment).

@Rall3n
Copy link
Collaborator

Rall3n commented Mar 11, 2019

It seems that solution in #3189 (comment) has problems with last react-select (2.4.1), cursor position stays in the end of input and doesn't move on click.

But it works fine on 2.1.1 (follow code sandbox link from the comment).

The original issue was to keep the value of the input when blurring, not to set the cursor (@follower90). For that you have to do a little more, like overwriting Input component to stop onMouseDown event from bubbling.

const Input = (props) => <components.Input {...props} onMouseDown={(e) => e.stopPropagation()} />;

But that is not the issue this PR is addressing. It is addressing the problem for which I have provided a solution given by the library, which still works in v2.4.1.

@barbalex
Copy link

barbalex commented Apr 1, 2019

@Rall3n please see my comments in #3189 (comment) on why that does not work in a real world case where you need to save values to the database and also need to know when the user has deleted a value that was only typed, not choosen from an option

@bladey bladey added pr/needs-review PRs that need to be reviewed to determine outcome enhancement pr/priority PRs that should be addressed sooner rather than later and removed deprecated labels Jun 4, 2020
@bladey bladey added pr/enhancement PRs that are specifically enhancing the project and removed category/enhancement labels Jun 24, 2020
@sacummings91
Copy link

What happened with this? Would be nice to have an easy way to stop the blur from resetting the input

@ebonow
Copy link
Collaborator

ebonow commented Oct 22, 2021

What happened with this? Would be nice to have an easy way to stop the blur from resetting the input

It's currently possible, though not entirely straightforward (more on this below).

Regarding rolling up into one prop, one complexity I've encountered is that this wouldn't solve everyone's use case. There seems to be a bit of disagreement on what should happen if there is an existing selected value, and the user blurs with some inputValue that does not represent the selected value.

Should onBlur retain the inputValue or revert back to the label of the selectedOption? It seems that both use cases are valid in different contexts so a boolean may not be adequate in solving this.

As to how to implement this, here's a codesandbox example.
https://codesandbox.io/s/react-select-editable-and-selectable-6nfdv

  const [value, setValue] = useState();
  const [inputValue, setInputValue] = useState("");

  const onInputChange = (inputValue, { action }) => {
    // onBlur => setInputValue to last selected value
    if (action === "input-blur") {
       setInputValue(value?.label || "");
    }

    // onInputChange => update inputValue
    if (action === "input-change") {
      setInputValue(inputValue);
    }
  };

  const onChange = (option) => {
    setValue(option);
    setInputValue(option?.label || "");
  };
  
  return (
    <Select value={value} 
      inputValue={inputValue} 
      onChange={onChange} 
      onInputChange={onInputChange} 
      {...otherProps...}
    />
  );

There's been some conversation in a different PR that talks about how this can potentially be done with a single onInputChange prop that doesn't require a controlled input. This would require adding the value to the actionMeta of the onInputChange, which seems like it could make this much easier to do by default.

@sacummings91
Copy link

I appreciate the thorough response. I have something similar to what you posted but not quite working right. I think I see why now thanks to your post. Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/enhancement PRs that are specifically enhancing the project pr/needs-review PRs that need to be reviewed to determine outcome pr/priority PRs that should be addressed sooner rather than later
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants