Skip to content

Commit a052b5d

Browse files
committed
feat: support @uiw/react-only-when/if.
1 parent c315024 commit a052b5d

File tree

5 files changed

+70
-43
lines changed

5 files changed

+70
-43
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ React component that renders the children if the `condition` prop is `true`.
2727

2828
```jsx
2929
import { If } from '@uiw/react-only-when';
30+
// Or
31+
import { If } from '@uiw/react-only-when/if'
3032

3133
<div>
3234
<If

if.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
declare module '@uiw/react-only-when/if' {
3+
import { ReactElement } from "react";
4+
import { FC, PropsWithChildren } from "react";
5+
export interface IfProps {
6+
readonly condition?: boolean;
7+
readonly render?: () => ReactElement;
8+
}
9+
export const If: FC<PropsWithChildren<IfProps>>;
10+
}

package.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,25 @@
44
"description": "A declarative component for conditional rendering.",
55
"main": "cjs/index.js",
66
"module": "esm/index.js",
7+
"exports": {
8+
"./README.md": "./README.md",
9+
".": {
10+
"import": "./esm/index.js",
11+
"require": "./cjs/index.js",
12+
"types": "./esm/index.d.ts"
13+
},
14+
"./if": {
15+
"import": "./esm/If.js",
16+
"require": "./cjs/If.js",
17+
"types": "./esm/If.d.ts"
18+
}
19+
},
720
"scripts": {
821
"prepack": "npm run build",
922
"doc": "kkt build --app-src ./website",
1023
"start": "kkt start --app-src ./website",
11-
"watch": "tsbb watch src/*tsx --use-babel --cjs cjs",
12-
"build": "tsbb build src/*tsx --use-babel --cjs cjs",
24+
"watch": "tsbb watch src/*.tsx --use-babel --cjs cjs",
25+
"build": "tsbb build src/*.tsx --use-babel --cjs cjs",
1326
"prettier": "prettier --write \"**/*.{js,jsx,tsx,ts,less,md,json}\"",
1427
"test": "kkt test --env=jsdom --app-src=./website",
1528
"test:coverage": "kkt test --env=jsdom --coverage --app-src=./website"

src/__test__/If.test.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-disable jest/no-conditional-expect */
2+
import TestRenderer from 'react-test-renderer';
3+
import { If } from '../';
4+
5+
describe('<If />', () => {
6+
7+
it('Not rendering children', () => {
8+
const component = TestRenderer.create(
9+
<If condition={false}>
10+
<span id="child" />
11+
</If>,
12+
);
13+
let only = component.toJSON();
14+
expect(only).toBeNull();
15+
});
16+
17+
it('rendering children', () => {
18+
const component = TestRenderer.create(
19+
<If condition={true}>
20+
<span id="child" />
21+
</If>,
22+
);
23+
let only = component.toJSON();
24+
25+
if (only && !Array.isArray(only)) {
26+
expect(only.type).toEqual('span');
27+
expect(only.props.id).toEqual('child');
28+
}
29+
});
30+
31+
it('render props', () => {
32+
const component = TestRenderer.create(
33+
<If condition={true} render={() => <span id="child" />} />,
34+
);
35+
let only = component.toJSON();
36+
37+
if (only && !Array.isArray(only)) {
38+
expect(only.type).toEqual('span');
39+
expect(only.props.id).toEqual('child');
40+
}
41+
});
42+
43+
})

src/__test__/index.test.tsx

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,6 @@
11
/* eslint-disable jest/no-conditional-expect */
22
import TestRenderer from 'react-test-renderer';
33
import Only from '../';
4-
import { If } from '../';
5-
6-
describe('<If />', () => {
7-
8-
it('Not rendering children', () => {
9-
const component = TestRenderer.create(
10-
<If condition={false}>
11-
<span id="child" />
12-
</If>,
13-
);
14-
let only = component.toJSON();
15-
expect(only).toBeNull();
16-
});
17-
18-
it('rendering children', () => {
19-
const component = TestRenderer.create(
20-
<If condition={true}>
21-
<span id="child" />
22-
</If>,
23-
);
24-
let only = component.toJSON();
25-
26-
if (only && !Array.isArray(only)) {
27-
expect(only.type).toEqual('span');
28-
expect(only.props.id).toEqual('child');
29-
}
30-
});
31-
32-
it('render props', () => {
33-
const component = TestRenderer.create(
34-
<If condition={true} render={() => <span id="child" />} />,
35-
);
36-
let only = component.toJSON();
37-
38-
if (only && !Array.isArray(only)) {
39-
expect(only.type).toEqual('span');
40-
expect(only.props.id).toEqual('child');
41-
}
42-
});
43-
44-
})
454

465
describe('<Only />', () => {
476
it('Not rendering children', () => {

0 commit comments

Comments
 (0)