Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly use NoOptionsMessage in react-select to change the "no options" text

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?

like image 417
laieman Avatar asked Mar 12 '21 09:03

laieman


People also ask

How do I change the Select option value in ReactJS?

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.

How do you style select options in react?

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.

How do you get selected options in react select?

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.


3 Answers

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

like image 186
Murli Prajapati Avatar answered Oct 10 '22 08:10

Murli Prajapati


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"} 
/>
like image 24
Vimal Saifudin Avatar answered Oct 10 '22 09:10

Vimal Saifudin


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.

like image 1
keul Avatar answered Oct 10 '22 08:10

keul