Skip to content

Commit

Permalink
Refactor processChildren function in phone-input.tsx to handle child …
Browse files Browse the repository at this point in the history
…elements with a key parameter.
  • Loading branch information
iPagar committed Apr 11, 2024
1 parent 67e54b7 commit 929b2bb
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/phone-input/phone-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,22 +111,29 @@ export function PhoneInputRoot(
}, [onPhoneChange, state.phoneNumber]);

// Функция для обработки children
const processChildren = (child: ChildrenFunc | React.ReactNode) => {
const processChildren = (
child: ChildrenFunc | React.ReactNode,
key?: number
) => {
if (typeof child === 'function') {
return child({
const redactored = child({
country: state.country,
countryList: state.countryList,
key,
onPhoneChange: (e) => state.handlePhoneNumberChange(e.target.value),
open: isOpen,
phone: state.phoneNumber,
});

return React.cloneElement(redactored, { key });
}
return child;

return React.cloneElement(child, { key });
};

// Обработка children, если это массив или одиночный элемент/функция
const renderedChildren = Array.isArray(children)
? children.map((child) => processChildren(child))
? children.map((child, index) => processChildren(child, index))
: processChildren(children);

return (
Expand Down

0 comments on commit 929b2bb

Please sign in to comment.