import React, { ComponentType, Fragment, FunctionComponent, Key, ReactNode, createElement, isValidElement, } from "react"; import { SetOptionalFn } from "../types/Fn"; import { Hoc } from "../types/Hoc"; import { newHoc } from "../utils/newHoc"; interface Options { /** * @default "i" * * @example * // ↓ "someName" here as well * const Component = withForEach(3, {indexName: "someName"})(Example) * * // is equivalent to * <> * // ↓ "someName" here as well * * * * */ indexName?: IndexName; /** * @default "children" * * @example * const Component = withForEach( * ["a", "b"], * // ↓ "someName" here as well * {valueName: "someName"} * )(Example) * * // is equivalent to * <> * // ↓ "someName" here as well * * * */ valueName?: ValueName; /** * @default (props) => props[indexName] // which is props.i * * @example * // makes key = 2^x * const Component = withForEach(4, {key: ({i}) => 2 ** i})(Example) * * // is equivalent to * <> * // ↓ note key values * * * * * */ key?: (props: object) => Key; } interface WithForEachHoc { ( target: number | Array | object, options?: Options, ): Hoc< [ SetOptionalFn< | ([IndexName & ""] extends [never] ? IndexName : "i") | ([ValueName & ""] extends [never] ? ValueName : "children") >, ] >; } const isReactNode = (value: unknown): boolean => isValidElement(value) || typeof value !== "object" || value === null; /** * Like Array.map but for React components. * Concatenates components for each item in array, range or object * * @experimental It needs to discuss its name * * @example * const Component = withForEach(3)(Example) * * // is equivalent to * <> * * * * * * @example * const Component = withForEach(["a", "b"])(Example) * * // is equivalent to * <> * a * b * * * @example * const Component = withForEach({ * a: 100, * b: 200, * })(Example) * * // is equivalent to * <> * 100 * 200 * */ export const withForEach = newHoc(function withForEach( Component: ComponentType, target: number | Array | object, { indexName = "i", key = (props: any): Key => props[indexName], valueName = "children", } = {}, ): FunctionComponent { function componentWithKey(newProps: any): ReactNode { if (valueName === "children") { const { children, ...rest } = newProps; return ( {children} ); } return ; } return function WithForEach(props: any): ReactNode { const children = ((): ReactNode[] => { if (typeof target === "number") { return Array.from({ length: target }).map((_v, i) => componentWithKey({ ...props, [indexName]: i }), ); } if (Array.isArray(target)) { if (target.length === 0) { return []; } if (isReactNode(target[0])) { return target.map((v, i) => componentWithKey({ ...props, [indexName]: i, [valueName]: v }), ); } return target.map((v, i) => componentWithKey({ ...v, ...props, [indexName]: i }), ); } if (typeof target === "object" && target !== null) { if (Object.keys(target).length === 0) { return []; } if (isReactNode(Object.values(target)[0])) { return Object.entries(target).map(([index, value]) => { return componentWithKey({ ...props, [indexName]: index, [valueName]: value, }); }); } return Object.entries(target).map(([index, value]) => { return componentWithKey({ ...value, ...props, [indexName]: index }); }); } if (process.env.NODE_ENV !== "production") { const never: never = target; never; } throw new Error( "withForEach: should be used with number, Array or object", ); })(); return createElement(Fragment, null, ...children); }; });