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