react-with-hoc
Version:
Work with React and HOCs (Higher-Order Components)
37 lines • 1.82 kB
TypeScript
import { DependencyNames } from "../types/DependencyNames";
import { Fn, HasAllPropsFn, IfThenFn, IntersectionFn, KeepNeversFn, OmitFn, Pipe, ReplaceFn, ToSchema } from "../types/Fn";
import { Hoc } from "../types/Hoc";
interface WithFactoryFn<PropName extends string, PropValue, DependencyProps extends [string | number | symbol, any]> extends Fn {
return: Pipe<this["arg0"], [
IfThenFn<HasAllPropsFn<PropName>, [
IntersectionFn<[PropName, PropValue]>,
KeepNeversFn<OmitFn<PropName>>
]>,
KeepNeversFn<ReplaceFn<DependencyProps>>
]>;
}
type WithFactoryHoc = <PropValue, PropName extends string, DependencyProps extends object, TDependencyName extends DependencyNames<DependencyProps> = DependencyNames<DependencyProps>>(propName: PropName, factory: (props: DependencyProps) => PropValue, dependencyNames: TDependencyName) => Hoc<[WithFactoryFn<PropName, PropValue, ToSchema<DependencyProps>>]>;
/**
* add value to a prop based on a function and/or other props
* @see {@link withDefaults} {@link withDefault} {@link withOverride} {@link withOverrides}
* @example
* function Card({ content, icon, border }: {
* content: string;
* icon?: "star" | "heart" | ...;
* }) {
* return <>...</>
* }
* const StandardCard = withFactory<{content: string; icon?: Icon}>("icon", ({content, icon}) => content.includes("star") ? "star" : icon)(Card);
*
* <StandardCard content="some content with star" icon="heart" />
* // is equivalent to
* <CardWithIcon content="some content with star" icon="star" />
*
* // and
* <StandardCard content="some content" icon="heart" />
* // is equivalent to
* <CardWithIcon content="some content" icon="heart" />
*/
export declare const withFactory: WithFactoryHoc;
export {};
//# sourceMappingURL=withFactory.d.ts.map