Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

Checkbox.tsx 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. disabled: {
  45. cursor: 'not-allowed'
  46. },
  47. activeArea: {
  48. display: 'grid',
  49. placeContent: 'center',
  50. width: '24px',
  51. height: '24px',
  52. backgroundColor: 'transparent',
  53. marginRight: '15px',
  54. position: 'relative',
  55. cursor: 'pointer',
  56. '& input[type="checkbox"]': {
  57. appearance: 'none',
  58. backgroundColor: 'transparent',
  59. margin: '3px',
  60. font: 'inherit',
  61. color: theme.palette.icon03,
  62. width: '18px',
  63. height: '18px',
  64. border: `2px solid ${theme.palette.icon03}`,
  65. borderRadius: '3px',
  66. display: 'grid',
  67. placeContent: 'center',
  68. '&::before': {
  69. content: 'url("")',
  70. width: '18px',
  71. height: '18px',
  72. opacity: 0,
  73. backgroundColor: theme.palette.action01,
  74. display: 'flex',
  75. alignItems: 'center',
  76. justifyContent: 'center',
  77. border: 0,
  78. borderRadius: '3px',
  79. transition: '.2s'
  80. },
  81. '&:checked::before': {
  82. opacity: 1
  83. },
  84. '&:disabled': {
  85. backgroundColor: theme.palette.ui03,
  86. borderColor: theme.palette.ui04,
  87. '&::before': {
  88. backgroundColor: theme.palette.ui04
  89. }
  90. },
  91. '&:checked+.checkmark': {
  92. opacity: 1
  93. }
  94. },
  95. '& .checkmark': {
  96. position: 'absolute',
  97. left: '3px',
  98. top: '3px',
  99. opacity: 0,
  100. transition: '.2s'
  101. },
  102. '&.is-mobile': {
  103. width: '40px',
  104. height: '40px',
  105. '& input[type="checkbox"]': {
  106. width: '24px',
  107. height: '24px',
  108. '&::before': {
  109. width: '24px',
  110. height: '24px'
  111. }
  112. },
  113. '& .checkmark': {
  114. left: '11px',
  115. top: '10px'
  116. }
  117. }
  118. }
  119. };
  120. });
  121. const Checkbox = ({
  122. checked,
  123. className,
  124. disabled,
  125. label,
  126. name,
  127. onChange
  128. }: ICheckboxProps) => {
  129. const { classes: styles, cx, theme } = useStyles();
  130. const isMobile = isMobileBrowser();
  131. return (
  132. <label className = { cx(styles.formControl, isMobile && 'is-mobile', className) }>
  133. <div className = { cx(styles.activeArea, isMobile && 'is-mobile', disabled && styles.disabled) }>
  134. <input
  135. checked = { checked }
  136. disabled = { disabled }
  137. name = { name }
  138. onChange = { onChange }
  139. type = 'checkbox' />
  140. <Icon
  141. aria-hidden = { true }
  142. className = 'checkmark'
  143. color = { disabled ? theme.palette.icon03 : theme.palette.icon01 }
  144. size = { 18 }
  145. src = { IconCheck } />
  146. </div>
  147. <div>{label}</div>
  148. </label>
  149. );
  150. };
  151. export default Checkbox;