Skip to content

Commit 065e711

Browse files
committed
feat(phone-input-controller): add phone input controller
uses mui-tel-input package
1 parent 2c0d8bd commit 065e711

12 files changed

+120
-35
lines changed

package.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"eslint-config-ruiaraujo-react": "^3.1.25",
6666
"eslint-plugin-react-hooks": "^4.6.2",
6767
"eslint-plugin-react-refresh": "^0.4.6",
68+
"mui-tel-input": "^5.1.2",
6869
"publint": "^0.2.7",
6970
"react": "^18.3.1",
7071
"react-dom": "^18.3.1",
@@ -75,11 +76,12 @@
7576
"vite-plugin-eslint": "^1.8.1"
7677
},
7778
"peerDependencies": {
78-
"@emotion/react": ">=11",
79-
"@emotion/styled": ">=11",
80-
"@mui/material": ">=5",
81-
"react": ">=18",
82-
"react-dom": ">=18",
83-
"react-hook-form": ">=7"
79+
"@emotion/react": "^11.11.4",
80+
"@emotion/styled": "^11.11.5",
81+
"@mui/material": "^5.15.16",
82+
"mui-tel-input": "^5.1.2",
83+
"react": "^18.3.1",
84+
"react-dom": "^18.3.1",
85+
"react-hook-form": "^7.51.4"
8486
}
8587
}

pnpm-lock.yaml

Lines changed: 36 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/AutocompleteController.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Autocomplete, CircularProgress, TextField } from '@mui/material';
22
import { Controller } from 'react-hook-form';
3-
import { useAsyncFieldControllerLabels, useFieldControllerLabels, useFieldControllerWithOptionsLabels } from '../index';
3+
import { useAsyncFieldControllerLabels, useFieldControllerLabels, useFieldControllerWithOptionsLabels } from '../hooks';
44
import { useState } from 'react';
55
import type {
66
AsyncFieldControllerProps,
77
FieldControllerProps,
88
FieldControllerWithOptionsProps,
99
ObjectLike,
10-
} from '../index';
10+
} from '../types';
1111
import type { AutocompleteProps, AutocompleteValue, ChipTypeMap, TextFieldProps } from '@mui/material';
1212
import type { FieldValues } from 'react-hook-form';
1313

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { FieldValues, useController } from 'react-hook-form';
2+
import { useFieldControllerLabels, useOnErrorMessage } from '../hooks';
3+
import { DistributiveOmit, FieldControllerProps } from '../types';
4+
import { MuiTelInput, type MuiTelInputProps } from 'mui-tel-input';
5+
6+
export type PhoneInputControllerProps<FV extends FieldValues> = FieldControllerProps<FV> &
7+
DistributiveOmit<
8+
MuiTelInputProps,
9+
'onChange' | 'error' | 'helperText' | 'label' | 'name' | 'disabled' | 'value' | 'onBlur'
10+
>;
11+
12+
export const PhoneInputController = <FV extends FieldValues>({
13+
control,
14+
label,
15+
name,
16+
optional = false,
17+
requiredLabel,
18+
onErrorMessage,
19+
disabled = false,
20+
...muiTelInputProps
21+
}: PhoneInputControllerProps<FV>) => {
22+
const { fieldControllerLabel } = useFieldControllerLabels({ label, optional, requiredLabel });
23+
const { fieldOnErrorMessage } = useOnErrorMessage({ onErrorMessage });
24+
25+
const {
26+
field: { onChange, ref, ...restField },
27+
fieldState: { invalid, error },
28+
} = useController({
29+
control,
30+
disabled,
31+
name,
32+
});
33+
34+
return (
35+
<MuiTelInput
36+
{...restField}
37+
defaultCountry='PT'
38+
aria-required={optional ? 'false' : 'true'}
39+
error={invalid}
40+
helperText={error?.message ? fieldOnErrorMessage(error?.message) : null}
41+
inputRef={ref}
42+
ref={ref}
43+
label={fieldControllerLabel}
44+
onChange={(_, info) => {
45+
onChange(info.numberValue);
46+
}}
47+
sx={{
48+
...muiTelInputProps?.sx,
49+
'.MuiTelInput-FlagImg': {
50+
borderRadius: (theme) => theme.shape.borderRadius / 4,
51+
},
52+
}}
53+
{...muiTelInputProps}
54+
/>
55+
);
56+
};

src/components/SelectController.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import {
1010
Typography,
1111
} from '@mui/material';
1212
import { Controller } from 'react-hook-form';
13-
import { useAsyncFieldControllerLabels, useFieldControllerLabels, useFieldControllerWithOptionsLabels } from '../index';
13+
import { useAsyncFieldControllerLabels, useFieldControllerLabels, useFieldControllerWithOptionsLabels } from '../hooks';
1414
import { useMemo } from 'react';
1515
import type {
1616
AsyncFieldControllerProps,
1717
FieldControllerProps,
1818
FieldControllerWithOptionsProps,
1919
ObjectLike,
20-
} from '../index';
20+
} from '../types';
2121
import type {
2222
CircularProgressProps,
2323
FormControlProps,

src/components/TextFieldController.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TextField as MuiTextField } from '@mui/material';
22
import { useController } from 'react-hook-form';
33
import { useFieldControllerLabels, useOnErrorMessage } from '../hooks';
4-
import type { FieldControllerProps } from '../index';
4+
import type { FieldControllerProps } from '../types';
55
import type { FieldValues } from 'react-hook-form';
66
import type { TextFieldProps } from '@mui/material';
77

src/components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './TextFieldController';
22
export * from './SelectController';
33
export * from './AutocompleteController';
4+
export * from './PhoneInputController';

src/hooks/useAsyncFieldControllerLabels.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useMemo } from 'react';
2-
import { useMuiFormConfig } from '../index';
3-
import type { AsyncFieldControllerProps } from '../index';
2+
import { useMuiFormConfig } from '../providers';
3+
import type { AsyncFieldControllerProps } from '../types';
44

55
interface Props extends Omit<AsyncFieldControllerProps, 'isLoading'> {}
66

src/hooks/useFieldControllerLabels.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useMemo } from 'react';
2-
import { useMuiFormConfig } from '../index';
3-
import type { FieldControllerProps } from '../index';
2+
import { useMuiFormConfig } from '../providers';
3+
import type { FieldControllerProps } from '../types';
44
import type { FieldValues } from 'react-hook-form';
55

66
interface Props extends Pick<FieldControllerProps<FieldValues>, 'requiredLabel'> {

src/hooks/useFieldControllerWithOptionsLabels.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useMemo } from 'react';
2-
import { useMuiFormConfig } from '../index';
3-
import type { FieldControllerWithOptionsProps, ObjectLike } from '../index';
2+
import { useMuiFormConfig } from '../providers';
3+
import type { FieldControllerWithOptionsProps, ObjectLike } from '../types';
44

55
interface Props extends Pick<FieldControllerWithOptionsProps<ObjectLike>, 'noOptionsLabel'> {}
66

0 commit comments

Comments
 (0)