样式化组件:使用 css 根据值添加颜色
styled components : using css to add colour based on values
我有一个如下所示的下拉组件
源代码:
const DropdownSelectPriority = props => {
const { multiChoiceArray } = props
return (
<ButtonContainer>
{multiChoiceArray.map(questionChoice => {
return (
<div key={questionChoice.id}>
<p>{questionChoice.text}</p>
<SimpleSelect
placeholder="Select a priority"
onValueChange={value => console.log(value)}
>
{questionChoice.options.map(options => {
return <option value={options.label}>{options.value}</option>
})}
</SimpleSelect>
</div>
)
})}
</ButtonContainer>
)
}
我想根据选择的值设置 div 的样式,我该如何实现?例如,如果水质管理是最优先的,我想添加一些样式等
只需设置具有背景 属性 的通用 Option
样式:
const Select = styled.select`
background: ${({ bg }) => bg};
`;
const Option = styled.option`
background: ${({ bg }) => bg};
`;
const options = ['red', 'green', 'blue'];
const App = () => {
const [selected, setSelected] = useState('red');
return (
<Select
value={selected}
onChange={e => setSelected(e.target.value)}
bg={selected}
>
{options.map(option => (
<Option key={option} value={option} bg={option}>
{option}
</Option>
))}
</Select>
);
};
我有一个如下所示的下拉组件
源代码:
const DropdownSelectPriority = props => {
const { multiChoiceArray } = props
return (
<ButtonContainer>
{multiChoiceArray.map(questionChoice => {
return (
<div key={questionChoice.id}>
<p>{questionChoice.text}</p>
<SimpleSelect
placeholder="Select a priority"
onValueChange={value => console.log(value)}
>
{questionChoice.options.map(options => {
return <option value={options.label}>{options.value}</option>
})}
</SimpleSelect>
</div>
)
})}
</ButtonContainer>
)
}
我想根据选择的值设置 div 的样式,我该如何实现?例如,如果水质管理是最优先的,我想添加一些样式等
只需设置具有背景 属性 的通用 Option
样式:
const Select = styled.select`
background: ${({ bg }) => bg};
`;
const Option = styled.option`
background: ${({ bg }) => bg};
`;
const options = ['red', 'green', 'blue'];
const App = () => {
const [selected, setSelected] = useState('red');
return (
<Select
value={selected}
onChange={e => setSelected(e.target.value)}
bg={selected}
>
{options.map(option => (
<Option key={option} value={option} bg={option}>
{option}
</Option>
))}
</Select>
);
};