Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-select changing drop down indicator icon to font-awesome icon is not working

I am trying to change the icon used for the react-select multi select indictor to a font-awesome icon, but it is not working. Any idea why?

import React from "react";
import Select, { components } from "react-select";
import { colourOptions } from "./docs/data";

const Placeholder = props => {
  return <components.Placeholder {...props} />;
};

const CaretDownIcon = () => {
  return <i className="fas fa-caret-down" />;
};

const DropdownIndicator = props => {
  return (
    <components.DropdownIndicator {...props}>
      <CaretDownIcon />
    </components.DropdownIndicator>
  );
};

export default () => (
  <Select
    closeMenuOnSelect={false}
    components={{ Placeholder, DropdownIndicator }}
    placeholder={"Choose"}
    styles={{
      placeholder: base => ({
        ...base,
        fontSize: "1em",
        color: colourOptions[2].color,
        fontWeight: 400
      })
    }}
    options={colourOptions}
  />
);

The item tag is shown in the DOM, but I do not see the icon.

DOM

like image 345
thehme Avatar asked Feb 08 '19 04:02

thehme


People also ask

How do you change the dropdown indicator in React-select?

DropdownIndicator {... props}> <CaretDownIcon /> </components. DropdownIndicator> ); }; export default () => ( <Select closeMenuOnSelect={false} components={{ Placeholder, DropdownIndicator }} placeholder={"Choose"} styles={{ placeholder: base => ({ ... base, fontSize: "1em", color: colourOptions[2].

How do I change the dropdown icon in React?

Conclusion. To change the dropdown icon in React Material UI select field, we can set the IconComponent prop to a function that returns the icon component we want to render.


1 Answers

I recommend you to check the documentation of Font Awesome for React.

To achieve the desired result I end up with the following code:

import React from "react";
import ReactDOM from "react-dom";
import Select, { components } from "react-select";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faCaretDown } from "@fortawesome/free-solid-svg-icons";
import { library } from "@fortawesome/fontawesome-svg-core";

library.add(faCaretDown);


const CaretDownIcon = () => {
  return <FontAwesomeIcon icon="caret-down" />;
};

const DropdownIndicator = props => {
  return (
    <components.DropdownIndicator {...props}>
      <CaretDownIcon />
    </components.DropdownIndicator>
  );
};

function App() {
  return (
    <div className="App">
      <Select
        closeMenuOnSelect={false}
        components={{ Placeholder, DropdownIndicator }}
        placeholder={"Choose"}
        options={colourOptions}
      />
    </div>
  );
}

Here a live example of what you want.

like image 73
Laura Avatar answered Oct 05 '22 19:10

Laura