UNPKG

@yamada-ui/react

Version:

React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion

1 lines 10.2 kB
{"version":3,"file":"children.cjs","names":["React","children","types"],"sources":["../../../src/utils/children.tsx"],"sourcesContent":["import {\n isArray,\n isEmpty,\n isNumber,\n isString,\n isUndefined,\n} from \"@yamada-ui/utils\"\nimport * as React from \"react\"\n\nexport function getValidChildren(\n children: React.ReactNode,\n): React.ReactElement<any>[] {\n return React.Children.toArray(children).filter((child) =>\n React.isValidElement(child),\n )\n}\n\nexport function useValidChildren(children: React.ReactNode) {\n return React.useMemo(() => getValidChildren(children), [children])\n}\n\nexport function isValidElement(child: any): child is React.ReactNode {\n return React.isValidElement(child) || isString(child) || isNumber(child)\n}\n\nexport function isSomeDisplayName(a: any, b: any): boolean {\n if (isUndefined(a) || isUndefined(b)) return false\n\n if (isArray(a)) {\n if (a.includes(b)) return true\n if (!!b.displayName && a.includes(b.displayName)) return true\n if (!!b.name && a.includes(b.name)) return true\n } else {\n if (a === b) return true\n if (!!a.displayName && !!b.displayName && a.displayName === b.displayName)\n return true\n if (!!a.name && !!b.name && a.name === b.name) return true\n if (!!a.displayName && !!b.name && a.displayName === b.name) return true\n if (!!a.name && !!b.displayName && a.name === b.displayName) return true\n }\n\n return false\n}\n\nexport function isSomeElement(a: any, b: any): boolean {\n if (isUndefined(a) || isUndefined(b)) return false\n if (a === b) return true\n if (isSomeDisplayName(a, b)) return true\n\n a = a._payload?.value\n\n if (isUndefined(a)) return false\n if (isSomeDisplayName(a, b)) return true\n\n return false\n}\n\nexport function findChild<Y = any>(\n children: React.ReactElement[],\n ...types: (React.JSXElementConstructor<any> | string)[]\n): React.ReactElement<Y> | undefined {\n const child = children.find((child) =>\n types.some((type) => isSomeElement(child.type, type)),\n )\n\n return child as React.ReactElement<Y> | undefined\n}\n\nexport function useFindChild<Y = any>(\n children: React.ReactElement[],\n ...types: (React.JSXElementConstructor<any> | string)[]\n): React.ReactElement<Y> | undefined {\n const typesRef = React.useRef(types)\n\n return React.useMemo(\n () => findChild<Y>(children, ...typesRef.current),\n [children],\n )\n}\n\nexport function findChildren<Y = any, M = any>(\n children: React.ReactElement[],\n ...types: (React.JSXElementConstructor<any> | string)[]\n): [React.ReactElement<Y> | undefined, ...React.ReactElement<M>[]] {\n const child = findChild(children, ...types)\n\n if (child) {\n return children.sort((a, b) => {\n if (types.some((type) => isSomeElement(a.type, type))) {\n return -1\n } else if (types.some((type) => isSomeElement(b.type, type))) {\n return 1\n } else {\n return 0\n }\n }) as [React.ReactElement<Y> | undefined, ...React.ReactElement<M>[]]\n } else {\n return [undefined, ...(children as React.ReactElement<M>[])]\n }\n}\n\nexport function useFindChildren<Y = any, M = any>(\n children: React.ReactElement<M>[],\n ...types: (React.JSXElementConstructor<any> | string)[]\n): [React.ReactElement<Y> | undefined, ...React.ReactElement<M>[]] {\n const typesRef = React.useRef(types)\n\n return React.useMemo(\n () => findChildren<Y, M>(children, ...typesRef.current),\n [children],\n )\n}\n\nexport function includesChildren(\n children: React.ReactElement<any>[],\n ...types: (React.JSXElementConstructor<any> | string)[]\n): boolean {\n return children.some((child) => {\n if (types.some((type) => isSomeElement(child.type, type))) return true\n\n const children = getValidChildren(child.props.children)\n\n return children.length ? includesChildren(children, ...types) : false\n })\n}\n\nexport function useIncludesChildren(\n children: React.ReactElement<any>[],\n ...types: (React.JSXElementConstructor<any> | string)[]\n): boolean {\n return React.useMemo(\n () => includesChildren(children, ...types),\n [children, types],\n )\n}\n\nexport function omitChildren<Y = any>(\n children: React.ReactElement[],\n ...types: (React.JSXElementConstructor<any> | string)[]\n): React.ReactElement<Y>[] {\n return children.filter((child) =>\n types.every((type) => !isSomeElement(child.type, type)),\n ) as React.ReactElement<Y>[]\n}\n\nexport function useOmitChildren<Y = any>(\n children: React.ReactElement<any>[],\n ...types: (React.JSXElementConstructor<any> | string)[]\n): React.ReactElement<Y>[] {\n const typesRef = React.useRef(types)\n\n return React.useMemo(\n () => omitChildren<Y>(children, ...typesRef.current),\n [children],\n )\n}\n\nexport function pickChildren<Y = any>(\n children: React.ReactElement[],\n ...types: (React.JSXElementConstructor<any> | string)[]\n): React.ReactElement<Y>[] {\n return children.filter((child) =>\n types.some((type) => isSomeElement(child.type, type)),\n ) as React.ReactElement<Y>[]\n}\n\nexport function usePickChildren<Y = any>(\n children: React.ReactElement<any>[],\n ...types: (React.JSXElementConstructor<any> | string)[]\n): React.ReactElement<Y>[] {\n const typesRef = React.useRef(types)\n\n return React.useMemo(\n () => pickChildren<Y>(children, ...typesRef.current),\n [children],\n )\n}\n\nexport function splitChildren<Y = any, M = any>(\n children: React.ReactNode,\n ...types: (\n | (React.JSXElementConstructor<any> | string)[]\n | React.JSXElementConstructor<any>\n | string\n )[]\n): [\n React.ReactElement<M>[] | React.ReactNode,\n ...(React.ReactElement<Y> | undefined)[],\n] {\n const validChildren = getValidChildren(children)\n\n if (isEmpty(validChildren)) return [children]\n\n const pickedChildren = types.map((types) =>\n isArray(types)\n ? findChild(validChildren, ...types)\n : findChild(validChildren, types),\n )\n\n const omittedChildren = omitChildren(validChildren, ...types.flat())\n\n return [omittedChildren, ...pickedChildren] as const\n}\n\nexport function useSplitChildren<Y = any, M = any>(\n children: React.ReactNode,\n ...types: (\n | (React.JSXElementConstructor<any> | string)[]\n | React.JSXElementConstructor<any>\n | string\n )[]\n): [\n React.ReactElement<M>[] | React.ReactNode,\n ...(React.ReactElement<Y> | undefined)[],\n] {\n const typesRef = React.useRef(types)\n\n return React.useMemo(\n () => splitChildren<Y, M>(children, ...typesRef.current),\n [children],\n )\n}\n\nexport const wrapOrPassProps = <Y extends React.PropsWithChildren>(\n Component: React.FC<Y>,\n nodeOrProps: React.ReactNode | Y,\n additionalProps?: Y,\n) => {\n if (isUndefined(nodeOrProps)) {\n return null\n } else if (isValidElement(nodeOrProps)) {\n additionalProps ??= {} as Y\n additionalProps.children = nodeOrProps\n\n return <Component {...additionalProps} />\n } else {\n return <Component {...additionalProps} {...nodeOrProps} />\n }\n}\n"],"mappings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