Files
grafana/packages/grafana-ui/src/utils/renderOrCallToRender.ts
Dominik Prokop 712564f66a NewPanelEdit: Add unified UI to queries and transformations (#23478)
* Do not use pointer cursor on icon by default

* Allow items alignment in the HorizontalGroup layout

* Add util for rendering components based on their type (element or function)

* Components for rendering query and transformation rows in a unified way

* Apply new UI fo query and transformation rows

* Add some tests

* Minor fix for scroll area

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
2020-04-09 21:23:22 +02:00

23 lines
693 B
TypeScript

import React from 'react';
/**
* Given react node or function returns element accordingly
*
* @param itemToRender
* @param props props to be passed to the function if item provided as such
*/
export function renderOrCallToRender<TProps = any>(
itemToRender: ((props?: TProps) => React.ReactNode) | React.ReactNode,
props?: TProps
): React.ReactNode {
if (React.isValidElement(itemToRender) || typeof itemToRender === 'string' || typeof itemToRender === 'number') {
return itemToRender;
}
if (typeof itemToRender === 'function') {
return itemToRender(props);
}
throw new Error(`${itemToRender} is not a React element nor a function that returns React element`);
}