import React, { ComponentType, FunctionComponent } from "react"; import { HasAllPropsFn, IfThenFn, IntersectionFn, SetOptionalFn, } from "../types/Fn"; import { Hoc } from "../types/Hoc"; import { newHoc } from "../utils/newHoc"; type WithDefaultHoc = ( propName: PropName, value: PropValue, ) => Hoc< [ IfThenFn< HasAllPropsFn, [ ...(PropValue extends (...args: any[]) => any ? [] : [IntersectionFn<[PropName, PropValue]>]), SetOptionalFn, ] >, ] >; /** * adds default value to a prop, so it can still receive a value but it now has a default one * @see {@link withDefaults} {@link withFactory} {@link withOverride} {@link withOverrides} * @example * function CardWithIcon({ content, icon }: { * content: string; * icon: "star" | "heart" | ...; * }) { * return <>... * } * const StandardCard = withDefault("icon", "star")(CardWithIcon); * * * // is equivalent to * * * // but * * // is equivalent to * // it propagates the new value */ export const withDefault = newHoc(function withDefault( Component: ComponentType, propName: string, value: any, ): FunctionComponent { return function WithDefault(props: any): JSX.Element { return ; }; });