mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Refactored withPoper HOC to PopperController using render prop
This commit is contained in:
parent
8e8b759b21
commit
de4e1a91f7
@ -1,6 +1,7 @@
|
|||||||
import React, { PureComponent } from 'react';
|
import React, { PureComponent } from 'react';
|
||||||
|
import * as PopperJS from 'popper.js';
|
||||||
|
import { Manager, Popper as ReactPopper } from 'react-popper';
|
||||||
import Portal from 'app/core/components/Portal/Portal';
|
import Portal from 'app/core/components/Portal/Portal';
|
||||||
import { Manager, Popper as ReactPopper, Reference } from 'react-popper';
|
|
||||||
import Transition from 'react-transition-group/Transition';
|
import Transition from 'react-transition-group/Transition';
|
||||||
|
|
||||||
const defaultTransitionStyles = {
|
const defaultTransitionStyles = {
|
||||||
@ -18,29 +19,23 @@ const transitionStyles = {
|
|||||||
interface Props {
|
interface Props {
|
||||||
renderContent: (content: any) => any;
|
renderContent: (content: any) => any;
|
||||||
show: boolean;
|
show: boolean;
|
||||||
placement?: any;
|
placement?: PopperJS.Placement;
|
||||||
content: string | ((props: any) => JSX.Element);
|
content: string | ((props: any) => JSX.Element);
|
||||||
refClassName?: string;
|
refClassName?: string;
|
||||||
|
referenceElement: PopperJS.ReferenceObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
class Popper extends PureComponent<Props> {
|
class Popper extends PureComponent<Props> {
|
||||||
render() {
|
render() {
|
||||||
const { children, renderContent, show, placement, refClassName } = this.props;
|
const { renderContent, show, placement } = this.props;
|
||||||
const { content } = this.props;
|
const { content } = this.props;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Manager>
|
<Manager>
|
||||||
<Reference>
|
|
||||||
{({ ref }) => (
|
|
||||||
<div className={`popper_ref ${refClassName || ''}`} ref={ref}>
|
|
||||||
{children}
|
|
||||||
</div>
|
|
||||||
)}
|
|
||||||
</Reference>
|
|
||||||
<Transition in={show} timeout={100} mountOnEnter={true} unmountOnExit={true}>
|
<Transition in={show} timeout={100} mountOnEnter={true} unmountOnExit={true}>
|
||||||
{transitionState => (
|
{transitionState => (
|
||||||
<Portal>
|
<Portal>
|
||||||
<ReactPopper placement={placement}>
|
<ReactPopper placement={placement} referenceElement={this.props.referenceElement}>
|
||||||
{({ ref, style, placement, arrowProps }) => {
|
{({ ref, style, placement, arrowProps }) => {
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
94
public/app/core/components/Tooltip/PopperController.tsx
Normal file
94
public/app/core/components/Tooltip/PopperController.tsx
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import * as PopperJS from 'popper.js';
|
||||||
|
|
||||||
|
type PopperContent = string | (() => JSX.Element);
|
||||||
|
|
||||||
|
export interface UsingPopperProps {
|
||||||
|
show?: boolean;
|
||||||
|
placement?: PopperJS.Placement;
|
||||||
|
content: PopperContent;
|
||||||
|
children: JSX.Element;
|
||||||
|
renderContent?: (content: PopperContent) => JSX.Element;
|
||||||
|
}
|
||||||
|
|
||||||
|
type PopperControllerRenderProp = (
|
||||||
|
showPopper: () => void,
|
||||||
|
hidePopper: () => void,
|
||||||
|
popperProps: {
|
||||||
|
show: boolean;
|
||||||
|
placement: PopperJS.Placement;
|
||||||
|
content: string | ((props: any) => JSX.Element);
|
||||||
|
renderContent: (content: any) => any;
|
||||||
|
}
|
||||||
|
) => JSX.Element;
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
placement?: PopperJS.Placement;
|
||||||
|
content: PopperContent;
|
||||||
|
className?: string;
|
||||||
|
children: PopperControllerRenderProp;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
placement: PopperJS.Placement;
|
||||||
|
show: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
class PopperController extends React.Component<Props, State> {
|
||||||
|
constructor(props: Props) {
|
||||||
|
super(props);
|
||||||
|
|
||||||
|
this.state = {
|
||||||
|
placement: this.props.placement || 'auto',
|
||||||
|
show: false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillReceiveProps(nextProps: Props) {
|
||||||
|
if (nextProps.placement && nextProps.placement !== this.state.placement) {
|
||||||
|
this.setState(prevState => {
|
||||||
|
return {
|
||||||
|
...prevState,
|
||||||
|
placement: nextProps.placement,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
showPopper = () => {
|
||||||
|
this.setState(prevState => ({
|
||||||
|
...prevState,
|
||||||
|
show: true,
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
hidePopper = () => {
|
||||||
|
this.setState(prevState => ({
|
||||||
|
...prevState,
|
||||||
|
show: false,
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
renderContent(content: PopperContent) {
|
||||||
|
if (typeof content === 'function') {
|
||||||
|
// If it's a function we assume it's a React component
|
||||||
|
const ReactComponent = content;
|
||||||
|
return <ReactComponent />;
|
||||||
|
}
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const { children, content } = this.props;
|
||||||
|
const { show, placement } = this.state;
|
||||||
|
|
||||||
|
return children(this.showPopper, this.hidePopper, {
|
||||||
|
show,
|
||||||
|
placement,
|
||||||
|
content,
|
||||||
|
renderContent: this.renderContent,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default PopperController;
|
@ -1,88 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
|
|
||||||
export interface UsingPopperProps {
|
|
||||||
showPopper: (prevState: object) => void;
|
|
||||||
hidePopper: (prevState: object) => void;
|
|
||||||
renderContent: (content: any) => any;
|
|
||||||
show: boolean;
|
|
||||||
placement?: string;
|
|
||||||
content: string | ((props: any) => JSX.Element);
|
|
||||||
className?: string;
|
|
||||||
refClassName?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Props {
|
|
||||||
placement?: string;
|
|
||||||
className?: string;
|
|
||||||
refClassName?: string;
|
|
||||||
content: string | ((props: any) => JSX.Element);
|
|
||||||
}
|
|
||||||
|
|
||||||
interface State {
|
|
||||||
placement: string;
|
|
||||||
show: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function withPopper(WrappedComponent) {
|
|
||||||
return class extends React.Component<Props, State> {
|
|
||||||
constructor(props) {
|
|
||||||
super(props);
|
|
||||||
this.setState = this.setState.bind(this);
|
|
||||||
this.state = {
|
|
||||||
placement: this.props.placement || 'auto',
|
|
||||||
show: false,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
componentWillReceiveProps(nextProps) {
|
|
||||||
if (nextProps.placement && nextProps.placement !== this.state.placement) {
|
|
||||||
this.setState(prevState => {
|
|
||||||
return {
|
|
||||||
...prevState,
|
|
||||||
placement: nextProps.placement,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
showPopper = () => {
|
|
||||||
this.setState(prevState => ({
|
|
||||||
...prevState,
|
|
||||||
show: true,
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
|
|
||||||
hidePopper = () => {
|
|
||||||
this.setState(prevState => ({
|
|
||||||
...prevState,
|
|
||||||
show: false,
|
|
||||||
}));
|
|
||||||
};
|
|
||||||
|
|
||||||
renderContent(content) {
|
|
||||||
if (typeof content === 'function') {
|
|
||||||
// If it's a function we assume it's a React component
|
|
||||||
const ReactComponent = content;
|
|
||||||
return <ReactComponent />;
|
|
||||||
}
|
|
||||||
return content;
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
|
||||||
const { show, placement } = this.state;
|
|
||||||
const className = this.props.className || '';
|
|
||||||
|
|
||||||
return (
|
|
||||||
<WrappedComponent
|
|
||||||
{...this.props}
|
|
||||||
showPopper={this.showPopper}
|
|
||||||
hidePopper={this.hidePopper}
|
|
||||||
renderContent={this.renderContent}
|
|
||||||
show={show}
|
|
||||||
placement={placement}
|
|
||||||
className={className}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user