【发布时间】:2021-11-09 11:29:32
【问题描述】:
我正在使用https://react-select.com/home#getting-started,我正在尝试创建尽可能接近此的内容:
我编码的 select 的原始样式(没有 select 库中的 customStyles 函数)给了我这样的结果:
但只要我开始添加styles(选择库中的customStyles函数),如下所示:
import { useEffect, useState } from 'react';
// styled components
import styled from 'styled-components';
// icons
import { IoIosArrowDown } from 'react-icons/io';
// select input
import Select from 'react-select';
const ContextItem = ({ text, options }) => {
const [treeNodeNames, setTreeNodeNames] = useState();
// THIS IS FROM THE LIBRARY
const customStyles = {
option: (provided, state) => ({
...provided,
// borderBottom: '1px dotted pink',
color: state.isSelected ? 'white' : 'black',
}),
control: () => ({
// none of react-select's styles are passed to <Control />
width: 200,
}),
singleValue: (provided, state) => {
const opacity = state.isDisabled ? 0.5 : 1;
const transition = 'opacity 300ms';
return { ...provided, opacity, transition };
},
};
useEffect(() => {
if (options) {
const treeNodes = options.data.findTreeNodesByTree.map((element) => {
return {value: element.name, label: element.name };
});
setTreeNodeNames(treeNodes);
}
}, [options]);
return (
<ContextContainer>
<ContextTitle>
<p> {text}</p>
</ContextTitle>
{treeNodeNames && (
<SearchInput>
<Select options={treeNodeNames} styles={customStyles} />
</SearchInput>
)}
</ContextContainer>
);
};
// STYLES
const ContextContainer = styled.div`
display: flex;
align-items: center;
justify-content: space-between;
`;
const ContextTitle = styled.div`
display: flex;
width: 30%;
align-items: center;
justify-content: space-between;
`;
const SearchInput = styled.div`
width: 60%;
padding: 5px;
outline: black;
`;
export default ContextItem;
我得到这样的东西:
所以问题是:如何使用 select 中的 customStyles 在不破坏所有内容的情况下达到第一张照片中显示的预期结果?有没有办法将原始反应选择的样式传递给并相应地修改它们。最后我只需要:
- 选择选项周围没有边框
- 选择选项移到右侧
- 同色
- 但选择或悬停时背景不同
- 单击所选输入字段时,边框颜色没有变化
我希望有道理,如果问题不清楚,请告诉我:) 感谢任何形式的帮助和线索。
【问题讨论】: