Files
web2.0-frontend/src/components/Checkbox/Checkbox.tsx
T
Aarni Halinen 103c48c790 Custom checkbox
2020-07-22 22:40:23 +03:00

87 lines
1.7 KiB
TypeScript

import React from "react";
import styled from "styled-components";
const Container = styled.label`
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
/* On mouse-over, add a grey background color */
&:hover input ~ .checkbox {
background-color: #ccc;
}
/* When the checkbox is checked, add a blue background */
& input:checked ~ .checkbox {
background-color: #2196F3;
}
/* Style the checkmark/indicator */
& .checkbox:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
/* Show the checkmark when checked */
& input:checked ~ .checkbox:after {
display: block;
}
`;
/* Hide the browser's default checkbox */
const DefaultBox = styled.input`
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
`;
/* Create a custom checkbox */
const CustomBox = styled.span`
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
/* Create the checkmark/indicator (hidden when not checked) */
&:after {
content: "";
position: absolute;
display: none;
}
`;
type CheckboxProps = Omit<
React.InputHTMLAttributes<HTMLInputElement>,
"type"
>;
const Checkbox: React.FC<CheckboxProps> = ({children, ...props}) => (
<Container>
{children}
<DefaultBox
type="checkbox"
{...props}
/>
<CustomBox className="checkbox" />
</Container>
)
export default Checkbox;