Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ComponentType } from 'react';

/** Generic properties applied to a Compound component */
interface CompoundComponentProperties {
interface CompoundComponentStaticProperties {
displayName: string;
[key: string]: any; // Typed as `any` to avoid issues with a mapped object
}

/** Return type of a CompoundComponent */
export type CompoundComponentType<
Props extends {} = {},
Properties extends CompoundComponentProperties = CompoundComponentProperties,
> = ComponentType<Props> & Properties;
StaticProperties extends CompoundComponentStaticProperties = CompoundComponentStaticProperties,
> = ComponentType<Props> & StaticProperties;

/**
* Factory function used to create a compound component parent.
Expand All @@ -26,15 +26,15 @@ export type CompoundComponentType<
* <MyComponent.SubComponent /> // Renders <MySubComponent />
* ```
* @param componentRenderFn The component render function
* @param properties Object describing the `displayName` and any sub-components
* @param staticProperties Object describing the `displayName` and any sub-components
* @returns {CompoundComponentType}
*/
export const CompoundComponent = <
Props extends {} = {},
Properties extends CompoundComponentProperties = CompoundComponentProperties,
StaticProperties extends CompoundComponentStaticProperties = CompoundComponentStaticProperties,
>(
componentRenderFn: ComponentType<Props>,
properties: Properties,
): CompoundComponentType<Props, Properties> => {
return Object.assign(componentRenderFn, properties);
staticProperties: StaticProperties,
): CompoundComponentType<Props, StaticProperties> => {
return Object.assign(componentRenderFn, staticProperties);
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ComponentType } from 'react';

interface SubComponentProperties<Key extends string> {
interface SubComponentStaticProperties<Key extends string> {
displayName: string;
key: Key;
}
Expand All @@ -26,14 +26,14 @@ export type SubComponentType<
* ```
*
* @param componentRenderFn The component render function
* @param properties Object describing the `displayName` and `key`
* @param staticProperties Object describing the `displayName` and `key`
* @returns {SubComponentType}
*/
export const CompoundSubComponent = <Key extends string, Props extends {} = {}>(
componentRenderFn: ComponentType<Props>,
properties: SubComponentProperties<Key>,
staticProperties: SubComponentStaticProperties<Key>,
): SubComponentType<Key, Props> => {
const { key, ...rest } = properties;
const { key, ...rest } = staticProperties;
return Object.assign(componentRenderFn, {
...rest,
[key]: true,
Expand Down
Loading