Skip to content

Instantly share code, notes, and snippets.

@jhonsore
Created December 20, 2021 11:28
Show Gist options
  • Save jhonsore/8db5a399f3e6268db53ff6491f86b042 to your computer and use it in GitHub Desktop.
Save jhonsore/8db5a399f3e6268db53ff6491f86b042 to your computer and use it in GitHub Desktop.
import React from "react";
import ReactDOM from "react-dom";
import styled from "styled-components";
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;
}
}
`;
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment