mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
scrollbar refactor: replace HOC by component with children
This commit is contained in:
parent
28cc605e32
commit
e67b8a3e1a
@ -0,0 +1,16 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import renderer from 'react-test-renderer';
|
||||||
|
import GrafanaScrollbar from './GrafanaScrollbar';
|
||||||
|
|
||||||
|
describe('GrafanaScrollbar', () => {
|
||||||
|
it('renders correctly', () => {
|
||||||
|
const tree = renderer
|
||||||
|
.create(
|
||||||
|
<GrafanaScrollbar>
|
||||||
|
<p>Scrollable content</p>
|
||||||
|
</GrafanaScrollbar>
|
||||||
|
)
|
||||||
|
.toJSON();
|
||||||
|
expect(tree).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
});
|
48
public/app/core/components/ScrollBar/GrafanaScrollbar.tsx
Normal file
48
public/app/core/components/ScrollBar/GrafanaScrollbar.tsx
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import Scrollbars from 'react-custom-scrollbars';
|
||||||
|
|
||||||
|
interface GrafanaScrollBarProps {
|
||||||
|
customClassName?: string;
|
||||||
|
autoHide?: boolean;
|
||||||
|
autoHideTimeout?: number;
|
||||||
|
autoHideDuration?: number;
|
||||||
|
hideTracksWhenNotNeeded?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
const grafanaScrollBarDefaultProps: Partial<GrafanaScrollBarProps> = {
|
||||||
|
customClassName: 'custom-scrollbars',
|
||||||
|
autoHide: true,
|
||||||
|
autoHideTimeout: 200,
|
||||||
|
autoHideDuration: 200,
|
||||||
|
hideTracksWhenNotNeeded: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps component into <Scrollbars> component from `react-custom-scrollbars`
|
||||||
|
*/
|
||||||
|
class GrafanaScrollbar extends React.Component<GrafanaScrollBarProps> {
|
||||||
|
static defaultProps = grafanaScrollBarDefaultProps;
|
||||||
|
|
||||||
|
render() {
|
||||||
|
const { customClassName, children, ...scrollProps } = this.props;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Scrollbars
|
||||||
|
className={customClassName}
|
||||||
|
autoHeight={true}
|
||||||
|
autoHeightMin={'100%'}
|
||||||
|
autoHeightMax={'100%'}
|
||||||
|
renderTrackHorizontal={props => <div {...props} className="track-horizontal" />}
|
||||||
|
renderTrackVertical={props => <div {...props} className="track-vertical" />}
|
||||||
|
renderThumbHorizontal={props => <div {...props} className="thumb-horizontal" />}
|
||||||
|
renderThumbVertical={props => <div {...props} className="thumb-vertical" />}
|
||||||
|
renderView={props => <div {...props} className="view" />}
|
||||||
|
{...scrollProps}
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</Scrollbars>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default GrafanaScrollbar;
|
@ -1,6 +1,6 @@
|
|||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
exports[`withScrollBar renders correctly 1`] = `
|
exports[`GrafanaScrollbar renders correctly 1`] = `
|
||||||
<div
|
<div
|
||||||
className="custom-scrollbars"
|
className="custom-scrollbars"
|
||||||
style={
|
style={
|
||||||
@ -32,9 +32,9 @@ exports[`withScrollBar renders correctly 1`] = `
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
<div
|
<p>
|
||||||
className="my-component"
|
Scrollable content
|
||||||
/>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
className="track-horizontal"
|
className="track-horizontal"
|
@ -1,23 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
import renderer from 'react-test-renderer';
|
|
||||||
import withScrollBar from './withScrollBar';
|
|
||||||
|
|
||||||
class TestComponent extends React.Component {
|
|
||||||
render() {
|
|
||||||
return <div className="my-component" />;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('withScrollBar', () => {
|
|
||||||
it('renders correctly', () => {
|
|
||||||
const TestComponentWithScroll = withScrollBar(TestComponent);
|
|
||||||
const tree = renderer
|
|
||||||
.create(
|
|
||||||
<TestComponentWithScroll>
|
|
||||||
<p>Scrollable content</p>
|
|
||||||
</TestComponentWithScroll>
|
|
||||||
)
|
|
||||||
.toJSON();
|
|
||||||
expect(tree).toMatchSnapshot();
|
|
||||||
});
|
|
||||||
});
|
|
@ -1,53 +0,0 @@
|
|||||||
import React from 'react';
|
|
||||||
import Scrollbars from 'react-custom-scrollbars';
|
|
||||||
|
|
||||||
interface WithScrollBarProps {
|
|
||||||
customClassName?: string;
|
|
||||||
autoHide?: boolean;
|
|
||||||
autoHideTimeout?: number;
|
|
||||||
autoHideDuration?: number;
|
|
||||||
hideTracksWhenNotNeeded?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
const withScrollBarDefaultProps: Partial<WithScrollBarProps> = {
|
|
||||||
customClassName: 'custom-scrollbars',
|
|
||||||
autoHide: true,
|
|
||||||
autoHideTimeout: 200,
|
|
||||||
autoHideDuration: 200,
|
|
||||||
hideTracksWhenNotNeeded: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wraps component into <Scrollbars> component from `react-custom-scrollbars`
|
|
||||||
*/
|
|
||||||
export default function withScrollBar<P extends object>(WrappedComponent: React.ComponentType<P>) {
|
|
||||||
return class extends React.Component<P & WithScrollBarProps> {
|
|
||||||
static defaultProps = withScrollBarDefaultProps;
|
|
||||||
|
|
||||||
render() {
|
|
||||||
// Use type casting here in order to get rest of the props working. See more
|
|
||||||
// https://github.com/Microsoft/TypeScript/issues/14409
|
|
||||||
// https://github.com/Microsoft/TypeScript/pull/13288
|
|
||||||
const { autoHide, autoHideTimeout, autoHideDuration, hideTracksWhenNotNeeded, customClassName, ...props } = this
|
|
||||||
.props as WithScrollBarProps;
|
|
||||||
const scrollProps = { autoHide, autoHideTimeout, autoHideDuration, hideTracksWhenNotNeeded };
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Scrollbars
|
|
||||||
className={customClassName}
|
|
||||||
autoHeight={true}
|
|
||||||
autoHeightMin={'100%'}
|
|
||||||
autoHeightMax={'100%'}
|
|
||||||
renderTrackHorizontal={props => <div {...props} className="track-horizontal" />}
|
|
||||||
renderTrackVertical={props => <div {...props} className="track-vertical" />}
|
|
||||||
renderThumbHorizontal={props => <div {...props} className="thumb-horizontal" />}
|
|
||||||
renderThumbVertical={props => <div {...props} className="thumb-vertical" />}
|
|
||||||
renderView={props => <div {...props} className="view" />}
|
|
||||||
{...scrollProps}
|
|
||||||
>
|
|
||||||
<WrappedComponent {...props} />
|
|
||||||
</Scrollbars>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user