-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(src/utils): add utils to wrap components and reduce generic typi…
…ng boilerplate
- Loading branch information
1 parent
a17f2ae
commit 8885efb
Showing
3 changed files
with
34 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './pattern-match'; | ||
export * from './not'; |
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 @@ | ||
export { not } from 'ts-pattern'; |
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,31 @@ | ||
import { ComponentType } from 'react'; | ||
|
||
import { | ||
Match, | ||
MatchProps, | ||
When, | ||
WhenProps, | ||
With, | ||
WithProps, | ||
Otherwise, | ||
} from '../components'; | ||
|
||
export const getPatternMatch = <Shape, Strict extends boolean>( | ||
// @ts-ignore | ||
// Reason: we want to enable developers to pass a parameter instead of a generic type. | ||
value?: Shape, | ||
// @ts-ignore | ||
// Reason: we want to enable developers to pass a parameter instead of a generic type. | ||
strict?: boolean | ||
) => { | ||
const TypedMatch = Match as ComponentType<MatchProps<Shape>>; | ||
const TypedWhen = When as ComponentType<WhenProps<Shape>>; | ||
const TypedWith = With as ComponentType<WithProps<Shape, Strict>>; | ||
|
||
return { Match: TypedMatch, When: TypedWhen, With: TypedWith, Otherwise }; | ||
}; | ||
|
||
export const usePatternMatch = <Shape, Strict extends boolean>( | ||
value?: Shape, | ||
strict?: boolean | ||
) => getPatternMatch<Shape, Strict>(value, strict); |