reactjs - 选择无线电输入时如何更改图标的颜色?
问题描述
我已经有几个小时的问题了,我创建了两个连接到无线电输入的按钮。
我使用样式化的组件,并且我希望当用户选择该选项时,我的复选图标可能会变为白色。
我正在考虑使用状态,但我不知道它是否是正确的解决方案。
import { React, useState } from "react";
import fire from '../firebase/firebase';
import styled from "styled-components";
import { FaCheckCircle } from 'react-icons/fa';
import '../../App.css';
export const BoxContainer = styled.div`
display: grid;
grid-template-rows: 100%;
grid-template-columns:1fr 1fr;
`;
export const Gauche = styled.div`
`;
export const Droite = styled.div`
`;
export const Descriptif = styled.div`
background-color: #F9F9FB;
margin: 50px;
border-radius: 10px;
height: 500px;
`;
export const Option = styled.div`
background-color: #8064F7;
margin: 50px;
border-radius: 10px;
height: 100px;
width:500px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
`;
export const Optionbutton = styled.input`
visibility: hidden;
`;
export const Select = styled.div`
margin-left: 30px;
${Optionbutton}:checked {
color:red !important;
}
`;
export const Intitule = styled.div`
text-align:left;
font-weight:bold;
color:white;
font-size: 20px;
`;
export const MiddleBox = styled.div`
margin-right: auto;
margin-left: 60px;
`;
export const Economie = styled.div`
background-color:#6D43E5;
border-radius:20px;
padding: 5px;
color:white;
`;
export const Prix = styled.div`
color:white;
font-weight:bold;
font-size:25px;
margin-right: 20px;
`;
export const OptionLabel = styled.label`
cursor: pointer;
`;
export function Shop(props) {
const [radio,setRadio] = useState("option1");
console.log({radio});
return (
<BoxContainer>
<Gauche>
<Descriptif>Bonjour</Descriptif>
</Gauche>
<Droite><form>
<OptionLabel><Optionbutton thechecked={props.isSelected} type="radio" value="option1" checked={radio === "option1"} onChange={(e) =>{ setRadio(e.target.value)}}></Optionbutton>
<Option>
<Select ><FaCheckCircle /></Select>
<MiddleBox>
<Intitule>1 Ticket vidéo</Intitule>
</MiddleBox>
<Prix>29.99 €</Prix>
</Option>
</OptionLabel>
<OptionLabel><Optionbutton type="radio" value="option2" checked={radio === "option2"} onChange={(e) =>{ setRadio(e.target.value)}}></Optionbutton>
<Option>
<Select><FaCheckCircle/></Select>
<MiddleBox>
<Intitule>1 Ticket vidéo</Intitule>
</MiddleBox>
<Prix>29.99 €</Prix>
</Option>
</OptionLabel></form>
</Droite>
</BoxContainer>
)
}
export default Shop;
太感谢了。
解决方案
在 styled-components 中,您可以将 props 传递给创建的组件,因此您可以将 true 值传递给 Select 组件以更改颜色。要在 styled-component 中使用 prop,请尝试以下操作:
export const Select = styled.div`
margin-left: 30px;
color: ${({isSelected}) => isSelected ? "red" : "black"};
`;
在 JSX 中你应该这样做:
<Select isSelected={radio === "option1"}>
<FaCheckCircle />
</Select>
推荐阅读
- python - 如何保持形状的位置参数以使其最左侧保持在给定参数“左”,而不是中点?
- mysql - 如何将查询结果存储在另一个表的特定列中
- python - 使用装饰器将字典插入 python 类
- arrays - 如何在 ReactJs 中获取 JSON url 中的类别
- visual-studio - 如何从 Windows VS2017 .NET Core 应用程序为 linux-arm 发布?
- c# - 时间:2019-01-10 标签:c#bookmark.zip文件嵌入对象
- json - Json 对象选择
- javascript - Highcharts:类别定位以匹配日期时间列
- laravel-5 - 如果多于一个,如何搜索收藏?
- php - 如何使用 php 文件作为数组,而不是 Laravel 5 中的类?