Checkbox.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import styled, { css } from 'styled-components';
  4. const Wrapper = styled.div`
  5. display: flex;
  6. justify-content: flex-start;
  7. align-items: center;
  8. margin: 24px 16px 24px;
  9. :first-child {
  10. margin-left: 0;
  11. }
  12. :last-child {
  13. margin-right: 0;
  14. }
  15. `;
  16. const Box = styled.span`
  17. position: relative;
  18. display: flex;
  19. align-items: center;
  20. font-weight: normal;
  21. color: #666;
  22. transition: color 0.3s ease-out;
  23. cursor: pointer;
  24. :hover {
  25. color: black;
  26. }
  27. :before {
  28. content: '';
  29. display: block;
  30. width: 18px;
  31. height: 18px;
  32. margin-right: 10px;
  33. border-radius: 4px;
  34. background-color: white;
  35. box-shadow: 0 2px 4px rgba(50, 50, 50, 0.2);
  36. cursor: pointer;
  37. @media only screen and (max-width: 768px) {
  38. width: 14px;
  39. height: 14px;
  40. margin-right: 8px;
  41. }
  42. }
  43. ${({ checked }) =>
  44. checked &&
  45. css`
  46. :before {
  47. box-shadow: 0 3px 5px rgba(50, 50, 50, 0.4);
  48. }
  49. :after {
  50. content: '';
  51. position: absolute;
  52. left: 2px;
  53. top: 4px;
  54. width: 14px;
  55. height: 14px;
  56. display: block;
  57. margin-right: 10px;
  58. border-radius: 2px;
  59. background-color: #9575cd;
  60. box-shadow: 0 2px 4px rgba(50, 50, 50, 0.2);
  61. cursor: pointer;
  62. @media only screen and (max-width: 768px) {
  63. left: 2px;
  64. top: 5px;
  65. width: 10px;
  66. height: 10px;
  67. }
  68. }
  69. `};
  70. `;
  71. const Checkbox = ({ checked, label, id, onClick }) => (
  72. <Wrapper>
  73. <Box checked={checked} id={id} onClick={onClick}>
  74. {label}
  75. </Box>
  76. </Wrapper>
  77. );
  78. Checkbox.propTypes = {
  79. checked: PropTypes.bool,
  80. label: PropTypes.string.isRequired,
  81. id: PropTypes.string.isRequired,
  82. onClick: PropTypes.func,
  83. };
  84. Checkbox.defaultProps = {
  85. checked: false,
  86. onClick: f => f,
  87. };
  88. export default Checkbox;