Skip to content

Commit 5d90f50

Browse files
author
Hardik_B
committed
[REFACTORED] to reduce bundle size
1 parent d72ee65 commit 5d90f50

3 files changed

Lines changed: 8 additions & 29 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dx-kit/react-conditional-match",
3-
"version": "2.2.4",
3+
"version": "2.2.5",
44
"description": "A flexible and reusable React component for conditionally rendering child elements. The component supports options for handling multiple matching children and custom fallbacks, and is lightweight and easy to use. This component can help simplify complex rendering logic in your React application and remove the need for ternary statements and if else blocks.",
55
"main": "./dist/index.esm.js",
66
"module": "./dist/index.cjs.js",

src/components/ConditionalMatch/ConditionalMatch.tsx

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,17 @@ const ConditionalMatch = ({ fallback, children = null, multiMatch = false }: Con
1616
const childrenToRender: JSX.Element[] = [];
1717

1818
Children.forEach(children, (child) => {
19-
if (child?.type !== Render) {
19+
if (child?.type !== Render || (!multiMatch && childrenToRender?.length >= 1) || !child?.props?.when) {
2020
return;
2121
}
22-
23-
if (!multiMatch && childrenToRender?.length >= 1) {
24-
return;
25-
}
26-
27-
if (child?.props?.when) {
28-
childrenToRender.push(child);
29-
}
22+
childrenToRender.push(child);
3023
});
3124

3225
return childrenToRender;
3326
}, [children, multiMatch]);
3427

35-
if (matchedChildren?.length) {
36-
return <Fragment>{matchedChildren}</Fragment>;
37-
}
38-
39-
return <Fragment>{fallback}</Fragment>;
28+
return <Fragment>{matchedChildren?.length ? matchedChildren : fallback}</Fragment>;
4029
};
4130

42-
const ConditionalMatchCompound = Object.assign(ConditionalMatch, {
43-
Render,
44-
});
45-
46-
export { ConditionalMatchCompound as ConditionalMatch };
31+
ConditionalMatch.Render = Render;
32+
export { ConditionalMatch };

src/components/ConditionalMatch/components/Render.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,5 @@ type RenderProps<T> = {
77
children: ReactNode;
88
};
99

10-
function Render<T>({ children, when }: RenderProps<T>): JSX.Element | null {
11-
if (!when) {
12-
return null;
13-
}
14-
15-
return <Fragment>{children}</Fragment>;
16-
}
17-
18-
export { Render };
10+
export const Render = <T,>({ children, when }: RenderProps<T>): JSX.Element | null =>
11+
when ? <Fragment>{children}</Fragment> : null;

0 commit comments

Comments
 (0)