Common front-end components for Web using the Tesseract design system, written for React. https://make.modal.sh/tesseract/web/react/common
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Select.tsx 6.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import * as React from 'react'
  2. import * as PropTypes from 'prop-types'
  3. import styled from 'styled-components'
  4. import stringify from '../../services/stringify'
  5. import { Size, SizeMap } from '../../services/utilities'
  6. import Icon from '../Icon/Icon'
  7. const MIN_HEIGHTS: SizeMap<string | number> = {
  8. small: '2.5rem',
  9. medium: '3rem',
  10. large: '4rem',
  11. }
  12. const LABEL_VERTICAL_PADDING_SIZES: SizeMap<string | number> = {
  13. small: '0.125rem',
  14. medium: '0.25rem',
  15. large: '0.5rem',
  16. }
  17. const VERTICAL_PADDING_SIZES: SizeMap<string | number> = {
  18. small: '0.6rem',
  19. medium: '0.85rem',
  20. large: '1.25rem',
  21. }
  22. const INPUT_FONT_SIZES: SizeMap<string | number> = {
  23. small: '0.85em',
  24. medium: '0.85em',
  25. large: '1em',
  26. }
  27. const SECONDARY_TEXT_SIZES: SizeMap<string | number> = {
  28. small: '0.65em',
  29. medium: '0.75em',
  30. large: '0.85em',
  31. }
  32. const ComponentBase = styled('div')({
  33. position: 'relative',
  34. borderRadius: '0.25rem',
  35. fontFamily: 'var(--font-family-base, sans-serif)',
  36. maxWidth: '100%',
  37. ':focus-within': {
  38. '--color-accent': 'var(--color-active, Highlight)',
  39. },
  40. })
  41. ComponentBase.displayName = 'div'
  42. const CaptureArea = styled('label')({
  43. display: 'block',
  44. })
  45. CaptureArea.displayName = 'label'
  46. const LabelWrapper = styled('span')({
  47. color: 'var(--color-accent, blue)',
  48. boxSizing: 'border-box',
  49. position: 'absolute',
  50. top: 0,
  51. left: 0,
  52. paddingLeft: '0.5rem',
  53. fontSize: '0.85em',
  54. maxWidth: '100%',
  55. overflow: 'hidden',
  56. textOverflow: 'ellipsis',
  57. whiteSpace: 'nowrap',
  58. fontWeight: 'bolder',
  59. zIndex: 2,
  60. pointerEvents: 'none',
  61. transitionProperty: 'color',
  62. lineHeight: 1,
  63. userSelect: 'none',
  64. })
  65. LabelWrapper.displayName = 'span'
  66. const Input = styled('select')({
  67. display: 'block',
  68. backgroundColor: 'var(--color-bg, white)',
  69. color: 'var(--color-fg, black)',
  70. appearance: 'none',
  71. boxSizing: 'border-box',
  72. position: 'relative',
  73. border: 0,
  74. paddingLeft: '1rem',
  75. margin: 0,
  76. font: 'inherit',
  77. minHeight: '4rem',
  78. minWidth: '8rem',
  79. maxWidth: '100%',
  80. zIndex: 1,
  81. cursor: 'pointer',
  82. transitionProperty: 'background-color, color',
  83. ':focus': {
  84. outline: 0,
  85. },
  86. ':disabled': {
  87. cursor: 'not-allowed',
  88. },
  89. '::-moz-focus-inner': {
  90. outline: 0,
  91. border: 0,
  92. },
  93. })
  94. Input.displayName = 'select'
  95. const Border = styled('span')({
  96. borderColor: 'var(--color-accent, blue)',
  97. boxSizing: 'border-box',
  98. display: 'inline-block',
  99. borderWidth: '0.125rem',
  100. borderStyle: 'solid',
  101. position: 'absolute',
  102. top: 0,
  103. left: 0,
  104. width: '100%',
  105. height: '100%',
  106. borderRadius: 'inherit',
  107. zIndex: 2,
  108. pointerEvents: 'none',
  109. transitionProperty: 'border-color',
  110. '::before': {
  111. position: 'absolute',
  112. top: 0,
  113. left: 0,
  114. width: '100%',
  115. height: '100%',
  116. content: "''",
  117. borderRadius: '0.125rem',
  118. opacity: 0.5,
  119. pointerEvents: 'none',
  120. },
  121. [`${ComponentBase}:focus-within &::before`]: {
  122. boxShadow: '0 0 0 0.375rem var(--color-accent, blue)',
  123. },
  124. })
  125. const HintWrapper = styled('span')({
  126. boxSizing: 'border-box',
  127. position: 'absolute',
  128. bottom: 0,
  129. left: 0,
  130. paddingLeft: '1rem',
  131. fontSize: '0.85em',
  132. opacity: 0.5,
  133. maxWidth: '100%',
  134. overflow: 'hidden',
  135. textOverflow: 'ellipsis',
  136. whiteSpace: 'nowrap',
  137. zIndex: 2,
  138. pointerEvents: 'none',
  139. lineHeight: 1,
  140. userSelect: 'none',
  141. })
  142. const IndicatorWrapper = styled('span')({
  143. color: 'var(--color-accent, blue)',
  144. boxSizing: 'border-box',
  145. position: 'absolute',
  146. top: 0,
  147. right: 0,
  148. height: '100%',
  149. display: 'grid',
  150. placeContent: 'center',
  151. padding: '0 1rem',
  152. zIndex: 1,
  153. pointerEvents: 'none',
  154. transitionProperty: 'color',
  155. lineHeight: 1,
  156. userSelect: 'none',
  157. })
  158. const propTypes = {
  159. /**
  160. * Short textual description indicating the nature of the component's value.
  161. */
  162. label: PropTypes.any,
  163. /**
  164. * Class name for the component, used for styling.
  165. */
  166. className: PropTypes.string,
  167. /**
  168. * Short textual description as guidelines for valid input values.
  169. */
  170. hint: PropTypes.any,
  171. /**
  172. * Size of the component.
  173. */
  174. size: PropTypes.oneOf<Size>(['small', 'medium', 'large']),
  175. /**
  176. * Can multiple values be selected?
  177. */
  178. multiple: PropTypes.bool,
  179. /**
  180. * Is the component active?
  181. */
  182. disabled: PropTypes.bool,
  183. /**
  184. * CSS styles.
  185. */
  186. style: PropTypes.object,
  187. }
  188. type Props = PropTypes.InferProps<typeof propTypes>
  189. const Select = React.forwardRef<HTMLSelectElement, Props>(
  190. (
  191. {
  192. label = '',
  193. className = '',
  194. hint = '',
  195. size = 'medium',
  196. multiple = false,
  197. disabled = false,
  198. style = {},
  199. ...etcProps
  200. },
  201. ref,
  202. ) => {
  203. return (
  204. <ComponentBase
  205. style={{
  206. opacity: disabled ? 0.5 : undefined,
  207. }}
  208. >
  209. <Border />
  210. <CaptureArea className={className!}>
  211. <LabelWrapper
  212. style={{
  213. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  214. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  215. paddingRight: !multiple ? MIN_HEIGHTS[size!] : '0.5rem',
  216. fontSize: SECONDARY_TEXT_SIZES[size!],
  217. }}
  218. >
  219. {stringify(label)}
  220. </LabelWrapper>
  221. {stringify(label).length > 0 && ' '}
  222. <Input
  223. {...etcProps}
  224. ref={ref}
  225. disabled={disabled!}
  226. multiple={multiple!}
  227. style={{
  228. ...style,
  229. verticalAlign: 'top',
  230. fontSize: INPUT_FONT_SIZES[size!],
  231. height: multiple ? undefined : MIN_HEIGHTS[size!],
  232. minHeight: MIN_HEIGHTS[size!],
  233. resize: multiple ? 'vertical' : undefined,
  234. paddingTop: VERTICAL_PADDING_SIZES[size!],
  235. paddingBottom: VERTICAL_PADDING_SIZES[size!],
  236. paddingRight: !multiple ? MIN_HEIGHTS[size!] : '1rem',
  237. }}
  238. />
  239. </CaptureArea>
  240. {stringify(hint).length > 0 && ' '}
  241. {stringify(hint).length > 0 && (
  242. <HintWrapper
  243. style={{
  244. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  245. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  246. paddingRight: MIN_HEIGHTS[size!],
  247. fontSize: SECONDARY_TEXT_SIZES[size!],
  248. }}
  249. >
  250. ({stringify(hint)})
  251. </HintWrapper>
  252. )}
  253. {!multiple && (
  254. <IndicatorWrapper
  255. style={{
  256. width: MIN_HEIGHTS[size!],
  257. }}
  258. >
  259. <Icon name="chevron-down" label="" />
  260. </IndicatorWrapper>
  261. )}
  262. </ComponentBase>
  263. )
  264. },
  265. )
  266. Select.propTypes = propTypes
  267. Select.displayName = 'Select'
  268. export default Select