Advertisement
Soty89

checkbox

Nov 18th, 2021
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import styled from 'styled-components';
  2. import { useState } from 'react';
  3. import { CheckIcon } from '../icons';
  4.  
  5. const Checkbox = ({ label, defaultValue = false, className, isDisabled }) => {
  6.   const [checked, setChecked] = useState(defaultValue);
  7.  
  8.   const onChange = () => {
  9.     setChecked(!checked);
  10.   };
  11.  
  12.   return (
  13.     <Container className={className} isDisabled={isDisabled}>
  14.       {label}
  15.       <HiddenInput
  16.         className="visually-hidden"
  17.         checked={checked}
  18.         onChange={onChange}
  19.         disabled={isDisabled}
  20.       />
  21.       <StyledCheckBox checked={checked}>
  22.         <CheckIcon />
  23.       </StyledCheckBox>
  24.     </Container>
  25.   );
  26. };
  27.  
  28. const Container = styled.label`
  29.   display: flex;
  30.   justify-content: space-between;
  31.   align-items: center;
  32.   white-space: nowrap;
  33.   cursor: ${({ isDisabled }) => (isDisabled ? 'default' : 'pointer')};
  34. `;
  35.  
  36. const HiddenInput = styled.input.attrs({ type: 'checkbox' })``;
  37.  
  38. const StyledCheckBox = styled.div`
  39.   width: 18px;
  40.   height: 18px;
  41.   border-radius: 4px;
  42.  
  43.   display: flex;
  44.   align-items: center;
  45.   justify-content: center;
  46.  
  47.   transition: background-color 0.3s ease-in-out;
  48.   background-color: ${({ checked }) =>
  49.     checked ? 'var(--primary-pink)' : 'var(--primary-white)'};
  50. `;
  51.  
  52. export default Checkbox;
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement