Commit c4ae05c5 by Torkel Ödegaard Committed by GitHub

Merge pull request #14078 from grafana/12759-description-n-links

12759 React-panel: description and links
parents 03040159 83073dea
......@@ -14,9 +14,9 @@
"@types/enzyme": "^3.1.13",
"@types/jest": "^23.3.2",
"@types/node": "^8.0.31",
"@types/react": "^16.4.14",
"@types/react": "^16.7.6",
"@types/react-custom-scrollbars": "^4.0.5",
"@types/react-dom": "^16.0.7",
"@types/react-dom": "^16.0.9",
"@types/react-select": "^2.0.4",
"angular-mocks": "1.6.6",
"autoprefixer": "^6.4.0",
......@@ -152,12 +152,12 @@
"prismjs": "^1.6.0",
"prop-types": "^15.6.2",
"rc-cascader": "^0.14.0",
"react": "^16.5.0",
"react": "^16.6.3",
"react-custom-scrollbars": "^4.2.1",
"react-dom": "^16.5.0",
"react-dom": "^16.6.3",
"react-grid-layout": "0.16.6",
"react-highlight-words": "^0.10.0",
"react-popper": "^0.7.5",
"react-popper": "^1.3.0",
"react-redux": "^5.0.7",
"react-select": "2.1.0",
"react-sizeme": "^2.3.6",
......@@ -180,6 +180,7 @@
"tslint-react": "^3.6.0"
},
"resolutions": {
"caniuse-db": "1.0.30000772"
"caniuse-db": "1.0.30000772",
"**/@types/react": "16.7.6"
}
}
import React from 'react';
import withTooltip from './withTooltip';
import { Target } from 'react-popper';
import React, { PureComponent } from 'react';
import Popper from './Popper';
import withPopper, { UsingPopperProps } from './withPopper';
interface PopoverProps {
tooltipSetState: (prevState: object) => void;
}
class Popover extends React.Component<PopoverProps, any> {
constructor(props) {
super(props);
this.toggleTooltip = this.toggleTooltip.bind(this);
}
class Popover extends PureComponent<UsingPopperProps> {
render() {
const { children, hidePopper, showPopper, className, ...restProps } = this.props;
toggleTooltip() {
const { tooltipSetState } = this.props;
tooltipSetState(prevState => {
return {
...prevState,
show: !prevState.show,
};
});
}
const togglePopper = restProps.show ? hidePopper : showPopper;
render() {
return (
<Target className="popper__target" onClick={this.toggleTooltip}>
{this.props.children}
</Target>
<div className={`popper__manager ${className}`} onClick={togglePopper}>
<Popper {...restProps}>{children}</Popper>
</div>
);
}
}
export default withTooltip(Popover);
export default withPopper(Popover);
import React, { PureComponent } from 'react';
import { Manager, Popper as ReactPopper, Reference } from 'react-popper';
import Transition from 'react-transition-group/Transition';
const defaultTransitionStyles = {
transition: 'opacity 200ms linear',
opacity: 0,
};
const transitionStyles = {
exited: { opacity: 0 },
entering: { opacity: 0 },
entered: { opacity: 1 },
exiting: { opacity: 0 },
};
interface Props {
renderContent: (content: any) => any;
show: boolean;
placement?: any;
content: string | ((props: any) => JSX.Element);
refClassName?: string;
}
class Popper extends PureComponent<Props> {
render() {
const { children, renderContent, show, placement, refClassName } = this.props;
const { content } = this.props;
return (
<Manager>
<Reference>
{({ ref }) => (
<div className={`popper_ref ${refClassName || ''}`} ref={ref}>
{children}
</div>
)}
</Reference>
<Transition in={show} timeout={100} mountOnEnter={true} unmountOnExit={true}>
{transitionState => (
<ReactPopper placement={placement}>
{({ ref, style, placement, arrowProps }) => {
return (
<div
ref={ref}
style={{
...style,
...defaultTransitionStyles,
...transitionStyles[transitionState],
}}
data-placement={placement}
className="popper"
>
<div className="popper__background">
{renderContent(content)}
<div ref={arrowProps.ref} data-placement={placement} className="popper__arrow" />
</div>
</div>
);
}}
</ReactPopper>
)}
</Transition>
</Manager>
);
}
}
export default Popper;
import React, { PureComponent } from 'react';
import withTooltip from './withTooltip';
import { Target } from 'react-popper';
interface Props {
tooltipSetState: (prevState: object) => void;
}
class Tooltip extends PureComponent<Props> {
showTooltip = () => {
const { tooltipSetState } = this.props;
tooltipSetState(prevState => ({
...prevState,
show: true,
}));
};
hideTooltip = () => {
const { tooltipSetState } = this.props;
tooltipSetState(prevState => ({
...prevState,
show: false,
}));
};
import Popper from './Popper';
import withPopper, { UsingPopperProps } from './withPopper';
class Tooltip extends PureComponent<UsingPopperProps> {
render() {
const { children, hidePopper, showPopper, className, ...restProps } = this.props;
return (
<Target className="popper__target" onMouseOver={this.showTooltip} onMouseOut={this.hideTooltip}>
{this.props.children}
</Target>
<div className={`popper__manager ${className}`} onMouseEnter={showPopper} onMouseLeave={hidePopper}>
<Popper {...restProps}>{children}</Popper>
</div>
);
}
}
export default withTooltip(Tooltip);
export default withPopper(Tooltip);
......@@ -3,10 +3,10 @@
exports[`Popover renders correctly 1`] = `
<div
className="popper__manager test-class"
onClick={[Function]}
>
<div
className="popper__target"
onClick={[Function]}
className="popper_ref "
>
<button>
Button with Popover
......
......@@ -3,11 +3,11 @@
exports[`Tooltip renders correctly 1`] = `
<div
className="popper__manager test-class"
onMouseEnter={[Function]}
onMouseLeave={[Function]}
>
<div
className="popper__target"
onMouseOut={[Function]}
onMouseOver={[Function]}
className="popper_ref "
>
<a
href="http://www.grafana.com"
......
import React from 'react';
import { Manager, Popper, Arrow } from 'react-popper';
interface IwithTooltipProps {
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;
}
export default function withTooltip(WrappedComponent) {
return class extends React.Component<IwithTooltipProps, any> {
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',
......@@ -30,6 +45,20 @@ export default function withTooltip(WrappedComponent) {
}
}
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
......@@ -40,18 +69,19 @@ export default function withTooltip(WrappedComponent) {
}
render() {
const { content, className } = this.props;
const { show, placement } = this.state;
const className = this.props.className || '';
return (
<Manager className={`popper__manager ${className || ''}`}>
<WrappedComponent {...this.props} tooltipSetState={this.setState} />
{this.state.show ? (
<Popper placement={this.state.placement} className="popper">
{this.renderContent(content)}
<Arrow className="popper__arrow" />
</Popper>
) : null}
</Manager>
<WrappedComponent
{...this.props}
showPopper={this.showPopper}
hidePopper={this.hidePopper}
renderContent={this.renderContent}
show={show}
placement={placement}
className={className}
/>
);
}
};
......
import React, { PureComponent } from 'react';
import classNames from 'classnames';
import PanelHeaderCorner from './PanelHeaderCorner';
import { PanelHeaderMenu } from './PanelHeaderMenu';
import { DashboardModel } from 'app/features/dashboard/dashboard_model';
......@@ -41,20 +42,15 @@ export class PanelHeader extends PureComponent<Props, State> {
const isLoading = false;
const panelHeaderClass = classNames({ 'panel-header': true, 'grid-drag-handle': !isFullscreen });
const { panel, dashboard, timeInfo } = this.props;
return (
<>
<PanelHeaderCorner panel={panel} />
<div className={panelHeaderClass}>
<span className="panel-info-corner">
<i className="fa" />
<span className="panel-info-corner-inner" />
</span>
{isLoading && (
<span className="panel-loading">
<i className="fa fa-spinner fa-spin" />
</span>
)}
<div className="panel-title-container" onClick={this.onMenuToggle}>
<div className="panel-title">
<span className="icon-gf panel-alert-icon" />
......@@ -76,6 +72,7 @@ export class PanelHeader extends PureComponent<Props, State> {
</div>
</div>
</div>
</>
);
}
}
import React, { PureComponent } from 'react';
import { PanelModel } from 'app/features/dashboard/panel_model';
import Tooltip from 'app/core/components/Tooltip/Tooltip';
import templateSrv from 'app/features/templating/template_srv';
import { LinkSrv } from 'app/features/dashboard/panellinks/link_srv';
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/time_srv';
import Remarkable from 'remarkable';
enum InfoModes {
Error = 'Error',
Info = 'Info',
Links = 'Links',
}
interface Props {
panel: PanelModel;
}
export class PanelHeaderCorner extends PureComponent<Props> {
timeSrv: TimeSrv = getTimeSrv();
getInfoMode = () => {
const { panel } = this.props;
// TODO
// if (error) {
// return InfoModes.Error;
// }
if (!!panel.description) {
return InfoModes.Info;
}
if (panel.links && panel.links.length) {
return InfoModes.Links;
}
return undefined;
};
getInfoContent = () => {
const { panel } = this.props;
const markdown = panel.description;
const linkSrv = new LinkSrv(templateSrv, this.timeSrv);
const sanitize = str => str; // TODO
const interpolatedMarkdown = templateSrv.replace(markdown, panel.scopedVars);
const remarkableInterpolatedMarkdown = new Remarkable().render(interpolatedMarkdown);
const html = (
<div className="markdown-html">
<div dangerouslySetInnerHTML={{ __html: remarkableInterpolatedMarkdown }} />
{panel.links &&
panel.links.length > 0 && (
<ul>
{panel.links.map((link, idx) => {
const info = linkSrv.getPanelLinkAnchorInfo(link, panel.scopedVars);
return (
<li key={idx}>
<a className="panel-menu-link" href={info.href} target={info.target}>
{info.title}
</a>
</li>
);
})}
</ul>
)}
</div>
);
return sanitize(html);
};
render() {
const infoMode: InfoModes | undefined = this.getInfoMode();
if (!infoMode) {
return null;
}
const infoContent = this.getInfoContent();
return (
<>
{infoMode === InfoModes.Info || infoMode === InfoModes.Links ? (
<Tooltip
content={infoContent}
className="absolute"
refClassName={`panel-info-corner panel-info-corner--${infoMode.toLowerCase()}`}
>
<i className="fa" />
<span className="panel-info-corner-inner" />
</Tooltip>
) : null}
</>
);
}
}
export default PanelHeaderCorner;
......@@ -49,6 +49,8 @@ export class PanelModel {
maxDataPoints?: number;
interval?: string;
description?: string;
links?: [];
// non persisted
fullscreen: boolean;
......
.popper {
position: absolute;
z-index: $zindex-tooltip;
background: $tooltipBackground;
color: $tooltipColor;
max-width: 400px;
border-radius: 3px;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
padding: 10px;
text-align: center;
}
......@@ -35,10 +31,18 @@
left: calc(50% - 5px);
margin-top: 0;
margin-bottom: 0;
padding-top: 5px;
}
.popper[data-placement^='bottom'] {
margin-top: 5px;
padding-top: 5px;
}
.popper__background {
background: $tooltipBackground;
border-radius: 3px;
box-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
padding: 10px;
}
.popper[data-placement^='bottom'] .popper__arrow {
......@@ -46,21 +50,21 @@
border-left-color: transparent;
border-right-color: transparent;
border-top-color: transparent;
top: -5px;
left: calc(50% - 5px);
top: 0;
left: calc(50% - 8px);
margin-top: 0;
margin-bottom: 0;
}
.popper[data-placement^='right'] {
margin-left: 5px;
padding-left: 5px;
}
.popper[data-placement^='right'] .popper__arrow {
border-width: 5px 5px 5px 0;
border-left-color: transparent;
border-top-color: transparent;
border-bottom-color: transparent;
left: -5px;
top: calc(50% - 5px);
left: 0;
top: calc(50% - 8px);
margin-left: 0;
margin-right: 0;
}
......
......@@ -78,3 +78,7 @@ button.close {
.d-inline-block {
display: inline-block;
}
.absolute {
position: absolute;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment