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