UNPKG

@grafana/ui

Version:
1 lines 2.25 kB
{"version":3,"file":"reactUtils.mjs","sources":["../../../src/utils/reactUtils.ts"],"sourcesContent":["import { ReactElement } from 'react';\nimport * as React from 'react';\n\n/** Returns the ID value of the first, and only, child element */\nexport function getChildId(children: ReactElement): string | undefined {\n let inputId: unknown;\n\n // Get the first, and only, child to retrieve form input's id\n const child = React.Children.only(children);\n\n // Retrieve input's id to apply on the label for correct click interaction\n // For some components (like Select), we want to get the ID from a different prop\n if ('id' in child?.props) {\n inputId = child.props.id;\n } else if ('inputId' in child.props) {\n inputId = child?.props.inputId;\n }\n\n return typeof inputId === 'string' ? inputId : undefined;\n}\n\n/**\n * Given react node or function returns element accordingly\n *\n * @param itemToRender\n * @param props props to be passed to the function if item provided as such\n */\nexport function renderOrCallToRender<TProps = {}>(\n itemToRender: ((props: TProps) => React.ReactNode) | React.ReactNode,\n props?: TProps\n): React.ReactNode {\n if (React.isValidElement(itemToRender) || typeof itemToRender === 'string' || typeof itemToRender === 'number') {\n return itemToRender;\n }\n\n if (typeof itemToRender === 'function' && props) {\n return itemToRender(props);\n }\n\n throw new Error(`${itemToRender} is not a React element nor a function that returns React element`);\n}\n"],"names":[],"mappings":";;AAIO,SAAS,WAAW,QAA4C,EAAA;AACrE,EAAI,IAAA,OAAA;AAGJ,EAAA,MAAM,KAAQ,GAAA,KAAA,CAAM,QAAS,CAAA,IAAA,CAAK,QAAQ,CAAA;AAI1C,EAAI,IAAA,IAAA,KAAQ,+BAAO,KAAO,CAAA,EAAA;AACxB,IAAA,OAAA,GAAU,MAAM,KAAM,CAAA,EAAA;AAAA,GACxB,MAAA,IAAW,SAAa,IAAA,KAAA,CAAM,KAAO,EAAA;AACnC,IAAA,OAAA,GAAU,+BAAO,KAAM,CAAA,OAAA;AAAA;AAGzB,EAAO,OAAA,OAAO,OAAY,KAAA,QAAA,GAAW,OAAU,GAAA,KAAA,CAAA;AACjD;AAQgB,SAAA,oBAAA,CACd,cACA,KACiB,EAAA;AACjB,EAAI,IAAA,KAAA,CAAM,eAAe,YAAY,CAAA,IAAK,OAAO,YAAiB,KAAA,QAAA,IAAY,OAAO,YAAA,KAAiB,QAAU,EAAA;AAC9G,IAAO,OAAA,YAAA;AAAA;AAGT,EAAI,IAAA,OAAO,YAAiB,KAAA,UAAA,IAAc,KAAO,EAAA;AAC/C,IAAA,OAAO,aAAa,KAAK,CAAA;AAAA;AAG3B,EAAA,MAAM,IAAI,KAAA,CAAM,CAAG,EAAA,YAAY,CAAmE,iEAAA,CAAA,CAAA;AACpG;;;;"}