diff --git a/docs/TelInput.md b/docs/TelInput.md index 06d1316f..8894ea0b 100644 --- a/docs/TelInput.md +++ b/docs/TelInput.md @@ -21,6 +21,14 @@ import { TelInput } from 'tailor-ui'; ``` +### With errors + +```jsx live + + + +``` + ## API The component is based on `react-intl-tel-input`, please check more API on it's [document](https://patw0929.github.io/react-intl-tel-input/). diff --git a/packages/tailor-ui/src/TelInput/TelInput.tsx b/packages/tailor-ui/src/TelInput/TelInput.tsx index 50c59a55..11ce0a0a 100644 --- a/packages/tailor-ui/src/TelInput/TelInput.tsx +++ b/packages/tailor-ui/src/TelInput/TelInput.tsx @@ -1,19 +1,23 @@ import IntlTelInput from 'react-intl-tel-input'; import React, { FC, MouseEventHandler } from 'react'; +import { useFormField } from '../FormField'; + import StyledTelInput from './style'; +type ChangeHandler = ( + isValid: boolean, + value: string, + phoneInfo: object, + fullPhoneNumber: string +) => void; + interface TelInputProps { id?: string; name?: string; value?: string; defaultValue?: string; - onChange?: ( - isValid: boolean, - value: string, - phoneInfo: object, - fullPhoneNumber: string - ) => void; + onChange?: ChangeHandler; onBlur?: MouseEventHandler; } @@ -23,18 +27,39 @@ const TelInput: FC = ({ onChange, onBlur, ...props -}) => ( - - - -); +}) => { + const [invalid, labelId, setValue] = useFormField({ + id, + value: props.value, + defaultValue: props.defaultValue, + }); + + const onPhoneNumberChange: ChangeHandler = ( + isValid, + value, + phoneInfo, + fullPhoneNumber + ) => { + setValue(value); + + if (onChange) { + onChange(isValid, value, phoneInfo, fullPhoneNumber); + } + }; + + return ( + + + + ); +}; export { TelInput };