UNPKG

react-with-hoc

Version:

Work with React and HOCs (Higher-Order Components)

32 lines 1.23 kB
import { HasAllPropsFn, IfThenFn, IntersectionFn, SetOptionalFn } from "../types/Fn"; import { Hoc } from "../types/Hoc"; type WithDefaultHoc = <PropValue, PropName extends string>(propName: PropName, value: PropValue) => Hoc<[ IfThenFn<HasAllPropsFn<PropName>, [ ...(PropValue extends (...args: any[]) => any ? [] : [IntersectionFn<[PropName, PropValue]>]), SetOptionalFn<PropName> ]> ]>; /** * 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); * * <StandardCard content="some content" /> * // is equivalent to * <CardWithIcon icon="star" content="some content" /> * * // but * <StandardCard content="some content" icon="heart" /> * // is equivalent to * <CardWithIcon content="some content" icon="heart" /> // it propagates the new value */ export declare const withDefault: WithDefaultHoc; export {}; //# sourceMappingURL=withDefault.d.ts.map