You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Checkbox.tsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import React from 'react';
  2. import { makeStyles } from 'tss-react/mui';
  3. import { isMobileBrowser } from '../../../environment/utils';
  4. import Icon from '../../../icons/components/Icon';
  5. import { IconCheck } from '../../../icons/svg';
  6. import { withPixelLineHeight } from '../../../styles/functions.web';
  7. interface ICheckboxProps {
  8. /**
  9. * Whether the input is checked or not.
  10. */
  11. checked: boolean;
  12. /**
  13. * Class name for additional styles.
  14. */
  15. className?: string;
  16. /**
  17. * Whether the input is disabled or not.
  18. */
  19. disabled?: boolean;
  20. /**
  21. * The label of the input.
  22. */
  23. label: string;
  24. /**
  25. * The name of the input.
  26. */
  27. name?: string;
  28. /**
  29. * Change callback.
  30. */
  31. onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
  32. }
  33. const useStyles = makeStyles()(theme => {
  34. return {
  35. formControl: {
  36. ...withPixelLineHeight(theme.typography.bodyLongRegular),
  37. color: theme.palette.text01,
  38. display: 'inline-flex',
  39. alignItems: 'center',
  40. '&.is-mobile': {
  41. ...withPixelLineHeight(theme.typography.bodyLongRegularLarge)
  42. }
  43. },
  44. activeArea: {
  45. display: 'grid',
  46. placeContent: 'center',
  47. width: '24px',
  48. height: '24px',
  49. backgroundColor: 'transparent',
  50. marginRight: '15px',
  51. position: 'relative',
  52. cursor: 'pointer',
  53. '& input[type="checkbox"]': {
  54. appearance: 'none',
  55. backgroundColor: 'transparent',
  56. margin: 0,
  57. font: 'inherit',
  58. color: theme.palette.icon03,
  59. width: '18px',
  60. height: '18px',
  61. border: `2px solid ${theme.palette.icon03}`,
  62. borderRadius: '3px',
  63. cursor: 'pointer',
  64. display: 'grid',
  65. placeContent: 'center',
  66. '&::before': {
  67. content: 'url("")',
  68. width: '18px',
  69. height: '18px',
  70. opacity: 0,
  71. backgroundColor: theme.palette.action01,
  72. display: 'flex',
  73. alignItems: 'center',
  74. justifyContent: 'center',
  75. border: 0,
  76. borderRadius: '3px',
  77. transition: '.2s'
  78. },
  79. '&:checked::before': {
  80. opacity: 1
  81. },
  82. '&:disabled': {
  83. backgroundColor: theme.palette.ui03,
  84. borderColor: theme.palette.ui04,
  85. '&::before': {
  86. backgroundColor: theme.palette.ui04
  87. }
  88. },
  89. '&:checked+.checkmark': {
  90. opacity: 1
  91. }
  92. },
  93. '& .checkmark': {
  94. position: 'absolute',
  95. left: '3px',
  96. top: '3px',
  97. opacity: 0,
  98. transition: '.2s'
  99. },
  100. '&.is-mobile': {
  101. width: '40px',
  102. height: '40px',
  103. '& input[type="checkbox"]': {
  104. width: '24px',
  105. height: '24px',
  106. '&::before': {
  107. width: '24px',
  108. height: '24px'
  109. }
  110. },
  111. '& .checkmark': {
  112. left: '11px',
  113. top: '10px'
  114. }
  115. }
  116. }
  117. };
  118. });
  119. const Checkbox = ({
  120. checked,
  121. className,
  122. disabled,
  123. label,
  124. name,
  125. onChange
  126. }: ICheckboxProps) => {
  127. const { classes: styles, cx, theme } = useStyles();
  128. const isMobile = isMobileBrowser();
  129. return (
  130. <div className = { cx(styles.formControl, isMobile && 'is-mobile', className) }>
  131. <label className = { cx(styles.activeArea, isMobile && 'is-mobile') }>
  132. <input
  133. checked = { checked }
  134. disabled = { disabled }
  135. name = { name }
  136. onChange = { onChange }
  137. type = 'checkbox' />
  138. <Icon
  139. className = 'checkmark'
  140. color = { disabled ? theme.palette.icon03 : theme.palette.icon01 }
  141. size = { 18 }
  142. src = { IconCheck } />
  143. </label>
  144. <label>{label}</label>
  145. </div>
  146. );
  147. };
  148. export default Checkbox;