Skip to content

Commit

Permalink
feat(2d): add withDefaults helper (#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
aarthificial committed Mar 11, 2024
1 parent d426508 commit 6bd072a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/2d/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './CanvasUtils';
export * from './is';
export * from './withDefaults';
26 changes: 26 additions & 0 deletions packages/2d/src/lib/utils/withDefaults.tsx
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} />;
}

0 comments on commit 6bd072a

Please sign in to comment.