如何在 react-select 组件上添加图标和工具提示?

我正在尝试在 react-select 组件选项上添加 fontawesome 图标和工具提示。下图我正在努力实现

http://img4.mukewang.com/61e9555400017e7c06610306.jpg

代码


import React, { Component } from 'react';

import Select from 'react-select';


const websiteFilterFiedsOptions = [

    {value: '', label: 'Search and select website activity'},

    {value: 'page_visited', label: 'Page Visited'},

    {value: 'form_submitted', label: 'Form Submitted'}

]


export default class Website extends Component { 


    constructor(props) {

        super(props);

        this.state = {

            selectedFilter: ''

        }

    }


    filterSelectedData = function(data) {


    }


    render() {

        return <div className="form-group">

                    <h6 className="website_filter"><b>Filters</b></h6>

                    <div className="location-search field-width filed_width_custom">

                        <Select

                            value={this.state.selectedFilter}

                            onChange={(e) => this.filterSelectedData(e)}

                            options={ websiteFilterFiedsOptions }

                            placeholder="Search and select website activity"

                        />

                    </div>

                </div>;

    }

}


潇潇雨雨
浏览 339回答 3
3回答

临摹微笑

您需要添加组件,如下所示,仅出于演示目的添加一个选项,您可以按照自己的方式设置样式。即使您可以使用可重用的组件来生成标签,因为标签接受一个节点。我希望这能解决问题。{&nbsp; &nbsp; value: "page_visited",&nbsp; &nbsp; label: (&nbsp; &nbsp; &nbsp; <>&nbsp; &nbsp; &nbsp; &nbsp; <span style={{ paddingRight: "5px" }}>Page Visited</span>&nbsp; &nbsp; &nbsp; &nbsp; <FontAwesomeIcon icon="info" title={"Page Visited Option"} />&nbsp; &nbsp; &nbsp; </>&nbsp; &nbsp; )&nbsp; },整个代码import React, { Component } from "react";import ReactDOM from "react-dom";import Select from "react-select";import { library } from "@fortawesome/fontawesome-svg-core";import { fab } from "@fortawesome/free-brands-svg-icons";import { faInfo, faCoffee } from "@fortawesome/free-solid-svg-icons";import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";library.add(fab, faInfo, faCoffee);const websiteFilterFiedsOptions = [&nbsp; { value: "", label: "Search and select website activity" },&nbsp; {&nbsp; &nbsp; value: "page_visited",&nbsp; &nbsp; label: (&nbsp; &nbsp; &nbsp; <>&nbsp; &nbsp; &nbsp; &nbsp; <span style={{ paddingRight: "5px" }}>Page Visited</span>&nbsp; &nbsp; &nbsp; &nbsp; <FontAwesomeIcon icon="info" title={"Page Visited Option"} />&nbsp; &nbsp; &nbsp; </>&nbsp; &nbsp; )&nbsp; },&nbsp; { value: "form_submitted", label: "Form Submitted" }];export default class Website extends Component {&nbsp; constructor(props) {&nbsp; &nbsp; super(props);&nbsp; &nbsp; this.state = {&nbsp; &nbsp; &nbsp; selectedFilter: ""&nbsp; &nbsp; };&nbsp; }&nbsp; filterSelectedData = function(data) {};&nbsp; render() {&nbsp; &nbsp; return (&nbsp; &nbsp; &nbsp; <div className="form-group">&nbsp; &nbsp; &nbsp; &nbsp; <h6 className="website_filter">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <b>Filters</b>&nbsp; &nbsp; &nbsp; &nbsp; </h6>&nbsp; &nbsp; &nbsp; &nbsp; <div className="location-search field-width filed_width_custom">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Select&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value={this.state.selectedFilter}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChange={e => this.filterSelectedData(e)}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options={websiteFilterFiedsOptions}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholder="Search and select website activity"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; );&nbsp; }}const rootElement = document.getElementById("root");ReactDOM.render(<Website />, rootElement);

RISEBY

我认为您可以通过使用图标值制作自己的组件和对象来尝试这种方式。https://github.com/JedWatson/react-select/issues/3480

摇曳的蔷薇

我认为您可以从此https://codesandbox.io/s/8ln4vnr1v2?from-embed获得参考
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript