Skip to content

Commit

Permalink
feat: support @uiw/react-only-when/if.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jun 8, 2023
1 parent c315024 commit a052b5d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 43 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ React component that renders the children if the `condition` prop is `true`.

```jsx
import { If } from '@uiw/react-only-when';
// Or
import { If } from '@uiw/react-only-when/if'

<div>
<If
Expand Down
10 changes: 10 additions & 0 deletions if.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

declare module '@uiw/react-only-when/if' {
import { ReactElement } from "react";
import { FC, PropsWithChildren } from "react";
export interface IfProps {
readonly condition?: boolean;
readonly render?: () => ReactElement;
}
export const If: FC<PropsWithChildren<IfProps>>;
}
17 changes: 15 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,25 @@
"description": "A declarative component for conditional rendering.",
"main": "cjs/index.js",
"module": "esm/index.js",
"exports": {
"./README.md": "./README.md",
".": {
"import": "./esm/index.js",
"require": "./cjs/index.js",
"types": "./esm/index.d.ts"
},
"./if": {
"import": "./esm/If.js",
"require": "./cjs/If.js",
"types": "./esm/If.d.ts"
}
},
"scripts": {
"prepack": "npm run build",
"doc": "kkt build --app-src ./website",
"start": "kkt start --app-src ./website",
"watch": "tsbb watch src/*tsx --use-babel --cjs cjs",
"build": "tsbb build src/*tsx --use-babel --cjs cjs",
"watch": "tsbb watch src/*.tsx --use-babel --cjs cjs",
"build": "tsbb build src/*.tsx --use-babel --cjs cjs",
"prettier": "prettier --write \"**/*.{js,jsx,tsx,ts,less,md,json}\"",
"test": "kkt test --env=jsdom --app-src=./website",
"test:coverage": "kkt test --env=jsdom --coverage --app-src=./website"
Expand Down
43 changes: 43 additions & 0 deletions src/__test__/If.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-disable jest/no-conditional-expect */
import TestRenderer from 'react-test-renderer';
import { If } from '../';

describe('<If />', () => {

it('Not rendering children', () => {
const component = TestRenderer.create(
<If condition={false}>
<span id="child" />
</If>,
);
let only = component.toJSON();
expect(only).toBeNull();
});

it('rendering children', () => {
const component = TestRenderer.create(
<If condition={true}>
<span id="child" />
</If>,
);
let only = component.toJSON();

if (only && !Array.isArray(only)) {
expect(only.type).toEqual('span');
expect(only.props.id).toEqual('child');
}
});

it('render props', () => {
const component = TestRenderer.create(
<If condition={true} render={() => <span id="child" />} />,
);
let only = component.toJSON();

if (only && !Array.isArray(only)) {
expect(only.type).toEqual('span');
expect(only.props.id).toEqual('child');
}
});

})
41 changes: 0 additions & 41 deletions src/__test__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,47 +1,6 @@
/* eslint-disable jest/no-conditional-expect */
import TestRenderer from 'react-test-renderer';
import Only from '../';
import { If } from '../';

describe('<If />', () => {

it('Not rendering children', () => {
const component = TestRenderer.create(
<If condition={false}>
<span id="child" />
</If>,
);
let only = component.toJSON();
expect(only).toBeNull();
});

it('rendering children', () => {
const component = TestRenderer.create(
<If condition={true}>
<span id="child" />
</If>,
);
let only = component.toJSON();

if (only && !Array.isArray(only)) {
expect(only.type).toEqual('span');
expect(only.props.id).toEqual('child');
}
});

it('render props', () => {
const component = TestRenderer.create(
<If condition={true} render={() => <span id="child" />} />,
);
let only = component.toJSON();

if (only && !Array.isArray(only)) {
expect(only.type).toEqual('span');
expect(only.props.id).toEqual('child');
}
});

})

describe('<Only />', () => {
it('Not rendering children', () => {
Expand Down

0 comments on commit a052b5d

Please sign in to comment.