Skip to content

Commit

Permalink
Rendering array children when no conditional node is present
Browse files Browse the repository at this point in the history
  • Loading branch information
brickpop committed Feb 26, 2024
1 parent d2e00a2 commit 0b92157
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# If-Then-Else

The missing conditional renderer that the React team forgot to ship.
The missing conditional renderer that the React team didn't ship.

Write clean UI components that can be read as a visual JS function.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.9.0",
"version": "0.10.0",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
16 changes: 15 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const If = (props: IfProps) => {
const mainCondition = resolveCondition(props);

// Many children
if (Array.isArray(children)) {
if (Array.isArray(children) && hasConditionalChildren(children)) {
for (const child of children) {
// Match the Then/ElseIf/Else elements only
if (child.type === Then) {
Expand Down Expand Up @@ -190,3 +190,17 @@ function resolveCondition(props: IfProps): boolean {
}
return !(props as any).not;
}

function hasConditionalChildren(children: ReactNode): boolean {
if (!Array.isArray(children)) {
return isConditionalChild(children as ReactElement);
}
for (const item of children) {
if (isConditionalChild(item)) return true;
}
return false;
}

function isConditionalChild(node: ReactElement) {
return node.type === Then || node.type === ElseIf || node.type === Else;
}
10 changes: 5 additions & 5 deletions test/main.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,22 @@ describe('If component', () => {

describe('Children array', () => {
describe('Extraneous children', () => {
it('Should ignore extraneous children', () => {
it('Should render pure non-conditional children', () => {
let actual = renderToString(
<If condition={true}>
<p>Hello</p>
<p>World</p>
</If>
);
expect(actual).toEqual('');
expect(actual).toEqual('<p>Hello</p><p>World</p>');

actual = renderToString(
<If condition={true}>
<p>Hello</p>
World
</If>
);
expect(actual).toEqual('');
expect(actual).toEqual('<p>Hello</p>World');

actual = renderToString(
<If condition={true}>
Expand All @@ -157,10 +157,10 @@ describe('If component', () => {
!
</If>
);
expect(actual).toEqual('');
expect(actual).toEqual('<ul><li>Hello</li><li>World</li></ul>!');
});

it('Should render only recognized logic nodes', () => {
it('Should only render conditional nodes when one or more present', () => {
// Then
let actual = renderToString(
<If condition={true}>
Expand Down

0 comments on commit 0b92157

Please sign in to comment.