|
| 1 | +import clsx from 'clsx'; |
| 2 | +import type { ChangeEventHandler } from 'react'; |
| 3 | +import React, { forwardRef } from 'react'; |
| 4 | +import styles from './Input-v2.module.css'; |
| 5 | + |
| 6 | +export type InputProps = React.InputHTMLAttributes<HTMLInputElement> & { |
| 7 | + /** |
| 8 | + * Aria-label to provide an accesible name for the text input if no visible label is provided. |
| 9 | + */ |
| 10 | + 'aria-label'?: string; |
| 11 | + /** |
| 12 | + * CSS class names that can be appended to the component. |
| 13 | + */ |
| 14 | + className?: string; |
| 15 | + /** |
| 16 | + * Disables the input and prevents editing the contents |
| 17 | + */ |
| 18 | + disabled?: boolean; |
| 19 | + /** |
| 20 | + * HTML id for the component |
| 21 | + */ |
| 22 | + id?: string; |
| 23 | + /** |
| 24 | + * Gives a hint as to the type of data needed for text input |
| 25 | + */ |
| 26 | + inputMode?: |
| 27 | + | 'text' |
| 28 | + | 'email' |
| 29 | + | 'url' |
| 30 | + | 'search' |
| 31 | + | 'tel' |
| 32 | + | 'none' |
| 33 | + | 'numeric' |
| 34 | + | 'decimal'; |
| 35 | + /** |
| 36 | + * Maximum number the input can take. |
| 37 | + */ |
| 38 | + max?: number | string; |
| 39 | + /** |
| 40 | + * Minimum number the input can take. |
| 41 | + */ |
| 42 | + min?: number | string; |
| 43 | + /** |
| 44 | + * HTML name attribute for the input |
| 45 | + */ |
| 46 | + name?: string; |
| 47 | + /** |
| 48 | + * Function that fires when field value has changed |
| 49 | + */ |
| 50 | + onChange?: ChangeEventHandler<HTMLInputElement>; |
| 51 | + /** |
| 52 | + * Placeholder attribute for input. Note: placeholder should be used sparingly |
| 53 | + */ |
| 54 | + placeholder?: string; |
| 55 | + /** |
| 56 | + * Toggles the form control's interactivity. When `readOnly` is set to `true`, the form control is not interactive |
| 57 | + */ |
| 58 | + readOnly?: boolean; |
| 59 | + /** |
| 60 | + * Indicates that field is required for form to be successfully submitted |
| 61 | + */ |
| 62 | + required?: boolean; |
| 63 | + /** |
| 64 | + * Title attribute on input |
| 65 | + */ |
| 66 | + title?: string; |
| 67 | + /** |
| 68 | + * HTML type attribute, allowing switching between text, password, and other HTML5 input field types |
| 69 | + */ |
| 70 | + type?: React.HTMLInputTypeAttribute; |
| 71 | + /** |
| 72 | + * The value of the input |
| 73 | + */ |
| 74 | + value?: string | number; |
| 75 | + /** |
| 76 | + * The default value of the input |
| 77 | + */ |
| 78 | + defaultValue?: string | number; |
| 79 | + // Design API |
| 80 | + /** |
| 81 | + * Error state of the form field |
| 82 | + */ |
| 83 | + isError?: boolean; |
| 84 | + /** |
| 85 | + * Whether there is a warning state for the field note text (and icon) |
| 86 | + */ |
| 87 | + isWarning?: boolean; |
| 88 | +}; |
| 89 | + |
| 90 | +/** |
| 91 | + * Input component for one line of text. |
| 92 | + */ |
| 93 | +export const Input = forwardRef<HTMLInputElement, InputProps>( |
| 94 | + ({ className, disabled, id, isError, isWarning, ...other }, ref) => { |
| 95 | + const componentClassName = clsx( |
| 96 | + styles['input'], |
| 97 | + isError && styles['error'], |
| 98 | + isWarning && styles['warning'], |
| 99 | + className, |
| 100 | + ); |
| 101 | + |
| 102 | + return ( |
| 103 | + <input |
| 104 | + className={componentClassName} |
| 105 | + disabled={disabled} |
| 106 | + id={id} |
| 107 | + ref={ref} |
| 108 | + {...other} |
| 109 | + /> |
| 110 | + ); |
| 111 | + }, |
| 112 | +); |
| 113 | + |
| 114 | +Input.displayName = 'Input'; |
0 commit comments