Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(TelInput): add FormField in TelInput, ref: #311 #373

Merged
merged 1 commit into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/TelInput.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ import { TelInput } from 'tailor-ui';
<TelInput onChange={console.log} />
```

### With errors

```jsx live
<FormField label="With Error" validationMessage="Something errors happend">
<TelInput onChange={console.log} />
</FormField>
```

## 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/).
63 changes: 44 additions & 19 deletions packages/tailor-ui/src/TelInput/TelInput.tsx
Original file line number Diff line number Diff line change
@@ -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;
}

Expand All @@ -23,18 +27,39 @@ const TelInput: FC<TelInputProps> = ({
onChange,
onBlur,
...props
}) => (
<StyledTelInput>
<IntlTelInput
fieldId={id}
fieldName={name}
onPhoneNumberChange={onChange}
onPhoneNumberBlur={onBlur}
format
preferredCountries={['tw']}
{...props}
/>
</StyledTelInput>
);
}) => {
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 (
<StyledTelInput invalid={invalid}>
<IntlTelInput
format
fieldId={labelId}
fieldName={name}
onPhoneNumberBlur={onBlur}
onPhoneNumberChange={onPhoneNumberChange}
preferredCountries={['tw']}
{...props}
/>
</StyledTelInput>
);
};

export { TelInput };