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

Add prevInputValue to action meta #4785

Merged
merged 10 commits into from Oct 28, 2021

Conversation

Rall3n
Copy link
Collaborator

@Rall3n Rall3n commented Sep 17, 2021

Until now if you want to do a feature like keeping the inputValue after an option has been selected you would have to manage the inputValue prop on your own to have access to the previous value.

With this PR the need for the extra state would be removed as the current value would be available trough the meta object in the onInputChange handler, offering a much better un-controlled behaviour.

Before:

const [inputValue, setInputValue] = useState('');

const onInputChange = useCallback((val, { action }) => {
  if (action === 'set-value') return inputValue;

  setInputValue(val);
  return val;
}, [inputValue]);

<Select
  inputValue={inputValue}
  onInputChange={onInputChange}
  closeMenuOnSelect={false}
/>

After:

<Select
  onInputChange={(value, { action, prevInputValue }) => action === 'set-value' ? prevInputValue : value}
  closeMenuOnSelect={false}
/>

@changeset-bot
Copy link

changeset-bot bot commented Sep 17, 2021

🦋 Changeset detected

Latest commit: a694069

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
react-select Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codesandbox-ci
Copy link

codesandbox-ci bot commented Sep 17, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit a694069:

Sandbox Source
react-codesandboxer-example Configuration

@ebonow ebonow modified the milestone: 5.2 Oct 13, 2021
@ebonow
Copy link
Collaborator

ebonow commented Oct 22, 2021

Adding to discussion here to give more transparency. This was discussed internally on a Slack channel. There is some conversation about the naming of the object properties in the actionMeta.

While value or prevValue might make sense in this particular context, there is already confusion about what constitutes a "value" in react-select as it would now mean either

  1. The prop "value" of an option
  2. The current "value" (ie: selected options) of the select
  3. The "value" attribute of the Input component (which is normally represented as inputValue)

The idea of these changes makes sense, and while not necessarily required as a part of this PR, it may be useful to introduce the Select value (definition 2 from above) as a means that users can have an editable Select that doesn't wipe out the value onBlur or onChange without requiring it to be a controlled input.

Currently, it would need to be done similar to this:

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

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

    if (action === "input-change") {
      setInputValue(inputValue);
    }
  };

  const onChange = (option) => {
    setValue(option);
    setInputValue(option?.label || "");
  };

Where as I was thinking it could become this:

const onInputChange = (inputValue, { action, value }) =>
  (action === "input-blur" || action === "set-value")
    ? (value?.label || '')
    : inputValue

Introducing the value of the Select into the actionMeta would likely be better named as value, so perhaps a more understandable name would be prevInputValue or some other deviation of the name ___InputValue

@Rall3n
Copy link
Collaborator Author

Rall3n commented Oct 22, 2021

It's prevInputValue now.

@ebonow ebonow changed the title Add current inputValue to action meta Add prevInputValue to action meta Oct 26, 2021
@JedWatson
Copy link
Owner

Thanks @Rall3n!

@JedWatson JedWatson merged commit 20ea030 into JedWatson:master Oct 28, 2021
@Rall3n Rall3n deleted the current-input-value-meta branch February 2, 2022 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants