Checkbox.js 2.1 KB

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