react-smart-conditional
Version:
Manage conditional rendering in react js and it's frameworks like a pro
1 lines • 5.81 kB
Source Map (JSON)
{"version":3,"file":"index.mjs","sources":["../src/types/polymorphic.ts","../src/utils/is-fragment.ts","../src/components/if.tsx","../src/components/else.tsx","../src/components/show.tsx"],"sourcesContent":["import {\n forwardRef,\n type ComponentPropsWithRef,\n type ElementType,\n type ForwardRefExoticComponent,\n type ForwardRefRenderFunction,\n type ReactElement,\n} from 'react';\n\ntype DistributiveOmit<T, K extends keyof any> = T extends any\n ? Omit<T, K>\n : never;\n\ntype Merge<A, B> = Omit<A, keyof B> & B;\ntype DistributiveMerge<A, B> = DistributiveOmit<A, keyof B> & B;\n\nexport type AsProps<\n Component extends ElementType,\n PermanentProps extends object,\n ComponentProps extends object,\n> = DistributiveMerge<ComponentProps, PermanentProps & { as?: Component }>;\n\nexport type PolymorphicWithRef<\n Default extends OnlyAs,\n Props extends object = object,\n OnlyAs extends ElementType = ElementType,\n> = <T extends OnlyAs = Default>(\n props: AsProps<T, Props, ComponentPropsWithRef<T>>,\n) => ReactElement | null;\n\nexport type PolyForwardComponent<\n Default extends OnlyAs,\n Props extends object = object,\n OnlyAs extends ElementType = ElementType,\n> = Merge<\n ForwardRefExoticComponent<\n Merge<ComponentPropsWithRef<Default>, Props & { as?: Default }>\n >,\n PolymorphicWithRef<Default, Props, OnlyAs>\n>;\n\nexport type PolyRefFunction = <\n Default extends OnlyAs,\n Props extends object = object,\n OnlyAs extends ElementType = ElementType,\n>(\n Component: ForwardRefRenderFunction<any, Props & { as?: OnlyAs }>,\n) => PolyForwardComponent<Default, Props, OnlyAs>;\n\nexport const polymorphicForwardRef = forwardRef as PolyRefFunction;\n","import { Fragment } from 'react';\n\nexport const isFragment = (Component: React.ElementType) =>\n Component === Fragment;\n","import React from 'react';\nimport { polymorphicForwardRef } from '../types/polymorphic';\nimport { isFragment } from '../utils/is-fragment';\n\nexport const If = polymorphicForwardRef<\n 'div',\n JSX.IntrinsicElements['div'] & { condition: boolean }\n>(({ as: Element = 'div', condition, ...props }, ref) =>\n condition ? (\n isFragment(Element) ? (\n props.children\n ) : (\n <Element ref={ref} {...props} />\n )\n ) : null,\n);\nIf.displayName = 'If';\n","import React from 'react';\nimport { polymorphicForwardRef } from '../types/polymorphic';\nimport { isFragment } from '../utils/is-fragment';\n\nexport const Else = polymorphicForwardRef<'div', JSX.IntrinsicElements['div']>(\n ({ as: Element = 'div', ...props }, ref) => {\n return isFragment(Element) ? (\n props.children\n ) : (\n <Element ref={ref} {...props} />\n );\n },\n);\nElse.displayName = 'Else';\n","import React, { type ReactNode, Children, isValidElement } from 'react';\nimport { If } from './if';\nimport { Else } from './else';\nimport { polymorphicForwardRef } from '../types/polymorphic';\nimport { isFragment } from '../utils/is-fragment';\n\ntype ConditionalComponent = typeof If | typeof Else;\n\ntype ShowProps = {\n multiple?: boolean;\n};\n\nconst Show = polymorphicForwardRef<\n 'div',\n JSX.IntrinsicElements['div'] & ShowProps\n>(({ as: Element = 'div', children, multiple = false, ...props }, ref) => {\n const trueConditions: ReactNode[] = [];\n let Otherwise: ReactNode = null;\n\n Children.toArray(children).forEach((child) => {\n if (isValidElement<{ condition?: boolean }>(child)) {\n const childType = child.type as ConditionalComponent;\n if (childType === If && child.props.condition) {\n trueConditions.push(child);\n if (!multiple) return;\n } else if (childType === Else && !Otherwise) {\n Otherwise = child;\n }\n } else {\n console.warn('Invalid child type in Show component');\n }\n });\n\n const content =\n trueConditions.length > 0\n ? multiple\n ? trueConditions\n : trueConditions[0]\n : Otherwise;\n\n return isFragment(Element) ? (\n content\n ) : (\n <Element ref={ref} {...props}>\n {content}\n </Element>\n );\n});\nShow.displayName = 'Show';\n\nconst ShowWithComponents = Object.assign(Show, {\n If,\n Else,\n});\nexport default ShowWithComponents;\nexport { ShowWithComponents as Show };\n"],"names":["polymorphicForwardRef","forwardRef","isFragment","Component","Fragment","_excluded","If","_ref","ref","as","Element","condition","props","_objectWithoutPropertiesLoose","children","React","createElement","_extends","displayName","Else","Show","multiple","trueConditions","Otherwise","Children","toArray","forEach","child","isValidElement","childType","type","push","console","warn","content","length","ShowWithComponents","Object","assign"],"mappings":"2bAiDa,MAAAA,EAAwBC,EC/CxBC,EAAcC,GACzBA,IAAcC,ECHhBC,EAAA,CAAA,KAAA,aAIaC,EAAKN,EAGhB,CAAAO,EAA+CC,KAAG,IAA/CC,GAAIC,EAAU,MAAKC,UAAEA,GAAqBJ,EAAPK,EAAKC,EAAAN,EAAAF,GAC3C,OAAAM,EACET,EAAWQ,GACTE,EAAME,SAENC,EAAAC,cAACN,EAAOO,EAAA,CAACT,IAAKA,GAASI,IAEvB,OAENN,EAAGY,YAAc,KChBjB,MAAAb,EAAA,CAAA,MAIac,EAAOnB,EAClB,CAAAO,EAAoCC,KAAO,IAAxCC,GAAIC,EAAU,OAAiBH,EAAPK,EAAKC,EAAAN,EAAAF,GAC9B,OAAOH,EAAWQ,GAChBE,EAAME,SAENC,EAACC,cAAAN,EAAOO,EAAA,CAACT,IAAKA,GAASI,GAAS,GAItCO,EAAKD,YAAc,4CCDbE,EAAOpB,EAGX,CAAAO,EAAgEC,KAAO,IAApEC,GAAIC,EAAU,MAAKI,SAAEA,EAAQO,SAAEA,GAAW,GAAiBd,EAAPK,EAAKC,EAAAN,EAAAF,GAC5D,MAAMiB,EAA8B,GACpC,IAAIC,EAAuB,KAE3BC,EAASC,QAAQX,GAAUY,QAASC,IAClC,GAAIC,EAAwCD,GAAQ,CAClD,MAAME,EAAYF,EAAMG,KACxB,GAAID,IAAcvB,GAAMqB,EAAMf,MAAMD,WAElC,GADAW,EAAeS,KAAKJ,IACfN,EAAU,YACNQ,IAAcV,GAASI,IAChCA,EAAYI,EAEhB,MACEK,QAAQC,KAAK,uCACf,GAGF,MAAMC,EACJZ,EAAea,OAAS,EACpBd,EACEC,EACAA,EAAe,GACjBC,EAEN,OAAOrB,EAAWQ,GAChBwB,EAEAnB,EAACC,cAAAN,EAAOO,GAACT,IAAKA,GAASI,GACpBsB,EAAO,GAIdd,EAAKF,YAAc,OAEb,MAAAkB,EAAqBC,OAAOC,OAAOlB,EAAM,CAC7Cd,KACAa"}