|
| 1 | +import { PatternFormat } from 'react-number-format'; |
| 2 | + |
| 3 | +import { InputError } from '~/components/input-error'; |
| 4 | +import { InputHelp } from '~/components/input-help'; |
| 5 | +import { InputLabel } from '~/components/input-label'; |
| 6 | +import { cn } from '~/utils/tailwind-utils'; |
| 7 | + |
| 8 | +const inputBaseClassName = |
| 9 | + 'block rounded-lg border-gray-500 focus:border-blue-500 focus:outline-none focus:ring focus:ring-blue-500'; |
| 10 | +const inputDisabledClassName = |
| 11 | + 'disabled:bg-gray-100 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-70'; |
| 12 | +const inputReadOnlyClassName = |
| 13 | + 'read-only:bg-gray-100 read-only:pointer-events-none read-only:cursor-not-allowed read-only:opacity-70'; |
| 14 | +const inputErrorClassName = 'border-red-500 focus:border-red-500 focus:ring-red-500'; |
| 15 | + |
| 16 | +export interface InputPatternFieldProps |
| 17 | + extends OmitStrict< |
| 18 | + React.ComponentProps<typeof PatternFormat>, |
| 19 | + 'aria-errormessage' | 'aria-invalid' | 'aria-labelledby' | 'aria-required' | 'value' | 'onChange' |
| 20 | + > { |
| 21 | + defaultValue: string; |
| 22 | + errorMessage?: string; |
| 23 | + format: string; |
| 24 | + helpMessagePrimary?: React.ReactNode; |
| 25 | + helpMessagePrimaryClassName?: string; |
| 26 | + helpMessageSecondary?: React.ReactNode; |
| 27 | + helpMessageSecondaryClassName?: string; |
| 28 | + id: string; |
| 29 | + label: string; |
| 30 | + name: string; |
| 31 | +} |
| 32 | + |
| 33 | +export function InputPatternField(props: InputPatternFieldProps) { |
| 34 | + const { |
| 35 | + 'aria-describedby': ariaDescribedby, |
| 36 | + className, |
| 37 | + defaultValue, |
| 38 | + errorMessage, |
| 39 | + helpMessagePrimary, |
| 40 | + helpMessagePrimaryClassName, |
| 41 | + helpMessageSecondary, |
| 42 | + helpMessageSecondaryClassName, |
| 43 | + id, |
| 44 | + label, |
| 45 | + required, |
| 46 | + ...restProps |
| 47 | + } = props; |
| 48 | + |
| 49 | + const inputWrapperId = `input-pattern-field-${id}`; |
| 50 | + const inputErrorId = `${inputWrapperId}-error`; |
| 51 | + const inputHelpMessagePrimaryId = `${inputWrapperId}-help-primary`; |
| 52 | + const inputHelpMessageSecondaryId = `${inputWrapperId}-help-secondary`; |
| 53 | + const inputLabelId = `${inputWrapperId}-label`; |
| 54 | + |
| 55 | + function getAriaDescribedby() { |
| 56 | + const describedby = []; |
| 57 | + if (ariaDescribedby) describedby.push(ariaDescribedby); |
| 58 | + if (helpMessagePrimary) describedby.push(inputHelpMessagePrimaryId); |
| 59 | + if (helpMessageSecondary) describedby.push(inputHelpMessageSecondaryId); |
| 60 | + return describedby.length > 0 ? describedby.join(' ') : undefined; |
| 61 | + } |
| 62 | + |
| 63 | + return ( |
| 64 | + <div id={inputWrapperId} data-testid={inputWrapperId}> |
| 65 | + <InputLabel id={inputLabelId} htmlFor={id} className="mb-2"> |
| 66 | + {label} |
| 67 | + </InputLabel> |
| 68 | + {errorMessage && ( |
| 69 | + <p className="mb-2"> |
| 70 | + <InputError id={inputErrorId}>{errorMessage}</InputError> |
| 71 | + </p> |
| 72 | + )} |
| 73 | + {helpMessagePrimary && ( |
| 74 | + <InputHelp |
| 75 | + id={inputHelpMessagePrimaryId} |
| 76 | + className={cn('mb-2', helpMessagePrimaryClassName)} |
| 77 | + data-testid="input-pattern-field-help-primary" |
| 78 | + > |
| 79 | + {helpMessagePrimary} |
| 80 | + </InputHelp> |
| 81 | + )} |
| 82 | + <PatternFormat |
| 83 | + aria-describedby={getAriaDescribedby()} |
| 84 | + aria-errormessage={errorMessage ? inputErrorId : undefined} |
| 85 | + aria-invalid={!!errorMessage} |
| 86 | + aria-labelledby={inputLabelId} |
| 87 | + aria-required={required} |
| 88 | + data-testid="input-pattern-field" |
| 89 | + id={id} |
| 90 | + className={cn( |
| 91 | + inputBaseClassName, |
| 92 | + inputDisabledClassName, |
| 93 | + inputReadOnlyClassName, |
| 94 | + errorMessage && inputErrorClassName, |
| 95 | + className, |
| 96 | + )} |
| 97 | + required={required} |
| 98 | + value={defaultValue} |
| 99 | + {...restProps} |
| 100 | + /> |
| 101 | + {helpMessageSecondary && ( |
| 102 | + <InputHelp |
| 103 | + id={inputHelpMessageSecondaryId} |
| 104 | + className={cn('mt-2', helpMessageSecondaryClassName)} |
| 105 | + data-testid="input-pattern-field-help-secondary" |
| 106 | + > |
| 107 | + {helpMessageSecondary} |
| 108 | + </InputHelp> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + ); |
| 112 | +} |
0 commit comments