Checkbox.tsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import React, { FC } from "react";
  2. import styled, { css } from "styled-components";
  3. import { ifProp } from "styled-tools";
  4. import { Flex, BoxProps } from "reflexbox/styled-components";
  5. import { Span } from "./Text";
  6. interface InputProps {
  7. checked: boolean;
  8. id?: string;
  9. name: string;
  10. onChange: any;
  11. }
  12. const Input = styled(Flex).attrs({
  13. as: "input",
  14. type: "checkbox",
  15. m: 0,
  16. p: 0,
  17. width: 0,
  18. height: 0,
  19. opacity: 0
  20. })<InputProps>`
  21. position: relative;
  22. opacity: 0;
  23. `;
  24. const Box = styled(Flex).attrs({
  25. alignItems: "center",
  26. justifyContent: "center"
  27. })<{ checked: boolean }>`
  28. position: relative;
  29. transition: color 0.3s ease-out;
  30. border-radius: 4px;
  31. background-color: white;
  32. box-shadow: 0 2px 4px rgba(50, 50, 50, 0.2);
  33. cursor: pointer;
  34. input:focus + & {
  35. outline: 3px solid rgba(65, 164, 245, 0.5);
  36. }
  37. ${ifProp(
  38. "checked",
  39. css`
  40. box-shadow: 0 3px 5px rgba(50, 50, 50, 0.4);
  41. :after {
  42. content: "";
  43. position: absolute;
  44. width: 80%;
  45. height: 80%;
  46. display: block;
  47. border-radius: 2px;
  48. background-color: #9575cd;
  49. box-shadow: 0 2px 4px rgba(50, 50, 50, 0.2);
  50. cursor: pointer;
  51. }
  52. `
  53. )}
  54. `;
  55. interface Props extends InputProps, BoxProps {
  56. label: string;
  57. }
  58. const Checkbox: FC<Props> = ({
  59. checked,
  60. height,
  61. id,
  62. label,
  63. name,
  64. width,
  65. onChange,
  66. ...rest
  67. }) => {
  68. return (
  69. <Flex
  70. flex="0 0 auto"
  71. as="label"
  72. alignItems="center"
  73. style={{ cursor: "pointer" }}
  74. {...(rest as any)}
  75. >
  76. <Input onChange={onChange} name={name} id={id} checked={checked} />
  77. <Box checked={checked} width={width} height={height} />
  78. <Span ml={12} color="#555">
  79. {label}
  80. </Span>
  81. </Flex>
  82. );
  83. };
  84. Checkbox.defaultProps = {
  85. width: [18],
  86. height: [18]
  87. };
  88. export default Checkbox;