如何给react-select中的选项添加图标?

How to add an icon to the options in react-select?

尝试将图标添加到 react-select 中的选项。我从文件 england.svggermany.svg 导入了 svg 图标。我创建了 customSingleValue 并将其放入

<Select components={{ SingleValue: customSingleValue }} />

显示标签,但不显示图标。

此处演示:https://stackblitz.com/edit/react-q19sor

import Select from 'react-select'
import { ReactComponent as IconFlagEngland } from "./england.svg";
import { ReactComponent as IconFlagGermany } from "./germany.svg";

const options = [
  { value: 'England', label: 'England', icon: <IconFlagEngland/> },
  { value: 'Germany', label: 'Germany', icon: <IconFlagGermany/> }
]

const customSingleValue = ({ options }) => (
    <div className="input-select">
        <div className="input-select__single-value">
            { options.icon && <span className="input-select__icon">{ options.icon }</span> }
            <span>{ options.label }</span>
        </div>
    </div>
);

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: 'React'
    };
  }

  render() {
    return (
       <Select
            defaultValue={ options [0] }
            options={ options }
            /*styles={ selectCustomStyles }*/
            /*onChange={ changeSelectHandler }*/
            components={ {SingleValue: customSingleValue } }
        />
    );
  }
}

render(<App />, document.getElementById('root'));

我认为问题在于您实际上并未导入 SVG。如果您尝试在任何地方的代码中直接使用 <IconFlagGermany/>,它会严重崩溃并显示此消息:

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

它目前没有崩溃,因为我认为你的 customSingleValue 函数没有按照你的预期运行(没有调查它,但很确定它有问题)。

如果您希望能够以这种方式导入 SVG,则需要在 Webpack(或您选择的捆绑器)中设置适当的加载器。也许是这样的:https://www.npmjs.com/package/react-svg-loader

但是,另一种解决方案是将您的 SVG 正确地导出为组件,就像从您的代码派生的这个演示中一样:https://stackblitz.com/edit/react-5gvytm

我找到了解决方法。我的技术类似于@canda:

import React, { Component } from "react";
import { render } from "react-dom";
import "./style.css";
import Select, { components } from "react-select";

const options = [
  { value: "England", label: "England", icon: "england.svg" },
  { value: "Germany", label: "Germany", icon: "germany.svg" }
];

const { Option } = components;
const IconOption = props => (
  <Option {...props}>
    <img
      src={require('./' + props.data.icon)}
      style={{ width: 36 }}
      alt={props.data.label}
    />
    {props.data.label}
  </Option>
);

class App extends Component {
  constructor() {
    super();
    this.state = {
      name: "React"
    };
  }

  render() {
    return (
      <Select
        defaultValue={options[0]}
        options={options}
        components={{ Option: IconOption }}
      />
    );
  }
}

render(<App />, document.getElementById("root"));

万一有人想在react-select中使用多选图标,可以使用下面的代码:

const { MultiValue } = components;

const MultiValueOption = (props) => {
  return (
    <MultiValue {...props}>
      <img
        src={require("./" + props.data.icon)}
        style={{ width: 36 }}
        alt={props.data.label}
      />
      <span>{props.data.value}</span>
    </MultiValue>
  );
};

<Select
  options={options}
  components={{
    Option: IconOption,
    MultiValue: MultiValueOption,
  }}
  isMulti={true}
></Select>;