Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Checkbox.tsx 4.6KB

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