Design system.
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.
 
 
 

254 linhas
6.8 KiB

  1. import * as React from 'react';
  2. import { TextControl, tailwind } from '@tesseract-design/web-base';
  3. import { useFallbackId } from '@modal-sh/react-utils';
  4. const { tw } = tailwind;
  5. const TextInputDerivedElementComponent = 'input' as const;
  6. /**
  7. * Derived HTML element of the {@link TextInput} component.
  8. */
  9. export type TextInputDerivedElement = HTMLElementTagNameMap[
  10. typeof TextInputDerivedElementComponent
  11. ];
  12. /**
  13. * Props of the {@link TextInput} component.
  14. */
  15. export interface TextInputProps extends Omit<React.HTMLProps<TextInputDerivedElement>, 'size' | 'type' | 'label' | 'list' | 'inputMode' | 'pattern'> {
  16. /**
  17. * Short textual description indicating the nature of the component's value.
  18. */
  19. label?: React.ReactNode,
  20. /**
  21. * Short textual description as guidelines for valid input values.
  22. */
  23. hint?: React.ReactNode,
  24. /**
  25. * Size of the component.
  26. */
  27. size?: TextControl.Size,
  28. /**
  29. * Additional description, usually graphical, indicating the nature of the component's value.
  30. */
  31. indicator?: React.ReactNode,
  32. /**
  33. * Should the component display a border?
  34. */
  35. border?: boolean,
  36. /**
  37. * Should the component occupy the whole width of its parent?
  38. */
  39. block?: boolean,
  40. /**
  41. * Type of the component value.
  42. */
  43. type?: TextControl.InputType,
  44. /**
  45. * Style of the component.
  46. */
  47. variant?: TextControl.Variant,
  48. /**
  49. * Is the label hidden?
  50. */
  51. hiddenLabel?: boolean,
  52. /**
  53. * Input mode of the component.
  54. */
  55. inputMode?: TextControl.InputMode,
  56. /**
  57. * Visual length of the input.
  58. */
  59. length?: number,
  60. }
  61. /**
  62. * Component for inputting textual values.
  63. *
  64. * This component supports multiline input and adjusts its layout accordingly.
  65. */
  66. export const TextInput = React.forwardRef<TextInputDerivedElement, TextInputProps>((
  67. {
  68. label,
  69. hint,
  70. indicator,
  71. size = 'medium' as const,
  72. border = false,
  73. block = false,
  74. type = 'text' as const,
  75. variant = 'default' as const,
  76. hiddenLabel = false,
  77. className,
  78. id: idProp,
  79. style,
  80. inputMode = type,
  81. length,
  82. ...etcProps
  83. },
  84. forwardedRef,
  85. ) => {
  86. const labelId = React.useId();
  87. const id = useFallbackId(idProp);
  88. let resultInputMode = inputMode as React.HTMLProps<TextInputDerivedElement>['inputMode'];
  89. if (type === 'text' && resultInputMode === 'search') {
  90. resultInputMode = 'text';
  91. } else if (type === 'search' && resultInputMode === 'text') {
  92. resultInputMode = 'search';
  93. }
  94. return (
  95. <div
  96. className={tw(
  97. 'relative rounded ring-secondary/50 overflow-hidden group has-[:disabled]:opacity-50',
  98. 'focus-within:ring-4',
  99. {
  100. 'block': block,
  101. 'inline-block align-middle': !block,
  102. },
  103. className,
  104. )}
  105. style={style}
  106. data-testid="base"
  107. >
  108. {label && (
  109. <>
  110. <label
  111. data-testid="label"
  112. htmlFor={id}
  113. id={labelId}
  114. className={tw(
  115. 'absolute z-[1] w-full top-0.5 left-0 pointer-events-none pl-1 text-xxs font-bold group-focus-within:text-secondary text-primary leading-none bg-negative select-none',
  116. {
  117. 'sr-only': hiddenLabel,
  118. },
  119. {
  120. 'pr-1': !indicator,
  121. },
  122. {
  123. 'pr-10': indicator && size === 'small',
  124. 'pr-12': indicator && size === 'medium',
  125. 'pr-16': indicator && size === 'large',
  126. },
  127. )}
  128. >
  129. <span className="block w-full whitespace-nowrap h-[1.1em] overflow-hidden text-ellipsis">
  130. {label}
  131. </span>
  132. </label>
  133. {' '}
  134. </>
  135. )}
  136. <TextInputDerivedElementComponent
  137. {...etcProps}
  138. size={length}
  139. ref={forwardedRef}
  140. aria-labelledby={labelId}
  141. id={id}
  142. type={type}
  143. inputMode={resultInputMode}
  144. data-testid="input"
  145. className={tw(
  146. 'bg-negative rounded-inherit w-full peer block font-inherit',
  147. 'focus:outline-0',
  148. 'disabled:opacity-50 disabled:cursor-not-allowed',
  149. {
  150. 'text-xxs': size === 'small',
  151. 'text-xs': size === 'medium',
  152. },
  153. {
  154. 'pl-4': variant === 'default',
  155. 'pl-1.5': variant === 'alternate',
  156. },
  157. {
  158. 'pt-4': variant === 'alternate',
  159. },
  160. {
  161. 'pr-4': variant === 'default' && !indicator,
  162. 'pr-1.5': variant === 'alternate' && !indicator,
  163. },
  164. {
  165. 'pr-10': indicator && size === 'small',
  166. 'pr-12': indicator && size === 'medium',
  167. 'pr-16': indicator && size === 'large',
  168. },
  169. {
  170. 'h-10': size === 'small',
  171. 'h-12': size === 'medium',
  172. 'h-16': size === 'large',
  173. },
  174. )}
  175. />
  176. {hint && (
  177. <div
  178. data-testid="hint"
  179. className={tw(
  180. 'absolute left-0 px-1 pointer-events-none text-xxs leading-none w-full bg-negative select-none',
  181. {
  182. 'bottom-0 pl-4 pb-1': variant === 'default',
  183. 'top-0.5': variant === 'alternate',
  184. },
  185. {
  186. 'pt-2': variant === 'alternate' && size === 'small',
  187. 'pt-3': variant === 'alternate' && size !== 'small',
  188. },
  189. {
  190. 'pr-4': !indicator && variant === 'default',
  191. 'pr-1': !indicator && variant === 'alternate',
  192. },
  193. {
  194. 'pr-10': indicator && size === 'small',
  195. 'pr-12': indicator && size === 'medium',
  196. 'pr-16': indicator && size === 'large',
  197. },
  198. )}
  199. >
  200. <div
  201. className="opacity-50 whitespace-nowrap w-full h-[1.1em] overflow-hidden text-ellipsis"
  202. >
  203. {hint}
  204. </div>
  205. </div>
  206. )}
  207. {indicator && (
  208. <div
  209. data-testid="indicator"
  210. className={tw(
  211. 'text-center flex items-center justify-center aspect-square absolute bottom-0 right-0 pointer-events-none select-none',
  212. {
  213. 'w-10': size === 'small',
  214. 'w-12': size === 'medium',
  215. 'w-16': size === 'large',
  216. },
  217. )}
  218. >
  219. {indicator}
  220. </div>
  221. )}
  222. {border && (
  223. <span
  224. data-testid="border"
  225. className="absolute z-[1] inset-0 rounded-inherit border-2 border-primary pointer-events-none peer-focus:border-secondary"
  226. />
  227. )}
  228. </div>
  229. );
  230. });
  231. TextInput.displayName = 'TextInput';
  232. TextInput.defaultProps = {
  233. label: undefined,
  234. hint: undefined,
  235. size: 'medium',
  236. indicator: undefined,
  237. border: false,
  238. block: false,
  239. type: 'text',
  240. variant: 'default',
  241. hiddenLabel: false,
  242. inputMode: 'text',
  243. length: undefined,
  244. };