gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 22:23:11 26 4
gpt4 key购买 nike

我正在尝试在 react 选择组件选项上添加很棒的图标和工具提示。下图是我试图实现的目标

enter image description here

代码

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>;
}
}

最佳答案

您需要添加如下所示的组件,仅为演示目的添加了一个选项,您可以按照自己的方式设计它。

甚至您可以使用可重用组件来生成标签,因为标签接受节点。

希望这能解决问题。

{
value: "page_visited",
label: (
<>
<span style={{ paddingRight: "5px" }}>Page Visited</span>
<FontAwesomeIcon icon="info" title={"Page Visited Option"} />
</>
)
},

整个代码

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 = [
{ value: "", label: "Search and select website activity" },
{
value: "page_visited",
label: (
<>
<span style={{ paddingRight: "5px" }}>Page Visited</span>
<FontAwesomeIcon icon="info" title={"Page Visited Option"} />
</>
)
},
{ 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>
);
}
}

const rootElement = document.getElementById("root");
ReactDOM.render(<Website />, rootElement);

工作中codepen

关于javascript - 如何在 react 选择组件上添加图标和工具提示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59154960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com