1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/renderer/components/tooltip/withTooltip.tsx
Roman f2803abcb6 tooltip refactoring -- part 1
Signed-off-by: Roman <ixrock@gmail.com>
2020-07-28 16:57:24 +03:00

41 lines
1.3 KiB
TypeScript

// Tooltip decorator for simple composition with other components
import React, { HTMLAttributes, ReactNode } from "react"
import hoistNonReactStatics from "hoist-non-react-statics";
import { Tooltip, TooltipProps } from "./tooltip";
import { isReactNode } from "../../utils/isReactNode";
import uniqueId from "lodash/uniqueId"
export interface TooltipDecoratorProps {
tooltip?: ReactNode | Omit<TooltipProps, "targetId">;
}
export function withTooltip<T extends React.ComponentType<any>>(Target: T): T {
const DecoratedComponent = class extends React.Component<HTMLAttributes<any> & TooltipDecoratorProps> {
static displayName = `withTooltip(${Target.displayName || Target.name})`;
protected tooltipId = uniqueId("tooltip_target_");
render() {
const { tooltip, ...targetProps } = this.props;
if (tooltip) {
const tooltipId = targetProps.id || this.tooltipId;
const tooltipProps: TooltipProps = {
targetId: tooltipId,
...(isReactNode(tooltip) ? { children: tooltip } : tooltip),
};
targetProps.id = tooltipId;
targetProps.children = (
<>
{targetProps.children}
<Tooltip {...tooltipProps}/>
</>
)
}
return <Target {...targetProps as any}/>;
}
}
return hoistNonReactStatics(DecoratedComponent, Target) as any;
}