Common front-end components for Web using the Tesseract design system, written for React. https://make.modal.sh/tesseract/web/react/common
Você não pode selecionar mais de 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.
 
 
 
 

307 linhas
7.4 KiB

  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)',
  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. 'backgroundColor': 'var(--color-bg, white)',
  68. 'color': 'var(--color-fg, black)',
  69. 'appearance': 'none',
  70. 'boxSizing': 'border-box',
  71. 'position': 'relative',
  72. 'border': 0,
  73. 'paddingLeft': '1rem',
  74. 'margin': 0,
  75. 'font': 'inherit',
  76. 'minHeight': '4rem',
  77. 'minWidth': '16rem',
  78. 'maxWidth': '100%',
  79. 'zIndex': 1,
  80. 'cursor': 'pointer',
  81. 'transitionProperty': 'background-color, color',
  82. ':focus': {
  83. outline: 0,
  84. },
  85. ':disabled': {
  86. cursor: 'not-allowed',
  87. },
  88. '::-moz-focus-inner': {
  89. outline: 0,
  90. border: 0,
  91. },
  92. })
  93. Input.displayName = 'select'
  94. const Border = styled('span')({
  95. 'borderColor': 'var(--color-accent, blue)',
  96. 'boxSizing': 'border-box',
  97. 'display': 'inline-block',
  98. 'borderWidth': '0.125rem',
  99. 'borderStyle': 'solid',
  100. 'position': 'absolute',
  101. 'top': 0,
  102. 'left': 0,
  103. 'width': '100%',
  104. 'height': '100%',
  105. 'borderRadius': 'inherit',
  106. 'zIndex': 2,
  107. 'pointerEvents': 'none',
  108. 'transitionProperty': 'border-color',
  109. '::before': {
  110. position: 'absolute',
  111. top: 0,
  112. left: 0,
  113. width: '100%',
  114. height: '100%',
  115. content: "''",
  116. borderRadius: '0.125rem',
  117. opacity: 0.5,
  118. pointerEvents: 'none',
  119. },
  120. [`${ComponentBase}:focus-within &::before`]: {
  121. boxShadow: '0 0 0 0.375rem var(--color-accent, blue)',
  122. },
  123. })
  124. const HintWrapper = styled('span')({
  125. boxSizing: 'border-box',
  126. position: 'absolute',
  127. bottom: 0,
  128. left: 0,
  129. paddingLeft: '1rem',
  130. fontSize: '0.85em',
  131. opacity: 0.5,
  132. maxWidth: '100%',
  133. overflow: 'hidden',
  134. textOverflow: 'ellipsis',
  135. whiteSpace: 'nowrap',
  136. zIndex: 2,
  137. pointerEvents: 'none',
  138. lineHeight: 1,
  139. userSelect: 'none',
  140. })
  141. const IndicatorWrapper = styled('span')({
  142. color: 'var(--color-accent, blue)',
  143. boxSizing: 'border-box',
  144. position: 'absolute',
  145. top: 0,
  146. right: 0,
  147. height: '100%',
  148. display: 'grid',
  149. placeContent: 'center',
  150. padding: '0 1rem',
  151. zIndex: 1,
  152. pointerEvents: 'none',
  153. transitionProperty: 'color',
  154. lineHeight: 1,
  155. userSelect: 'none',
  156. })
  157. const propTypes = {
  158. /**
  159. * Short textual description indicating the nature of the component's value.
  160. */
  161. label: PropTypes.any,
  162. /**
  163. * Class name for the component, used for styling.
  164. */
  165. className: PropTypes.string,
  166. /**
  167. * Short textual description as guidelines for valid input values.
  168. */
  169. hint: PropTypes.any,
  170. /**
  171. * Size of the component.
  172. */
  173. size: PropTypes.oneOf<Size>(['small', 'medium', 'large']),
  174. /**
  175. * Should the component take up the remaining space parallel to the content flow?
  176. */
  177. block: PropTypes.bool,
  178. /**
  179. * Can multiple values be selected?
  180. */
  181. multiple: PropTypes.bool,
  182. /**
  183. * Is the component active?
  184. */
  185. disabled: PropTypes.bool,
  186. /**
  187. * CSS styles.
  188. */
  189. style: PropTypes.object,
  190. }
  191. type Props = PropTypes.InferProps<typeof propTypes>
  192. /**
  193. * Component for selecting values from a larger number of options.
  194. * @see {@link Checkbox} for a similar component on selecting values among very few choices.
  195. * @see {@link RadioButton} for a similar component on selecting a single value among very few choices.
  196. * @type {React.ComponentType<{readonly label?: string, readonly hint?: string, readonly className?: string, readonly
  197. * size?: 'small' | 'medium' | 'large', readonly multiple?: boolean, readonly block?: boolean} &
  198. * React.ClassAttributes<unknown>>}
  199. */
  200. const Select = React.forwardRef<HTMLSelectElement, Props>(
  201. (
  202. {
  203. label = '',
  204. className = '',
  205. hint = '',
  206. size: sizeProp = 'medium',
  207. block = false,
  208. multiple = false,
  209. disabled = false,
  210. style = {},
  211. ...etcProps
  212. },
  213. ref,
  214. ) => {
  215. const size = sizeProp as Size
  216. return (
  217. <ComponentBase
  218. style={{
  219. display: block ? 'block' : 'inline-block',
  220. opacity: disabled ? 0.5 : undefined,
  221. }}
  222. >
  223. <Border />
  224. <CaptureArea className={className!}>
  225. <LabelWrapper
  226. style={{
  227. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  228. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  229. paddingRight: !multiple ? MIN_HEIGHTS[size!] : '0.5rem',
  230. fontSize: SECONDARY_TEXT_SIZES[size!],
  231. }}
  232. >
  233. {stringify(label)}
  234. </LabelWrapper>
  235. {stringify(label).length > 0 && ' '}
  236. <Input
  237. {...etcProps}
  238. ref={ref}
  239. disabled={disabled!}
  240. multiple={multiple!}
  241. style={{
  242. ...style,
  243. display: block ? 'block' : 'inline-block',
  244. verticalAlign: 'top',
  245. fontSize: INPUT_FONT_SIZES[size!],
  246. width: block || multiple ? '100%' : undefined,
  247. height: multiple ? undefined : MIN_HEIGHTS[size!],
  248. minHeight: MIN_HEIGHTS[size!],
  249. resize: multiple ? 'vertical' : undefined,
  250. paddingTop: VERTICAL_PADDING_SIZES[size!],
  251. paddingBottom: VERTICAL_PADDING_SIZES[size!],
  252. paddingRight: !multiple ? MIN_HEIGHTS[size!] : '1rem',
  253. }}
  254. />
  255. </CaptureArea>
  256. {stringify(hint).length > 0 && ' '}
  257. {stringify(hint).length > 0 && (
  258. <HintWrapper
  259. style={{
  260. paddingTop: LABEL_VERTICAL_PADDING_SIZES[size!],
  261. paddingBottom: LABEL_VERTICAL_PADDING_SIZES[size!],
  262. paddingRight: MIN_HEIGHTS[size!],
  263. fontSize: SECONDARY_TEXT_SIZES[size!],
  264. }}
  265. >
  266. ({stringify(hint)})
  267. </HintWrapper>
  268. )}
  269. {!multiple && (
  270. <IndicatorWrapper
  271. style={{
  272. width: MIN_HEIGHTS[size!],
  273. }}
  274. >
  275. <Icon name="chevron-down" label="" />
  276. </IndicatorWrapper>
  277. )}
  278. </ComponentBase>
  279. )
  280. },
  281. )
  282. Select.propTypes = propTypes
  283. Select.displayName = 'Select'
  284. export default Select