Commit a0da303f by Johannes Schill

Change KeyboardNavigation from hoc to render prop component

parent 07ce88f6
import React, { PureComponent } from 'react';
import classNames from 'classnames';
import _ from 'lodash';
import withKeyboardNavigation, { KeyboardNavigationProps } from './withKeyboardNavigation';
import KeyboardNavigation, { KeyboardNavigationProps } from './KeyboardNavigation';
import { DataSourceSelectItem } from 'app/types';
export interface Props {
......@@ -13,8 +13,7 @@ interface State {
searchQuery: string;
}
export const DataSourcePicker = withKeyboardNavigation(
class DataSourcePicker extends PureComponent<Props & KeyboardNavigationProps, State> {
export class DataSourcePicker extends PureComponent<Props, State> {
searchInput: HTMLElement;
constructor(props) {
......@@ -41,8 +40,9 @@ export const DataSourcePicker = withKeyboardNavigation(
return filtered.length - 1;
}
renderDataSource = (ds: DataSourceSelectItem, index: number) => {
const { onChangeDataSource, selected, onMouseEnter } = this.props;
renderDataSource = (ds: DataSourceSelectItem, index: number, keyNavProps: KeyboardNavigationProps) => {
const { onChangeDataSource } = this.props;
const { selected, onMouseEnter } = keyNavProps;
const onClick = () => onChangeDataSource(ds);
const isSelected = selected === index;
const cssClass = classNames({
......@@ -50,13 +50,7 @@ export const DataSourcePicker = withKeyboardNavigation(
'ds-picker-list__item--selected': isSelected,
});
return (
<div
key={index}
className={cssClass}
title={ds.name}
onClick={onClick}
onMouseEnter={() => onMouseEnter(index)}
>
<div key={index} className={cssClass} title={ds.name} onClick={onClick} onMouseEnter={() => onMouseEnter(index)}>
<img className="ds-picker-list__img" src={ds.meta.info.logos.small} />
<div className="ds-picker-list__name">{ds.name}</div>
</div>
......@@ -77,11 +71,9 @@ export const DataSourcePicker = withKeyboardNavigation(
}));
};
renderFilters() {
renderFilters({ onKeyDown, selected }: KeyboardNavigationProps) {
const { searchQuery } = this.state;
const { onKeyDown } = this.props;
return (
<>
<label className="gf-form--has-input-icon">
<input
type="text"
......@@ -92,7 +84,7 @@ export const DataSourcePicker = withKeyboardNavigation(
value={searchQuery}
onKeyDown={evt => {
onKeyDown(evt, this.maxSelectedIndex, () => {
const { onChangeDataSource, selected } = this.props;
const { onChangeDataSource } = this.props;
const ds = this.getDataSources()[selected];
onChangeDataSource(ds);
});
......@@ -100,22 +92,26 @@ export const DataSourcePicker = withKeyboardNavigation(
/>
<i className="gf-form-input-icon fa fa-search" />
</label>
</>
);
}
render() {
return (
<KeyboardNavigation
render={(keyNavProps: KeyboardNavigationProps) => (
<>
<div className="cta-form__bar">
{this.renderFilters()}
{this.renderFilters(keyNavProps)}
<div className="gf-form--grow" />
</div>
<div className="ds-picker-list">{this.getDataSources().map(this.renderDataSource)}</div>
<div className="ds-picker-list">
{this.getDataSources().map((ds, index) => this.renderDataSource(ds, index, keyNavProps))}
</div>
</>
)}
/>
);
}
}
);
}
export default DataSourcePicker;
import React, { KeyboardEvent, ComponentType, Component } from 'react';
import React, { KeyboardEvent, Component } from 'react';
interface State {
selected: number;
......@@ -10,8 +10,11 @@ export interface KeyboardNavigationProps {
selected: number;
}
const withKeyboardNavigation = <P extends object>(WrappedComponent: ComponentType<P & KeyboardNavigationProps>) => {
return class WithKeyboardNavigation extends Component<P, State> {
interface Props {
render: (injectProps: any) => void;
}
class KeyboardNavigation extends Component<Props, State> {
constructor(props) {
super(props);
......@@ -55,11 +58,14 @@ const withKeyboardNavigation = <P extends object>(WrappedComponent: ComponentTyp
};
render() {
return (
<WrappedComponent {...this.state} {...this.props} onKeyDown={this.onKeyDown} onMouseEnter={this.onMouseEnter} />
);
}
const injectProps = {
onKeyDown: this.onKeyDown,
onMouseEnter: this.onMouseEnter,
selected: this.state.selected,
};
};
export default withKeyboardNavigation;
return <>{this.props.render({ ...injectProps })}</>;
}
}
export default KeyboardNavigation;
......@@ -4,7 +4,7 @@ import _ from 'lodash';
import config from 'app/core/config';
import { PanelPlugin } from 'app/types/plugins';
import VizTypePickerPlugin from './VizTypePickerPlugin';
import withKeyboardNavigation, { KeyboardNavigationProps } from './withKeyboardNavigation';
import KeyboardNavigation, { KeyboardNavigationProps } from './KeyboardNavigation';
export interface Props {
current: PanelPlugin;
......@@ -15,8 +15,7 @@ interface State {
searchQuery: string;
}
export const VizTypePicker = withKeyboardNavigation(
class VizTypePicker extends PureComponent<Props & KeyboardNavigationProps, State> {
export class VizTypePicker extends PureComponent<Props, State> {
searchInput: HTMLElement;
pluginList = this.getPanelPlugins('');
......@@ -49,8 +48,9 @@ export const VizTypePicker = withKeyboardNavigation(
return _.sortBy(panels, 'sort');
}
renderVizPlugin = (plugin: PanelPlugin, index: number) => {
const { onTypeChanged, selected, onMouseEnter } = this.props;
renderVizPlugin = (plugin: PanelPlugin, index: number, keyNavProps: KeyboardNavigationProps) => {
const { onTypeChanged } = this.props;
const { selected, onMouseEnter } = keyNavProps;
const isSelected = selected === index;
const isCurrent = plugin.id === this.props.current.id;
return (
......@@ -87,9 +87,8 @@ export const VizTypePicker = withKeyboardNavigation(
}));
};
renderFilters = () => {
renderFilters = ({ onKeyDown, selected }: KeyboardNavigationProps) => {
const { searchQuery } = this.state;
const { onKeyDown } = this.props;
return (
<>
<label className="gf-form--has-input-icon">
......@@ -100,10 +99,9 @@ export const VizTypePicker = withKeyboardNavigation(
ref={elem => (this.searchInput = elem)}
onChange={this.onSearchQueryChange}
value={searchQuery}
// onKeyDown={this.props.onKeyDown}
onKeyDown={evt => {
onKeyDown(evt, this.maxSelectedIndex, () => {
const { onTypeChanged, selected } = this.props;
const { onTypeChanged } = this.props;
const vizType = this.getFilteredPluginList()[selected];
onTypeChanged(vizType);
});
......@@ -119,14 +117,19 @@ export const VizTypePicker = withKeyboardNavigation(
const filteredPluginList = this.getFilteredPluginList();
return (
<KeyboardNavigation
render={(keyNavProps: KeyboardNavigationProps) => (
<>
<div className="cta-form__bar">
{this.renderFilters()}
{this.renderFilters(keyNavProps)}
<div className="gf-form--grow" />
</div>
<div className="viz-picker">{filteredPluginList.map(this.renderVizPlugin)}</div>
<div className="viz-picker">
{filteredPluginList.map((plugin, index) => this.renderVizPlugin(plugin, index, keyNavProps))}
</div>
</>
)}
/>
);
}
}
);
}
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