-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed9c7ad
commit 2d4ad56
Showing
10 changed files
with
169 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { ReactElement, Children } from 'react'; | ||
import { FC, PropsWithChildren } from 'react'; | ||
|
||
export const Switch: FC<PropsWithChildren<{}>> = ({ children }) => { | ||
let matchChild: ReactElement | null = null; | ||
let defaultCase: ReactElement | null = null; | ||
Children.forEach(Children.toArray(children) as ReactElement<PropsWithChildren<CaseProps>>[], (child) => { | ||
if (!matchChild && child.type === Case) { | ||
const { condition } = child.props; | ||
const conditionIsTrue = Boolean(condition); | ||
if (conditionIsTrue) { | ||
matchChild = child; | ||
} | ||
} else if (!defaultCase && child.type === Default) { | ||
defaultCase = child; | ||
} | ||
}); | ||
return matchChild ?? defaultCase ?? null; | ||
}; | ||
|
||
export interface CaseProps { | ||
readonly condition?: boolean; | ||
} | ||
|
||
export const Case: FC<PropsWithChildren<CaseProps>> = ({ children }) => children; | ||
export const Default: FC<PropsWithChildren> = ({ children }) => children; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
declare module '@uiw/react-only-when/switch' { | ||
import { FC, PropsWithChildren } from 'react'; | ||
export const Switch: FC<PropsWithChildren<{}>>; | ||
export interface CaseProps { | ||
readonly condition?: boolean; | ||
} | ||
export const Case: FC<PropsWithChildren<CaseProps>>; | ||
export const Default: FC<PropsWithChildren>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* eslint-disable jest/no-conditional-expect */ | ||
import renderer from 'react-test-renderer'; | ||
import { Switch, Case, Default } from '../core/src/switch'; | ||
|
||
|
||
it('<Switch />', () => { | ||
const component = renderer.create( | ||
<Switch></Switch> | ||
); | ||
const only = component.toJSON(); | ||
expect(only).toBeNull(); | ||
}); | ||
|
||
it('<Default />', () => { | ||
const component = renderer.create( | ||
<Switch> | ||
<Default>you graduated</Default> | ||
</Switch> | ||
); | ||
const only = component.toJSON(); | ||
expect(only).toEqual('you graduated'); | ||
}); | ||
|
||
it('<Case />', () => { | ||
const component = renderer.create( | ||
<Switch> | ||
<Case condition={true}>preschool</Case> | ||
<Default>you graduated</Default> | ||
</Switch> | ||
); | ||
const only = component.toJSON(); | ||
expect(only).toEqual('preschool'); | ||
}); | ||
|
||
it('<Case /> condition=true', () => { | ||
const component = renderer.create( | ||
<Switch> | ||
<Case condition={true}>preschool</Case> | ||
<Case condition={true}>primary school</Case> | ||
<Default>you graduated</Default> | ||
</Switch> | ||
); | ||
const only = component.toJSON(); | ||
expect(only).toEqual('preschool'); | ||
}); | ||
|
||
it('<Case /> condition=false', () => { | ||
const component = renderer.create( | ||
<Switch> | ||
<Case condition={false}>preschool</Case> | ||
<Case condition={false}>primary school</Case> | ||
<Default>you graduated</Default> | ||
</Switch> | ||
); | ||
const only = component.toJSON(); | ||
expect(only).toEqual('you graduated'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters