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

[Bug] loadOptions is not passed to custom components #4487

Open
alexandernst opened this issue Mar 11, 2021 · 5 comments
Open

[Bug] loadOptions is not passed to custom components #4487

alexandernst opened this issue Mar 11, 2021 · 5 comments

Comments

@alexandernst
Copy link

I'm customizing the components of react-select and I have a use case in which I'd like to know if the select element has been provided with a loadOptions function.

It should be possible to access the props of the customized component, and then the selectProps property, which should then give me all the props that have been passed to the react select. The loadOptions is missing in the list of properties that selectProps is returning.

Here is a repro demo: https://codesandbox.io/s/react-select-v3-sandbox-forked-yns2r?file=/example.js (check line 13)

@alexandernst
Copy link
Author

I'm debugging this further and it seems that more props are missing. This might be caused because of missing spread somewhere?

@ebonow
Copy link
Collaborator

ebonow commented Mar 11, 2021

Confirmed. It appears that this is being omitted for some reason when passing the HOC AsyncSelect props to the Select.

render() {
const { loadOptions, isLoading: isLoadingProp, ...props } = this.props;
const {
defaultOptions,
inputValue,
isLoading,
loadedInputValue,
loadedOptions,
passEmptyOptions,
} = this.state;
const options = passEmptyOptions
? []
: inputValue && loadedInputValue
? loadedOptions
: defaultOptions || [];
return (
<SelectComponent
{...props}
ref={ref => {
this.select = ref;
}}
options={options}
isLoading={isLoading || isLoadingProp}
onInputChange={this.handleInputChange}
/>
);
}

A work around for now potentially could be to pass in another prop that references your loadOptions function, but I am also curious as to why this is.

@ebonow
Copy link
Collaborator

ebonow commented Mar 11, 2021

@alexandernst I was able to get a bit more information about the history of this. Apparently the Async component was created prior to the SelectProps being passed into the different components.

Would you mind sharing what the use-case is for accessing the loadOptions so we can better understand how you are using it?

@alexandernst
Copy link
Author

alexandernst commented Mar 11, 2021

@ebonow Sure!

I have a custom menuList component with a (more or less) complex logic.

When the user opens the menu, a few elements should be made visible. Depending if the select widget was passed a loadOptions function, a custom message should be appended to the bottom of the menu list elements. Then that function should be called in order to retrieve some more elements and get them appended to the list.

I was able to workaround the problem by doing an ugly hack which involves nesting the menuList component inside the definition of the select component, but being able to properly access the props.loadOptions prop would allow me to clean up and split my code.

@tubs-coder
Copy link

Sorry to awaken an old thread - just to mention i've also hit this issue.

I am looking to change the behaviour of the MultiValueRemove button based on the options received from the loadOptions function.

Use case: Some values in my multi-select are read only and some are not. I'm looking to read loadOptions to determine whether I should render the MultiValueRemove component for a particular item - if it exists in loadOptions then it can be added/removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants