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

[Autocomplete] Forward props to renderTags() #32637

Merged
merged 5 commits into from
May 11, 2022
Merged

Conversation

emlai
Copy link
Contributor

@emlai emlai commented May 5, 2022

To be able to access e.g. getOptionLabel() inside renderTags()

Opportunity surfaced in #32548

To be able to access e.g. getOptionLabel() inside renderTags()
@danilo-leal danilo-leal added the component: autocomplete This is the name of the generic UI component, not the React module! label May 5, 2022
@michaldudak
Copy link
Member

You can define the type of ownerState as such:

export type AutocompleteOwnerState<
  T,
  Multiple extends boolean | undefined,
  DisableClearable extends boolean | undefined,
  FreeSolo extends boolean | undefined,
  ChipComponent extends React.ElementType = ChipTypeMap['defaultComponent'],
> = AutocompleteProps<T, Multiple, DisableClearable, FreeSolo, ChipComponent> & {
  disablePortal: boolean;
  focused: boolean;
  fullWidth: boolean;
  hasClearIcon: boolean;
  hasPopupIcon: boolean;
  inputFocued: boolean;
  popupOpen: boolean;
  size: OverridableStringUnion<'small' | 'medium', AutocompletePropsSizeOverrides>;
};

then pass ownerState instead of props to renderTags.

* @returns {ReactNode}
*/
renderTags?: (
value: T[],
getTagProps: AutocompleteRenderGetTagProps,
autocompleteProps: AutocompleteProps<T, Multiple, DisableClearable, FreeSolo, ChipComponent>,
state: AutocompleteOwnerState<T, Multiple, DisableClearable, FreeSolo, ChipComponent>,
Copy link
Member

Choose a reason for hiding this comment

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

Call it ownerState please, as this name is used across the codebase and it'll be clear what state we mention.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 👍

@mui-bot
Copy link

mui-bot commented May 10, 2022

Details of bundle changes

Generated by 🚫 dangerJS against c7ea708

Copy link
Member

@michaldudak michaldudak left a comment

Choose a reason for hiding this comment

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

Thanks for your work on this! I updated the code formatting (ran yarn prettier) and synced API docs with the changes you made (yarn docs:api). It's good to go!

@michaldudak michaldudak merged commit 6ad56c2 into mui:master May 11, 2022
@emlai emlai deleted the fix/32548 branch May 11, 2022 12:21
@oliviertassinari oliviertassinari added the new feature New feature or request label May 11, 2022
@oliviertassinari
Copy link
Member

oliviertassinari commented May 11, 2022

In #32548 I think that we could have closed it with advising to create a new wrapper component because the use case sounds quite advanced (and for future similar cases).

Now, providing the ownerState in the renderXX props, as we do for the component slots makes a lot of sense for API consistency and predictability. So overall, interesting new API. Hence, I think that we could redact "Fixes #32548" to "Opportunity surfaced in #32548" in the PR description.

@michaldudak
Copy link
Member

To make it even more consistent with other components, I would propose replacing renderTags with components.TagList, or similar. It would be a breaking change, though, so adding an ownerState to the current function is a good compromise IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: autocomplete This is the name of the generic UI component, not the React module! new feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants