|
| 1 | +import classnames from 'classnames'; |
| 2 | +import React, { forwardRef, InputHTMLAttributes, useMemo } from 'react'; |
| 3 | +import { useFocus } from '../../hooks'; |
| 4 | +import { Icon } from '../Icon'; |
| 5 | + |
| 6 | +export interface InputProps extends InputHTMLAttributes<HTMLInputElement> { |
| 7 | + /** |
| 8 | + * Messaging feedback for the input. This can be used for displaying feedback |
| 9 | + * inside of forms. |
| 10 | + * @default "default" |
| 11 | + */ |
| 12 | + message?: 'default' | 'warning' | 'error'; |
| 13 | + /** |
| 14 | + * Text to display under the input describing the message. |
| 15 | + */ |
| 16 | + messageText?: string; |
| 17 | + /** |
| 18 | + * Should this input fill the width of the parent container. |
| 19 | + */ |
| 20 | + fillWidth?: boolean; |
| 21 | + /** |
| 22 | + * A class for the wrapper div which encompases the input and icon and warning |
| 23 | + * texrt. Use this to do things like add margin-bottom to the entire input for |
| 24 | + * alignment in forms. |
| 25 | + */ |
| 26 | + wrapperClassName?: string; |
| 27 | + /** |
| 28 | + * If true, will add padding below the input to compensate for message text |
| 29 | + * being absent. This prevents UI jumping when message text appears. |
| 30 | + */ |
| 31 | + padMessageText?: boolean; |
| 32 | +} |
| 33 | + |
| 34 | +export const Input = forwardRef<HTMLInputElement, InputProps>( |
| 35 | + ( |
| 36 | + { |
| 37 | + message = 'default', |
| 38 | + messageText, |
| 39 | + fillWidth, |
| 40 | + wrapperClassName, |
| 41 | + padMessageText, |
| 42 | + className, |
| 43 | + onFocus, |
| 44 | + onBlur, |
| 45 | + ...props |
| 46 | + }, |
| 47 | + ref, |
| 48 | + ) => { |
| 49 | + const { focused, handleOnFocus, handleOnBlur } = useFocus(onFocus, onBlur); |
| 50 | + |
| 51 | + const icon = useMemo(() => { |
| 52 | + switch (message) { |
| 53 | + case 'default': |
| 54 | + return undefined; |
| 55 | + case 'warning': |
| 56 | + return 'Warning'; |
| 57 | + case 'error': |
| 58 | + return 'Error'; |
| 59 | + } |
| 60 | + }, [message]); |
| 61 | + |
| 62 | + const wrapperClass = classnames( |
| 63 | + 'ui__base ui__input__wrapper', |
| 64 | + { |
| 65 | + 'ui__input__wrapper--fill': fillWidth, |
| 66 | + 'ui__input__wrapper--pad': padMessageText && !messageText, |
| 67 | + }, |
| 68 | + wrapperClassName, |
| 69 | + ); |
| 70 | + const mainClass = classnames('ui__input', `ui__input--message-${message}`, { 'ui__input--focused': focused }); |
| 71 | + const inputClass = classnames('ui__input__input', { 'ui__input__input--disabled': props.disabled }, className); |
| 72 | + |
| 73 | + return ( |
| 74 | + <div className={wrapperClass}> |
| 75 | + <div className={mainClass}> |
| 76 | + <input {...props} onFocus={handleOnFocus} onBlur={handleOnBlur} className={inputClass} ref={ref} /> |
| 77 | + {icon ? <Icon className="ui__input__icon" icon={icon} /> : undefined} |
| 78 | + </div> |
| 79 | + {messageText ? <div className="ui__input__messageText">{messageText}</div> : undefined} |
| 80 | + </div> |
| 81 | + ); |
| 82 | + }, |
| 83 | +); |
0 commit comments