-->

react-select changing drop down indicator icon to

2019-08-26 18:01发布

问题:

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.

回答1:

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.