import React, { ComponentType, FunctionComponent } from "react"; import { Fn, FromSchema, IntersectionFn, Pipe, SetOptionalFn, } from "../types/Fn"; import { Hoc } from "../types/Hoc"; import { newHoc } from "../utils/newHoc"; interface WithSpreadFn extends Fn { return: Pipe< this["arg0"], [ IntersectionFn< [PropName, FromSchema>] >, SetOptionalFn, ] >; } interface WithSpreadHoc { ( propName: PropName, ): Hoc<[WithSpreadFn]>; } /** * Start receiving an object and spread all properties to Component * * @experimental It uses the same mechanism as {@link withDefaults} but it could also have the same mechanism as {@link withOverrides} * * @example * function Profile({ name, age, city }: {...}) { * ... * } * const NewProfile = withSpread<"profile", "name" | "age" | "city">("profile")(Profile) * * // is equivalent to * */ export const withSpread = newHoc(function withSpread( Component: ComponentType, key: string, ): FunctionComponent { return function WithObject(props: any): JSX.Element { const newProps: any = { ...props[key], ...props, }; return ; }; });