Skip to content

Commit 4e1ac8d

Browse files
committed
chore: remove ref and additional props from components when as is areact fragment
1 parent 4d727c1 commit 4e1ac8d

File tree

13 files changed

+156
-17
lines changed

13 files changed

+156
-17
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
All notable changes to the react-smart-conditional package will be documented in this file.
44

5+
## [1.0.3] - 2025-03-05
6+
7+
### Fixed
8+
9+
- Excluded `ref` and unsupported props when `as` is a `React.Fragment` to prevent React warnings and errors.
10+
511
## [1.0.1] - 2024-09-11
612

713
### Changes
814

9-
- Updated github link in README.md
15+
- Updated GitHub link in README.md
1016

1117
## [1.0.0] - 2024-09-07
1218

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![License](https://img.shields.io/github/license/oluwatunmiisheii/react-smart-conditional?logo=github&logoColor=959DA5&labelColor=2D3339)](https://github.com/oluwatunmiisheii/react-smart-conditional/blob/main/LICENSE)
44
[![Contact](https://img.shields.io/badge/contact-@__Adenugawilson-blue.svg?style=flat&logo=twitter)](https://x.com/Adenugawilson)
55

6-
# React Conditional Render
6+
# React Smart Conditional
77

88
A flexible and reusable React component for conditional rendering.
99

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-smart-conditional",
33
"description": "Manage conditional rendering in react js and it's frameworks like a pro",
4-
"version": "1.0.21",
4+
"version": "1.0.3",
55
"source": "src/index.ts",
66
"main": "dist/index.js",
77
"module": "dist/index.module.js",
@@ -19,7 +19,7 @@
1919
},
2020
"sideEffects": false,
2121
"scripts": {
22-
"lint": "eslint '**/*.{js,ts,tsx}'",
22+
"lint": "eslint '**/*.{js,ts,tsx}' --ignore-pattern 'dist/'",
2323
"lint:fix": "pnpm lint --fix",
2424
"prettier": "prettier --write \"{src,tests,example/src}/**/*.{js,ts,jsx,tsx}\"",
2525
"build-only": "rm -rf ./dist/*; microbundle build --entry src/index.ts --name react-smart-conditional --tsconfig tsconfig.json",
@@ -35,7 +35,7 @@
3535
"react",
3636
"conditional",
3737
"render",
38-
"react-smart-conditional",
38+
"conditional-render",
3939
"react-conditional-render"
4040
],
4141
"author": "Wilson Adenuga <[email protected]>",

src/components/else.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import React from 'react';
22
import { polymorphicForwardRef } from '../types/polymorphic';
3+
import { isFragment } from '../utils/is-fragment';
34

4-
export const Else = polymorphicForwardRef<'div'>(
5-
({ as: Element = 'div', ...props }, ref) => <Element ref={ref} {...props} />,
5+
export const Else = polymorphicForwardRef<'div', JSX.IntrinsicElements['div']>(
6+
({ as: Element = 'div', ...props }, ref) => {
7+
return isFragment(Element) ? (
8+
props.children
9+
) : (
10+
<Element ref={ref} {...props} />
11+
);
12+
},
613
);
714
Else.displayName = 'Else';

src/components/if.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import React from 'react';
22
import { polymorphicForwardRef } from '../types/polymorphic';
3+
import { isFragment } from '../utils/is-fragment';
34

4-
export const If = polymorphicForwardRef<'div', { condition: boolean }>(
5-
({ as: Element = 'div', condition, ...props }, ref) =>
6-
condition ? <Element ref={ref} {...props} /> : null,
5+
export const If = polymorphicForwardRef<
6+
'div',
7+
JSX.IntrinsicElements['div'] & { condition: boolean }
8+
>(({ as: Element = 'div', condition, ...props }, ref) =>
9+
condition ? (
10+
isFragment(Element) ? (
11+
props.children
12+
) : (
13+
<Element ref={ref} {...props} />
14+
)
15+
) : null,
716
);
817
If.displayName = 'If';

src/components/show.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { type ReactNode, Children, isValidElement } from 'react';
22
import { If } from './if';
33
import { Else } from './else';
44
import { polymorphicForwardRef } from '../types/polymorphic';
5+
import { isFragment } from '../utils/is-fragment';
56

67
type ConditionalComponent = typeof If | typeof Else;
78

@@ -30,13 +31,18 @@ const Show = polymorphicForwardRef<
3031
}
3132
});
3233

33-
return (
34+
const content =
35+
trueConditions.length > 0
36+
? multiple
37+
? trueConditions
38+
: trueConditions[0]
39+
: Otherwise;
40+
41+
return isFragment(Element) ? (
42+
content
43+
) : (
3444
<Element ref={ref} {...props}>
35-
{trueConditions.length > 0
36-
? multiple
37-
? trueConditions
38-
: trueConditions[0]
39-
: Otherwise}
45+
{content}
4046
</Element>
4147
);
4248
});

src/utils/is-fragment.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Fragment } from 'react';
2+
3+
export const isFragment = (Component: React.ElementType) =>
4+
Component === Fragment;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Else should not pass ref and props when custom component is a Fragment 1`] = `
4+
<div>
5+
Hello
6+
</div>
7+
`;
8+
9+
exports[`Else should pass additional props 1`] = `
10+
<div>
11+
<div
12+
class="test"
13+
id="test"
14+
>
15+
Hello
16+
</div>
17+
</div>
18+
`;

test/else/else.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,25 @@ describe('Else', () => {
2020
render(<Else ref={ref}>Hello</Else>);
2121
expect(ref.current).toBeInTheDocument();
2222
});
23+
24+
it('should pass additional props', () => {
25+
const { container } = render(
26+
<Else className="test" id="test">
27+
Hello
28+
</Else>,
29+
);
30+
expect(container).toMatchSnapshot();
31+
});
32+
33+
it('should not pass ref and props when custom component is a Fragment', () => {
34+
const ref = React.createRef<HTMLDivElement>();
35+
36+
const { container } = render(
37+
<Else as={React.Fragment} ref={ref}>
38+
Hello
39+
</Else>,
40+
);
41+
expect(ref.current).toBeNull();
42+
expect(container).toMatchSnapshot();
43+
});
2344
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`If should not pass ref and props when custom component is a Fragment 1`] = `
4+
<div>
5+
Hello
6+
</div>
7+
`;
8+
9+
exports[`If should pass additional props 1`] = `
10+
<div>
11+
<div
12+
class="test"
13+
id="test"
14+
>
15+
Hello
16+
</div>
17+
</div>
18+
`;

0 commit comments

Comments
 (0)