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

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