Skip to content

Commit

Permalink
fix: Refactor PhoneInputRoot component in phone-input.tsx to improve …
Browse files Browse the repository at this point in the history
…code readability and maintainability
  • Loading branch information
iPagar committed May 1, 2024
1 parent da1bc6f commit 7a00cf0
Show file tree
Hide file tree
Showing 12 changed files with 450 additions and 306 deletions.
1 change: 1 addition & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const config = {
'@storybook/addon-essentials',
'@storybook/addon-onboarding',
'@storybook/addon-interactions',
'@storybook/addon-jest',
],
framework: {
name: '@storybook/react-vite',
Expand Down
152 changes: 97 additions & 55 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@
"@semantic-release/release-notes-generator": "^12.1.0",
"@storybook/addon-essentials": "^7.6.12",
"@storybook/addon-interactions": "^7.6.12",
"@storybook/addon-jest": "^8.0.9",
"@storybook/addon-links": "^7.6.12",
"@storybook/addon-onboarding": "^1.0.11",
"@storybook/blocks": "^7.6.12",
"@storybook/icons": "^1.2.9",
"@storybook/react": "^7.6.12",
"@storybook/react-vite": "^7.6.12",
"@storybook/test": "^7.6.12",
"@testing-library/jest-dom": "^6.4.2",
"@testing-library/react": "^15.0.5",
"@types/jest": "^29.5.12",
"@types/react-dom": "^18.2.18",
Expand All @@ -68,8 +71,8 @@
"lint-staged": "^15.2.1",
"prettier": "^3.2.4",
"prop-types": "^15.8.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-hook-form": "^7.50.0",
"react-spring": "^9.7.3",
"react-test-renderer": "^18.3.1",
Expand Down
30 changes: 30 additions & 0 deletions src/phone-input/phone-input-item.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import clsx from 'clsx';
import { CountryCode, getCountryCallingCode } from 'libphonenumber-js';
import React, { useCallback } from 'react';
import { UseControllerProps, useController } from 'react-hook-form';

import styles from './phone-input.module.scss';
import { usePhoneInput } from './phone-input-provider';
Expand Down Expand Up @@ -52,6 +54,34 @@ export function PhoneInputItem(
);
}

export function PhoneInputItemWithForm(
props: React.LiHTMLAttributes<HTMLLIElement> & {
country: string;
} & UseControllerProps
) {
const { country, name } = props;
const {
field: { ref, ...fieldProps },
} = useController({
...props,
});

return (
<PhoneInputItem
{...props}
{...fieldProps}
onClick={() => {
fieldProps.onChange({
target: {
name,
value: `+${getCountryCallingCode(country as CountryCode)}`,
},
});
}}
/>
);
}

// Расширение типов пропсов для хука
type UsePhoneInputItemProps = {
selected: string;
Expand Down
Loading

0 comments on commit 7a00cf0

Please sign in to comment.