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 removedValue into onChange clear action meta #3911

Merged
merged 5 commits into from
Feb 4, 2021

Conversation

eugenet8k
Copy link
Contributor

@eugenet8k eugenet8k commented Jan 21, 2020

The similar actions remove-value and pop-value have removedValue
in their onChange meta arguments.

It would be reasonable to pass the whole this.state.selectValue as
removedValue assuming that all selected values are removed upon clear.

This will help some client code to add additional logic in onChange
handlers like filtering of fixed values to preserve after clear without
referring to the options array.

@changeset-bot
Copy link

changeset-bot bot commented Jan 21, 2020

🦋 Changeset detected

Latest commit: 43c8330

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 Jun 1, 2020

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 43c8330:

Sandbox Source
react-codesandboxer-example Configuration

@eugenet8k
Copy link
Contributor Author

@gwyneplaine and @JedWatson I have rebased to the current master branch. This PR still seems reasonable IMO. I think it is good to follow existing ReactSelect API of passing items that are affected by current action in the event context.

@bladey bladey added pr/needs-review PRs that need to be reviewed to determine outcome enhancement and removed uncertain labels Jun 1, 2020
@bladey bladey added pr/enhancement PRs that are specifically enhancing the project and removed category/enhancement labels Jun 24, 2020
@eugenet8k eugenet8k force-pushed the clear-action-arguments branch 2 times, most recently from 92324ab to 005969b Compare January 15, 2021 17:41
@eugenet8k
Copy link
Contributor Author

@Methuselah96 and @JedWatson please review. Despite all the recent improvements (thank you!) this commit still seems relevant and brings value IMO.

Copy link
Collaborator

@Methuselah96 Methuselah96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a changeset?

@@ -720,7 +720,8 @@ export default class Select extends Component<Props, State> {
this.focusInput();
};
clearValue = () => {
this.onChange(null, { action: 'clear' });
const { selectValue } = this.state;
this.onChange(null, { action: 'clear', removedValue: selectValue });
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be an array, whereas the removedValue on the other actions only remove one option at a time, so we should probably make this plural so that the distinction is clear.

Suggested change
this.onChange(null, { action: 'clear', removedValue: selectValue });
this.onChange(null, { action: 'clear', removedValues: selectValue });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Methuselah96 I certainly see the logic behind your suggestion, but the existing selectValue is also an Array, so I thought it would be rather confusing to introduce removedValues and not having selectValues in a first place. As a developer that got to know with ReactSelect API recently, I would rather go with a single removedValue, that generally describes a Value (regardless of type) that is being removed. But if you guys insist I will update it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The selectValue in state is not exposed in the API, it's a private name, whereas this is in the public API.

I intend to do an internal refactor to rename selectValue to selectedOptions when I get the chance, but this would not be a breaking change, whereas renaming removedValue to removedValues would be a breaking change. That is why I'm inclined to prefer to get this name right up-front.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Methuselah96 thanks for the expanded explanation, this all make sense. I have pushed the updated commit.

Copy link
Collaborator

@ebonow ebonow Feb 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to play devil's advocate with semantics, but I struggle with this because this does introduce yet one more nuance. As a developer new to this library with all of the onChange actions, am I interested in value, option, removedValue, or removedValues?

removedValues would inaccurately be identified as plural if the Select is not multi.

One other possible fringe nuance comes to mind. Would it be possible to create a use case to remove multiple values at once, and if so, wouldn't the expected actionMeta for remove-value expect a removedValue prop, but then for clear becomes removedValues.

In my mind, a singular selectValue was cleared, so removedValue (or even possibly clearedValue) makes more sense for me. Philosophically, my preference is that the actionMeta object be more predictable instead of the user being responsible for knowing all the different object shapes for different actions.

I agree that the terminology used to describe a value is confusing and have secretly also desired to see it changed to the distinction selected so that options can just selected or unselected.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my mind you would always filter the action meta by the action property before you do anything with the other properties. As a developer I would look at the different shapes that an action meta can be and look at the appropriate properties for that action.

The distinction is that removedValues would always be an array (even if it's a single select). removedValue is always a single option. I think it's important to name them differently in order to know that you have to deal with them separately. If we did decide to go with removedValue it would be a breaking change because current code might be depending on the fact that removedValue is not an array.

I think we're all in general agreement that the current naming is confusing. Hopefully we can we work toward clarify it moving forward, but for right now I think this is the best we can do.

@eugenet8k eugenet8k force-pushed the clear-action-arguments branch 2 times, most recently from 642a716 to 001a3cd Compare January 15, 2021 18:52
@eugenet8k
Copy link
Contributor Author

@Methuselah96 @JedWatson please approve. All feedback was addressed.

@Methuselah96 Methuselah96 added this to the 4.1 milestone Jan 22, 2021
@Methuselah96
Copy link
Collaborator

Methuselah96 commented Jan 22, 2021

@eugenet8k Apologies for the delay on this and thank you for your patience. We're working on releasing a 4.0 in order to resolve #4094. I've added this to the 4.1 milestone and we'll review this PR to see if we want to include it in that release.

@Methuselah96
Copy link
Collaborator

@eugenet8k My apologies, but it looks like there are some merge conflicts with #4339 (that we merged for the 4.0 release). Would you mind resolving those conflicts? Hopefully we can get this merged in soon if the rest of the team is on board with the change.

The similar actions `remove-value` and `pop-value` have `removedValue`
in their `onChange` meta arguments.

It would be reasonable to pass the whole `this.state.selectValue` as
`removedValues` assuming that all selected values are removed upon `clear`.

This will help some client code to add additional logic in `onChange`
handlers like filtering of fixed values to preseve after `clear` without
referring to `options` array.
@Kepro
Copy link

Kepro commented Apr 13, 2021

just a side note, typescript definition is wrong and also docs

  onChange(value, { action, removedValue }) {
    switch (action) {
      case 'remove-value':
      case 'pop-value':
        if (removedValue.isFixed) {
          return;
        }
        break;
      case 'clear':
        value = colourOptions.filter(v => v.isFixed);
        break;
    }

    value = orderOptions(value);
    this.setState({ value: value });
  }

it should be now

  onChange(value, { action, removedValue, removedValues }) {
    switch (action) {
      case 'remove-value':
      case 'pop-value':
        if (removedValue.isFixed) {
          return;
        }
        break;
      case 'clear':
        value = removedValues.filter(item => item.isFixed);
        break;
    }

    value = orderOptions(value);
    this.setState({ value: value });
  }

typescript-bot pushed a commit to DefinitelyTyped/DefinitelyTyped that referenced this pull request Apr 15, 2021
… by @Kepro

* add removedValues to clearActionMeta

regarding to merge request from JedWatson/react-select#3911

* add missing OptionType

* fix missing definition to types
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants