I am trying to change the "No options" message in react-select
? I was able to do this with the following code
<AsyncSelect
styles={customStyles}
components={{ DropdownIndicator: () => null, IndicatorSeparator: () => null, NoOptionsMessage: () => "test" }}
className="form-control firm-search"
cacheOptions
defaultOptions
value={selectedValue}
getOptionLabel={e => e.name}
getOptionValue={e => e.path}
loadOptions={loadOptions}
onInputChange={handleInputChange}
onChange={handleChange}
placeholder='test ...'
/>
The problem is that the text is not styled anymore. Is it possible to change the text but keep the default style? If not, is it then possible to apply CSS to the new text?
To set selected value of a select drop down to null with React, we can set the state we use as the value of the value prop of the select drop down to null . to set onChange to a function that calls setSelected to e. target. value which is the selected value if it's truthy.
To style react-select drop down options, we can create an object with the styles we want to apply and set that as the value of the styles prop. We create the colourStyles object with the control method that return an object with the styles. And we have the option method that returns the styles for the options.
To fetch the selected value from the select element, you can use the onChange event handler prop. Just like the input or textarea elements, you can use the onChange event handler to get the value from the event object. Now, make this select input element controlled by using the state to pass the value.
There are two ways to style the custom component. You can pass a style object to styles
prop or create a new custom component.
Following code shows both approaches.
import React from "react";
import "./style.css";
import Select, { components } from "react-select";
const msgStyles = {
background: "skyblue",
color: "white"
};
const NoOptionsMessage = props => {
return (
<components.NoOptionsMessage {...props}>
<span className="custom-css-class">Text</span>
</components.NoOptionsMessage>
);
};
const CustomNoOptionsMessage = () => {
return (
<Select
isClearable
components={{ NoOptionsMessage }}
styles={{ noOptionsMessage: base => ({ ...base, ...msgStyles }) }}
isSearchable
name="color"
options={[]}
/>
);
};
export default function App() {
return (
<div>
<CustomNoOptionsMessage />
</div>
);
}
Here you can find the working code:https://stackblitz.com/edit/react-lejn8c?file=src/App.js
Another way to do this:
<AsyncSelect
isClearable
defaultOptions
cacheOptions
defaultOptions
value={selectedValue}
getOptionLabel={e => e.name}
getOptionValue={e => e.path}
loadOptions={loadOptions}
onInputChange={handleInputChange}
onChange={handleChange}
noOptionsMessage={({inputValue}) => !inputValue ? noOptionsText : "No results found"}
/>
There is a documented way of doing this: https://react-select.com/components
You can define your own NoOptionMessage
component:
import { components } from 'react-select';
const NoOptionsMessage = props => {
return (
<components.NoOptionsMessage {...props}>
Test…
</components.NoOptionsMessage>
);
};
In the components
module you can access every native implementation so you don't need to reinvent the wheel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With