Skip to content

Commit d1edb7b

Browse files
authored
fix(tel-field) - fix tel field props (#980)
* fix(tel-field) - props * fix prop * fix types
1 parent 5e6bccf commit d1edb7b

3 files changed

Lines changed: 42 additions & 27 deletions

File tree

src/components/form/fields/TelField.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { FormField } from '@/src/components/ui/form';
22
import { useFormFields } from '@/src/context';
3-
import { Components, JSFField } from '@/src/types/remoteFlows';
3+
import { Components } from '@/src/types/remoteFlows';
44
import {
55
useFormContext,
66
ControllerFieldState,
77
ControllerRenderProps,
88
FieldValues,
99
} from 'react-hook-form';
10-
import { TelFieldComponentProps } from '@/src/types/fields';
10+
import { TelFieldComponentProps, TelFieldDataProps } from '@/src/types/fields';
1111
import { useMemo, useCallback, useState, useEffect, useRef } from 'react';
1212

1313
export type Country = {
@@ -271,20 +271,9 @@ export function TelFieldRenderer({
271271
);
272272
}
273273

274-
export type TelFieldDataProps = Omit<JSFField, 'options'> & {
275-
onChangeCountryCode?: (newCountry: Country) => void;
276-
onChangePhoneNumber?: (event: React.ChangeEvent<HTMLInputElement>) => void;
274+
export type TelFieldProps = TelFieldDataProps & {
275+
name: string;
277276
component?: Components['tel'];
278-
options: {
279-
value: string;
280-
label: string;
281-
meta: {
282-
countryCode: string;
283-
};
284-
pattern: string;
285-
}[];
286-
currentCountry?: Country;
287-
nationalPhoneNumber?: string;
288277
};
289278

290279
export function TelField({
@@ -295,7 +284,7 @@ export function TelField({
295284
onChangePhoneNumber,
296285
component,
297286
...rest
298-
}: TelFieldDataProps) {
287+
}: TelFieldProps) {
299288
const { components } = useFormFields();
300289
const { control } = useFormContext();
301290

src/components/form/fields/tests/TelField.test.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { render, screen, waitFor } from '@testing-library/react';
33
import userEvent from '@testing-library/user-event';
44
import { FormProvider, useForm } from 'react-hook-form';
55
import { object, string } from 'yup';
6-
import { TelField, TelFieldDataProps } from '../TelField';
6+
import { TelField, TelFieldProps } from '../TelField';
77
import { TelFieldDefault } from '../default/TelFieldDefault';
88
import { $TSFixMe } from '@/src/types/remoteFlows';
99
import { yupResolver } from '@hookform/resolvers/yup';
@@ -39,7 +39,7 @@ async function fillPhoneInput(phoneNumber: string) {
3939
await user.type(phoneInput, phoneNumber);
4040
}
4141

42-
const mockOptions: TelFieldDataProps['options'] = [
42+
const mockOptions: TelFieldProps['options'] = [
4343
{
4444
value: 'US',
4545
label: 'United States',
@@ -66,7 +66,7 @@ const mockOptions: TelFieldDataProps['options'] = [
6666
},
6767
];
6868

69-
const createTelSchema = (options: TelFieldDataProps['options']) => {
69+
const createTelSchema = (options: TelFieldProps['options']) => {
7070
return string()
7171
.required('Phone number is required')
7272
.max(30, 'Must be at most 30 characters')
@@ -96,7 +96,7 @@ const createTelSchema = (options: TelFieldDataProps['options']) => {
9696
};
9797

9898
describe('TelField Component - Split UI', () => {
99-
const defaultProps: TelFieldDataProps = {
99+
const defaultProps: TelFieldProps = {
100100
name: 'phoneNumber',
101101
label: 'Phone Number',
102102
description: 'Enter your phone number',
@@ -113,18 +113,20 @@ describe('TelField Component - Split UI', () => {
113113
};
114114

115115
const renderWithFormContext = (
116-
props: TelFieldDataProps,
116+
props: TelFieldProps,
117117
defaultValues?: $TSFixMe,
118118
) => {
119119
const TestComponent = () => {
120120
const methods = useForm({
121121
mode: 'onBlur',
122122
defaultValues: defaultValues || {},
123-
resolver: yupResolver(
124-
object().shape({
125-
phoneNumber: props.schema,
126-
}),
127-
),
123+
resolver: props.schema
124+
? yupResolver(
125+
object().shape({
126+
phoneNumber: props.schema,
127+
}),
128+
)
129+
: undefined,
128130
});
129131
return (
130132
<FormProvider {...methods}>

src/types/fields.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { FieldFileDataProps } from '@/src/components/form/fields/FileUploadField';
2-
import { TelFieldDataProps } from '@/src/components/form/fields/TelField';
32
import { DailySchedule } from '@/src/components/form/fields/workScheduleUtils';
43
import { JSFField } from '@/src/types/remoteFlows';
54
import {
@@ -144,6 +143,31 @@ export type PricingPlanComponentProps = Omit<
144143
fieldData: PricingPlanDataProps;
145144
};
146145

146+
export type TelFieldDataProps = Omit<FieldDataProps, 'options'> & {
147+
options: {
148+
value: string;
149+
label: string;
150+
meta: {
151+
countryCode: string;
152+
};
153+
pattern: string;
154+
}[];
155+
currentCountry?: {
156+
name: string;
157+
dialCode: string;
158+
pattern: string;
159+
areaCodes?: string[];
160+
};
161+
nationalPhoneNumber?: string;
162+
onChangeCountryCode?: (newCountry: {
163+
name: string;
164+
dialCode: string;
165+
pattern: string;
166+
areaCodes?: string[];
167+
}) => void;
168+
onChangePhoneNumber?: (event: React.ChangeEvent<HTMLInputElement>) => void;
169+
};
170+
147171
export type TelFieldComponentProps = Omit<FieldComponentProps, 'fieldData'> & {
148172
fieldData: TelFieldDataProps;
149173
};

0 commit comments

Comments
 (0)