import React, {ChangeEvent} from 'react'; import classNames from 'classnames'; import {useFormContext} from 'react-hook-form'; import {Option} from 'checkout/ShippingAddressProps'; import CheckmarkIcon from '@moda/icons/checkmark-16'; import WarningIcon from '@moda/icons/warning-16'; interface ErrorObject { value: number; message: string; } interface ValidationRules { maxLength?: number | ErrorObject; minLength?: number | ErrorObject; required?: boolean | string; pattern?: RegExp; validate?: any; max?: number; min?: number; } interface Props { validationRules?: ValidationRules; onChange?(o: Option): void; className?: string; label: string; name: string; } interface Error { message: ''; } const ShippingAddressTextInput: React.FC = ({ validationRules = {}, className, onChange, label, name }): JSX.Element => { const {register, watch, errors, triggerValidation} = useFormContext(); function handleChange(e: ChangeEvent) { if (onChange) onChange({label: e.target.value, value: e.target.value}); triggerValidation(name); } const error = errors[name] as Error; return (
{error && } {!error && watch(name) && } {error &&
{error.message}
}
); }; export default ShippingAddressTextInput;