Design system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

126 regels
3.0 KiB

  1. import * as React from 'react';
  2. import * as TextControlBase from '@tesseract-design/web-base-textcontrol';
  3. export type MaskedTextInputProps = Omit<React.HTMLProps<HTMLInputElement>, 'size' | 'type' | 'style'> & {
  4. /**
  5. * Short textual description indicating the nature of the component's value.
  6. */
  7. label?: React.ReactNode,
  8. /**
  9. * Short textual description as guidelines for valid input values.
  10. */
  11. hint?: React.ReactNode,
  12. /**
  13. * Size of the component.
  14. */
  15. size?: TextControlBase.TextControlSize,
  16. /**
  17. * Additional description, usually graphical, indicating the nature of the component's value.
  18. */
  19. indicator?: React.ReactNode,
  20. /**
  21. * Should the component display a border?
  22. */
  23. border?: boolean,
  24. /**
  25. * Should the component occupy the whole width of its parent?
  26. */
  27. block?: boolean,
  28. /**
  29. * Style of the component.
  30. */
  31. style?: TextControlBase.TextControlStyle,
  32. /**
  33. * Is the label hidden?
  34. */
  35. hiddenLabel?: boolean,
  36. }
  37. /**
  38. * Component for inputting textual values.
  39. *
  40. * This component supports multiline input and adjusts its layout accordingly.
  41. */
  42. export const MaskedTextInput = React.forwardRef<HTMLInputElement, MaskedTextInputProps>(
  43. (
  44. {
  45. label = '',
  46. hint = '',
  47. indicator = null,
  48. size = TextControlBase.TextControlSize.MEDIUM,
  49. border = false,
  50. block = false,
  51. style = TextControlBase.TextControlStyle.DEFAULT,
  52. hiddenLabel = false,
  53. className: _className,
  54. placeholder: _placeholder,
  55. as: _as,
  56. ...etcProps
  57. }: MaskedTextInputProps,
  58. ref,
  59. ) => {
  60. const textInputBaseArgs = React.useMemo<TextControlBase.TextControlBaseArgs>(() => ({
  61. block,
  62. border,
  63. size,
  64. indicator: Boolean(indicator),
  65. style,
  66. resizable: false,
  67. predefinedValues: false,
  68. }), [block, border, size, indicator, style]);
  69. return (
  70. <div
  71. className={TextControlBase.Root(textInputBaseArgs)}
  72. >
  73. <input
  74. {...etcProps}
  75. className={TextControlBase.Input(textInputBaseArgs)}
  76. ref={ref}
  77. aria-label={label}
  78. type="password"
  79. data-testid="input"
  80. />
  81. {
  82. border && (
  83. <span
  84. data-testid="border"
  85. />
  86. )
  87. }
  88. {
  89. label && !hiddenLabel && (
  90. <div
  91. data-testid="label"
  92. className={TextControlBase.LabelWrapper(textInputBaseArgs)}
  93. >
  94. {label}
  95. </div>
  96. )
  97. }
  98. {hint && (
  99. <div
  100. className={TextControlBase.HintWrapper(textInputBaseArgs)}
  101. data-testid="hint"
  102. >
  103. <div
  104. className={TextControlBase.Hint()}
  105. >
  106. {hint}
  107. </div>
  108. </div>
  109. )}
  110. {indicator && (
  111. <div
  112. className={TextControlBase.IndicatorWrapper(textInputBaseArgs)}
  113. >
  114. {indicator}
  115. </div>
  116. )}
  117. </div>
  118. );
  119. },
  120. );
  121. MaskedTextInput.displayName = 'MaskedTextInput';