-
-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2d): add withDefaults helper (#982)
- Loading branch information
1 parent
d426508
commit 6bd072a
Showing
2 changed files
with
27 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './CanvasUtils'; | ||
export * from './is'; | ||
export * from './withDefaults'; |
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 {FunctionComponent, NodeConstructor, PropsOf} from '../components'; | ||
|
||
/** | ||
* Create a higher order component with default props. | ||
* | ||
* @example | ||
* ```tsx | ||
* const MyTxt = withDefaults(Txt, { | ||
* fill: '#f3303f', | ||
* }); | ||
* | ||
* // ... | ||
* | ||
* view.add(<MyTxt>Hello, World!</MyTxt>); | ||
* ``` | ||
* | ||
* @param component - The base class or function component to wrap. | ||
* @param defaults - The default props to apply. | ||
*/ | ||
export function withDefaults<T extends FunctionComponent | NodeConstructor>( | ||
component: T, | ||
defaults: PropsOf<T>, | ||
) { | ||
const Node = component; | ||
return (props: PropsOf<T>) => <Node {...defaults} {...props} />; | ||
} |