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] Expose autocomplete props in renderTags/renderOption function #24415

Closed
ajpopplestone opened this issue Jan 14, 2021 · 4 comments
Labels
component: autocomplete This is the name of the generic UI component, not the React module! discussion

Comments

@ajpopplestone
Copy link

To add more flexibly to the renderTags/renderOptions function in the Autocomplete component, expose the autocomplete props in the render function parameters.

Examples 🌈

<Autocomplete renderTags={(value, getTagProps, autocompleteProps) => { const { someCustomProp } = autocompleteProps // do something }} someCustomProp={[1, 2, 4]} .... >

Motivation 🔦

Currently I'm trying to conditionally render the styling of a tag dependent on if the value is present in an array held in state.
This array is external to the Autocomplete component so there is no way I can see to access this in the renderTags function.
This would also allow for custom clickHandlers to be passed to the tag components.
The library react-select has this functionality.
Let me know your thoughts and if you have a better way to accomplish what I'm trying to do, thanks!

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 14, 2021

@ne4rly0ne I'm not following, what stop you to provide the state to the renderTags method?

The library react-select has this functionality.

Do you have more details?

@oliviertassinari oliviertassinari added the component: autocomplete This is the name of the generic UI component, not the React module! label Jan 14, 2021
@ajpopplestone
Copy link
Author

@ne4rly0ne I'm not following, what stop you to provide the state to the renderTags method?

On review, it is possible to refactor the code so state is provided to the renderTags, however, I'm trying to recreate a react-select component and the api for that is more straight forward. Since the documentation for autocomplete is 'meant to be an improved version of the "react-select"', I thought I would suggest this.

In custom render functions in react-select all props passed to the main component are available, in addition to the data for that component. A codesandbox example is below.
https://codesandbox.io/s/fe1ec?module=/example

I believe having all autocomplete props available in the arguments passed to the render functions would give flexibly when creating components.

My apologies if I have not explained this well.

@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 15, 2021

@ne4rly0ne Ok, great, thanks for the extra details. So it's related to the composition API and the customization of nested slots. We cover some of the problems in #21453. The plan is to have a componentsProps API to allow developers to inject custom props to each customization slot. We didn't make it generic as react-select for isolation purposes. It should make it easier to maintain the code.

In our case, we don't expose a component, but a render function, so it's simpler. You can do the following:

const renderTags = (props) => (tagValue, getTagProps) => <div />;

<Autocomplete
  renderTags={renderTags({
    someCustomProp: [1, 2, 4],
  })}
>

Or even simpler, have the props in the global scope.

@ajpopplestone
Copy link
Author

Thanks @oliviertassinari clarifies nicely.

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! discussion
Projects
None yet
Development

No branches or pull requests

2 participants