@grafana/ui
Version:
Grafana Components Library
1 lines • 2.27 kB
Source Map (JSON)
{"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,QAAA,EAA4C;AACrE,EAAA,IAAI,OAAA;AAGJ,EAAA,MAAM,KAAA,GAAQ,KAAA,CAAM,QAAA,CAAS,IAAA,CAAK,QAAQ,CAAA;AAI1C,EAAA,IAAI,IAAA,KAAQ,+BAAO,KAAA,CAAA,EAAO;AACxB,IAAA,OAAA,GAAU,MAAM,KAAA,CAAM,EAAA;AAAA,EACxB,CAAA,MAAA,IAAW,SAAA,IAAa,KAAA,CAAM,KAAA,EAAO;AACnC,IAAA,OAAA,GAAU,+BAAO,KAAA,CAAM,OAAA;AAAA,EACzB;AAEA,EAAA,OAAO,OAAO,OAAA,KAAY,QAAA,GAAW,OAAA,GAAU,KAAA,CAAA;AACjD;AAQO,SAAS,oBAAA,CACd,cACA,KAAA,EACiB;AACjB,EAAA,IAAI,KAAA,CAAM,eAAe,YAAY,CAAA,IAAK,OAAO,YAAA,KAAiB,QAAA,IAAY,OAAO,YAAA,KAAiB,QAAA,EAAU;AAC9G,IAAA,OAAO,YAAA;AAAA,EACT;AAEA,EAAA,IAAI,OAAO,YAAA,KAAiB,UAAA,IAAc,KAAA,EAAO;AAC/C,IAAA,OAAO,aAAa,KAAK,CAAA;AAAA,EAC3B;AAEA,EAAA,MAAM,IAAI,KAAA,CAAM,CAAA,EAAG,YAAY,CAAA,iEAAA,CAAmE,CAAA;AACpG;;;;"}