如何使用 Hooks 切换 class 与 React radio 组件?
How do I toggle class with React radio component using Hooks?
我已经将一个无线电 select 组件与 React Hooks 放在一起,可以在两个有效的选项之间切换。当单选按钮被 select 编辑时,如何将 class 添加到大纲框中?我希望背景从白色变为灰色。我正在使用带样式的组件并尝试仅使用 CSS 来执行此操作但没有成功。我怎样才能使用钩子来完成这个?此处的工作示例:https://codesandbox.io/embed/react-styled-components-radio-button-qpxul?fontsize=14
const { useState } = React;
const App = () => {
const [select, setSelect] = useState("optionA");
const handleSelectChange = event => {
const value = event.target.value;
setSelect(value);
};
return (
<Wrapper>
<Item>
<RadioButton
type="radio"
name="radio"
value="optionA"
checked={select === "optionA"}
onChange={event => handleSelectChange(event)}
/>
<RadioButtonLabel />
<div>Choose Pickup</div>
</Item>
<Item>
<RadioButton
type="radio"
name="radio"
value="optionB"
checked={select === "optionB"}
onChange={event => handleSelectChange(event)}
/>
<RadioButtonLabel />
<div>Choose Delivery</div>
</Item>
</Wrapper>
);
};
const Wrapper = styled.div`
height: auto;
width: 100%;
padding: 0px 16px 24px 16px;
box-sizing: border-box;
`;
const Item = styled.div`
display: flex;
align-items: center;
height: 48px;
position: relative;
border: 1px solid #ccc;
box-sizing: border-box;
border-radius: 2px;
margin-bottom: 10px;
`;
const RadioButtonLabel = styled.label`
position: absolute;
top: 25%;
left: 4px;
width: 24px;
height: 24px;
border-radius: 50%;
background: white;
border: 1px solid #ccc;
`;
const RadioButton = styled.input`
opacity: 0;
z-index: 1;
cursor: pointer;
width: 25px;
height: 25px;
margin-right: 10px;
&:hover ~ ${RadioButtonLabel} {
background: #ccc;
&::after {
content: "\f005";
font-family: "FontAwesome";
display: block;
color: white;
width: 12px;
height: 12px;
margin: 4px;
}
}
&:checked + ${Item} {
background: yellowgreen;
border: 2px solid yellowgreen;
}
&:checked + ${RadioButtonLabel} {
background: yellowgreen;
border: 1px solid yellowgreen;
&::after {
content: "\f005";
font-family: "FontAwesome";
display: block;
color: white;
width: 12px;
height: 12px;
margin: 4px;
}
}
`;
在 CSS 中没有父选择器,因此您不能从 checkbox
.
中定位父元素
但是你可以根据收音机的选择状态添加一个class
<Item className={select === "optionA" ? 'active-radio' : null}>
或者如果您想通过样式化的组件来实现,您可以使用
<Item active={select === "optionA"}>
结合
const Item = styled.div`
display: flex;
align-items: center;
height: 48px;
position: relative;
border: 1px solid #ccc;
box-sizing: border-box;
border-radius: 2px;
margin-bottom: 10px;
${props => props.active && (`
box-shadow: 0 0 10px -4px black;
`)}
`;
演示在 https://codesandbox.io/s/react-styled-components-radio-button-f5zpe
我已经将一个无线电 select 组件与 React Hooks 放在一起,可以在两个有效的选项之间切换。当单选按钮被 select 编辑时,如何将 class 添加到大纲框中?我希望背景从白色变为灰色。我正在使用带样式的组件并尝试仅使用 CSS 来执行此操作但没有成功。我怎样才能使用钩子来完成这个?此处的工作示例:https://codesandbox.io/embed/react-styled-components-radio-button-qpxul?fontsize=14
const { useState } = React;
const App = () => {
const [select, setSelect] = useState("optionA");
const handleSelectChange = event => {
const value = event.target.value;
setSelect(value);
};
return (
<Wrapper>
<Item>
<RadioButton
type="radio"
name="radio"
value="optionA"
checked={select === "optionA"}
onChange={event => handleSelectChange(event)}
/>
<RadioButtonLabel />
<div>Choose Pickup</div>
</Item>
<Item>
<RadioButton
type="radio"
name="radio"
value="optionB"
checked={select === "optionB"}
onChange={event => handleSelectChange(event)}
/>
<RadioButtonLabel />
<div>Choose Delivery</div>
</Item>
</Wrapper>
);
};
const Wrapper = styled.div`
height: auto;
width: 100%;
padding: 0px 16px 24px 16px;
box-sizing: border-box;
`;
const Item = styled.div`
display: flex;
align-items: center;
height: 48px;
position: relative;
border: 1px solid #ccc;
box-sizing: border-box;
border-radius: 2px;
margin-bottom: 10px;
`;
const RadioButtonLabel = styled.label`
position: absolute;
top: 25%;
left: 4px;
width: 24px;
height: 24px;
border-radius: 50%;
background: white;
border: 1px solid #ccc;
`;
const RadioButton = styled.input`
opacity: 0;
z-index: 1;
cursor: pointer;
width: 25px;
height: 25px;
margin-right: 10px;
&:hover ~ ${RadioButtonLabel} {
background: #ccc;
&::after {
content: "\f005";
font-family: "FontAwesome";
display: block;
color: white;
width: 12px;
height: 12px;
margin: 4px;
}
}
&:checked + ${Item} {
background: yellowgreen;
border: 2px solid yellowgreen;
}
&:checked + ${RadioButtonLabel} {
background: yellowgreen;
border: 1px solid yellowgreen;
&::after {
content: "\f005";
font-family: "FontAwesome";
display: block;
color: white;
width: 12px;
height: 12px;
margin: 4px;
}
}
`;
在 CSS 中没有父选择器,因此您不能从 checkbox
.
但是你可以根据收音机的选择状态添加一个class
<Item className={select === "optionA" ? 'active-radio' : null}>
或者如果您想通过样式化的组件来实现,您可以使用
<Item active={select === "optionA"}>
结合
const Item = styled.div`
display: flex;
align-items: center;
height: 48px;
position: relative;
border: 1px solid #ccc;
box-sizing: border-box;
border-radius: 2px;
margin-bottom: 10px;
${props => props.active && (`
box-shadow: 0 0 10px -4px black;
`)}
`;
演示在 https://codesandbox.io/s/react-styled-components-radio-button-f5zpe