Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Allow children and fallback props be render props
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoisseau committed Apr 11, 2020
1 parent c207c04 commit edfd50f
Show file tree
Hide file tree
Showing 38 changed files with 127 additions and 32 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# v1.0.7
- Switch
- Add debug props, if `debug={true}` console.warn if no `Case` match nor `Default`
- Case
- children props can now be a render props. `children={matchedValue => (<span>{matchedValue}</span>)}`
- Default
- children props can now be a render props. `children={() => (<span>Default</span>)}`
- Match
- children props can now be a render props. `children={matchValue => (<span>{matchValue}</span>)}`
- same for `fallback` props
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {Match, Switch, Case, Default} from 'react-logic-match'

class Example extends React.Component {
render () {
const error = new Error('msg in jsx');

return (
<>
<Match condition={'foo' === 'bar'}>
Expand All @@ -33,6 +35,9 @@ class Example extends React.Component {
<Match condition={'bar' === 'bar'}>
<p>'bar' === 'bar'</p>
</Match>
<Match condition={error}>{() => {
<p>{error.message}</p>
}</Match>

<Switch value={1}>
<Case value={0}>
Expand Down Expand Up @@ -67,6 +72,8 @@ See `<Switch value>-<Case value>-<Default>` as a simple js `switch-case-default`

For more info you can check the source code. It's strictly typed and not so much complicated

Since v1.0.7, children and fallbacks props can be render props (a function who return JSX)

## License

MIT © [tpoisseau](https://github.com/tpoisseau)
2 changes: 1 addition & 1 deletion dist/Case.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { ICaseProps } from "./ICase";
export default class Case<T> extends React.Component<ICaseProps<T>> {
render(): JSX.Element | (JSX.Element & string) | (JSX.Element & number) | (JSX.Element & false) | (JSX.Element & true) | (JSX.Element & React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)>) | (JSX.Element & React.ReactNodeArray) | (JSX.Element & React.ReactPortal);
render(): JSX.Element | JSX.Element[] | (((value: T) => JSX.Element | JSX.Element[]) & string) | (((value: T) => JSX.Element | JSX.Element[]) & number) | (((value: T) => JSX.Element | JSX.Element[]) & false) | (((value: T) => JSX.Element | JSX.Element[]) & true) | (((value: T) => JSX.Element | JSX.Element[]) & React.ReactNodeArray);
}
2 changes: 2 additions & 0 deletions dist/Case.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/Case.es.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dist/Case.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/Case.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/Default.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { IDefaultProps } from "./IDefault";
export default class Default extends React.Component<IDefaultProps> {
render(): JSX.Element | (JSX.Element & string) | (JSX.Element & number) | (JSX.Element & false) | (JSX.Element & true) | (JSX.Element & React.ReactElement<any, string | ((props: any) => React.ReactElement<any, string | any | (new (props: any) => React.Component<any, any, any>)> | null) | (new (props: any) => React.Component<any, any, any>)>) | (JSX.Element & React.ReactNodeArray) | (JSX.Element & React.ReactPortal);
render(): JSX.Element | JSX.Element[] | ((() => JSX.Element | JSX.Element[]) & string) | ((() => JSX.Element | JSX.Element[]) & number) | ((() => JSX.Element | JSX.Element[]) & false) | ((() => JSX.Element | JSX.Element[]) & true) | ((() => JSX.Element | JSX.Element[]) & React.ReactNodeArray);
}
2 changes: 2 additions & 0 deletions dist/Default.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/Default.es.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions dist/Default.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/Default.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion dist/ICase.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
/// <reference types="react" />
declare type JSXElements = JSX.Element | JSX.Element[];
export interface ICaseProps<T> {
value: T;
children: JSX.Element;
children: JSXElements | ((value: T) => JSXElements);
}
export {};
4 changes: 3 additions & 1 deletion dist/IDefault.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/// <reference types="react" />
declare type JSXElements = JSX.Element | JSX.Element[];
export interface IDefaultProps {
children: JSX.Element;
children: JSXElements | (() => JSXElements);
}
export {};
6 changes: 4 additions & 2 deletions dist/IMatch.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// <reference types="react" />
declare type JSXElements = JSX.Element | JSX.Element[];
export interface IMatchProps {
condition: boolean;
fallback?: JSX.Element | null | undefined;
children: JSX.Element;
fallback?: JSXElements | ((condition: boolean) => JSXElements) | null | undefined;
children: JSXElements | ((condition: boolean) => JSXElements);
}
export {};
1 change: 1 addition & 0 deletions dist/ISwitch.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export declare type SwitchChildren<T> = ReactElement<Case<T>> | ReactElement<Def
export interface ISwitchProps<T> {
value: T;
children: SwitchChildren<T> | SwitchChildren<T>[];
debug?: boolean;
}
2 changes: 1 addition & 1 deletion dist/Match.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/// <reference types="react" />
import { IMatchProps } from "./IMatch";
export default function Match({ condition, fallback, children }: IMatchProps): JSX.Element | null;
export default function Match({ condition, fallback, children }: IMatchProps): JSX.Element | JSX.Element[] | null;
7 changes: 6 additions & 1 deletion dist/Match.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/Match.es.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion dist/Match.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/Match.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion dist/Switch.es.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit edfd50f

Please sign in to comment.