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

為{幸葍}努か 提交于 2019-12-11 18:35:48

问题


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.



来源:https://stackoverflow.com/questions/54586184/react-select-changing-drop-down-indicator-icon-to-font-awesome-icon-is-not-worki

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!