diff options
Diffstat (limited to 'src/components/atoms/forms/fields/input/input.tsx')
| -rw-r--r-- | src/components/atoms/forms/fields/input/input.tsx | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/components/atoms/forms/fields/input/input.tsx b/src/components/atoms/forms/fields/input/input.tsx new file mode 100644 index 0000000..0f0736c --- /dev/null +++ b/src/components/atoms/forms/fields/input/input.tsx @@ -0,0 +1,72 @@ +import { + type ForwardedRef, + forwardRef, + type InputHTMLAttributes, + type HTMLInputTypeAttribute, +} from 'react'; +import styles from '../fields.module.scss'; + +export type InputProps = Omit< + InputHTMLAttributes<HTMLInputElement>, + 'disabled' | 'hidden' | 'readonly' | 'required' | 'type' +> & + Required<Pick<InputHTMLAttributes<HTMLInputElement>, 'id' | 'name'>> & { + /** + * Should the field be disabled? + * + * @default false + */ + isDisabled?: boolean; + /** + * Should the field be hidden? + * + * @default false + */ + isHidden?: boolean; + /** + * Should the field be readonly? + * + * @default false + */ + isReadOnly?: boolean; + /** + * Should the field be required? + * + * @default false + */ + isRequired?: boolean; + /** + * The input type. + */ + type: Exclude<HTMLInputTypeAttribute, 'checkbox' | 'radio' | 'range'>; + }; + +const InputWithRef = ( + { + className = '', + isDisabled = false, + isHidden = false, + isReadOnly = false, + isRequired = false, + ...props + }: InputProps, + ref: ForwardedRef<HTMLInputElement> +) => { + const fieldClassName = `${styles.field} ${className}`; + + return ( + <input + {...props} + className={fieldClassName} + disabled={isDisabled} + readOnly={isReadOnly} + ref={ref} + required={isRequired} + /> + ); +}; + +/** + * Input component. + */ +export const Input = forwardRef(InputWithRef); |
