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

fix(Autocomplete): isItemSelected default check is not accessible #406

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

Conversation

ktmud
Copy link

@ktmud ktmud commented Oct 23, 2020

to: @williaster @alecklandgraf

Description

The doc states isItemSelected of Autocomplete will compare values by default, but without setting this prop, it's actually always returning false. Since changing the default behavior would be a breaking change, I'm adding null to allow prop values in typing so that users can have access to the actual default behavior.

Motivation and Context

See description.

Testing

Make sure <Autocomplete isItemSelected={null} > doesn't throw type check errors.

Screenshots

Checklist

  • My code follows the style guide of this project.
  • I have updated or added documentation accordingly.
  • I have read the CONTRIBUTING document.

@airbnb-bot
Copy link
Collaborator

airbnb-bot commented Oct 23, 2020

Size Changes

Package Diff ESM Prev ESM CJS Prev CJS
core +0.0% 567.36 KB 567.33 KB 709.52 KB 709.46 KB

Compared to master. File sizes are unminified and ungzipped.

View raw build stats

Previous (master)

{
  "apollo": {
    "esm": 10832,
    "lib": 14147
  },
  "app-shell": {
    "esm": 12906,
    "lib": 19874
  },
  "composer": {
    "esm": 68247,
    "lib": 101805
  },
  "core": {
    "esm": 580945,
    "lib": 726482
  },
  "forms": {
    "esm": 37350,
    "lib": 49298
  },
  "icons": {
    "esm": 156355,
    "lib": 205626
  },
  "layouts": {
    "esm": 15298,
    "lib": 20770
  },
  "metrics": {
    "esm": 5467,
    "lib": 7729
  },
  "test-utils": {
    "esm": 4279,
    "lib": 5937
  }
}

Current

{
  "apollo": {
    "esm": 10832,
    "lib": 14147
  },
  "app-shell": {
    "esm": 12906,
    "lib": 19874
  },
  "composer": {
    "esm": 68247,
    "lib": 101805
  },
  "core": {
    "esm": 580981,
    "lib": 726548
  },
  "forms": {
    "esm": 37350,
    "lib": 49298
  },
  "icons": {
    "esm": 156355,
    "lib": 205626
  },
  "layouts": {
    "esm": 15298,
    "lib": 20770
  },
  "metrics": {
    "esm": 5467,
    "lib": 7729
  },
  "test-utils": {
    "esm": 4279,
    "lib": 5937
  }
}

@@ -643,7 +643,7 @@ export default class Autocomplete<T extends Item = Item> extends React.Component
return this.getFilteredItems(this.state).map((item: T, index) => {
const value = this.props.getItemValue!(item);
const selected = this.props.isItemSelected
? this.props.isItemSelected(item, value)
? this.props.isItemSelected(item, this.state.value)
Copy link
Contributor

Choose a reason for hiding this comment

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

@ktmud I'm not sure I agree this is a bug. it looks like the api is simply passing the item value as the second argument, not the autocomplete value.

This would be a breaking change if someone had logic that assumed the second argument was the item value.

Copy link
Author

Choose a reason for hiding this comment

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

Currently there is not way to compare item value against state.value since isItemSelected has a default value of () => false, which you cannot override by passing undefined, the only other compatible value according to typing definition.

It would be weird (and probably not advisable) to check isItemSelected without checking it against the real selected value. Plus users shouldn't need item values, either, because they can easily get it from getItemValue.

Copy link
Contributor

@williaster williaster Oct 26, 2020

Choose a reason for hiding this comment

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

Plus users shouldn't need item values, either, because they can easily get it from getItemValue.

agree with this for sure

Currently there is not way to compare item value against state.value

can you give more context to when this is an issue? wouldn't you be able to get value from onChange callbacks? or in the case of a controlled component the user would be passing the value themselves?

Copy link
Author

@ktmud ktmud Oct 27, 2020

Choose a reason for hiding this comment

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

This function (isItemSelected) is used when rendering the menu items. I wanted to render selected values differently in the menu. Currently it's not possible to do that without passing in an isItemSelected override that depends on a value state in the parent component. If the renderItem API already passes a selected prop, then it should be correct, right?

@ktmud ktmud force-pushed the bugfix--autocomplete-isItemSelected branch from a63db89 to 6e8b6e2 Compare October 27, 2020 17:52
So users can use the default isItemSelected check without type warnings.
@ktmud ktmud force-pushed the bugfix--autocomplete-isItemSelected branch from 6e8b6e2 to 2bbc2ee Compare October 27, 2020 17:54
@ktmud ktmud changed the title fix(Autocomplete): isItemSelected should check against input value fix(Autocomplete): isItemSelected default check is not accessible Oct 27, 2020
@ktmud ktmud marked this pull request as ready for review October 27, 2020 17:59
Copy link
Contributor

@williaster williaster left a comment

Choose a reason for hiding this comment

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

Awesome, thanks for entertaining the non-breaking change idea 🙏

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.

3 participants