|
|
|
@ -1,19 +1,23 @@ |
|
|
|
|
import React, { PureComponent } from 'react'; |
|
|
|
|
import classNames from 'classnames'; |
|
|
|
|
import _ from 'lodash'; |
|
|
|
|
|
|
|
|
|
import withKeyboardNavigation from './withKeyboardNavigation'; |
|
|
|
|
import { DataSourceSelectItem } from 'app/types'; |
|
|
|
|
|
|
|
|
|
interface Props { |
|
|
|
|
export interface Props { |
|
|
|
|
onChangeDataSource: (ds: any) => void; |
|
|
|
|
datasources: DataSourceSelectItem[]; |
|
|
|
|
selected?: number; |
|
|
|
|
onKeyDown?: (evt: any, maxSelectedIndex: number, onEnterAction: () => void) => void; |
|
|
|
|
onMouseEnter?: (select: number) => void; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
interface State { |
|
|
|
|
searchQuery: string; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export class DataSourcePicker extends PureComponent<Props, State> { |
|
|
|
|
export const DataSourcePicker = withKeyboardNavigation( |
|
|
|
|
class DataSourcePicker extends PureComponent<Props, State> { |
|
|
|
|
searchInput: HTMLElement; |
|
|
|
|
|
|
|
|
|
constructor(props) { |
|
|
|
@ -35,15 +39,27 @@ export class DataSourcePicker extends PureComponent<Props, State> { |
|
|
|
|
return filtered; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
get maxSelectedIndex() { |
|
|
|
|
const filtered = this.getDataSources(); |
|
|
|
|
return filtered.length - 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
renderDataSource = (ds: DataSourceSelectItem, index: number) => { |
|
|
|
|
const { onChangeDataSource } = this.props; |
|
|
|
|
const { onChangeDataSource, selected, onMouseEnter } = this.props; |
|
|
|
|
const onClick = () => onChangeDataSource(ds); |
|
|
|
|
const isSelected = selected === index; |
|
|
|
|
const cssClass = classNames({ |
|
|
|
|
'ds-picker-list__item': true, |
|
|
|
|
'ds-picker-list__item--selected': isSelected, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<div key={index} className={cssClass} title={ds.name} onClick={onClick}> |
|
|
|
|
<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> |
|
|
|
@ -66,6 +82,7 @@ export class DataSourcePicker extends PureComponent<Props, State> { |
|
|
|
|
|
|
|
|
|
renderFilters() { |
|
|
|
|
const { searchQuery } = this.state; |
|
|
|
|
const { onKeyDown } = this.props; |
|
|
|
|
return ( |
|
|
|
|
<> |
|
|
|
|
<label className="gf-form--has-input-icon"> |
|
|
|
@ -76,6 +93,13 @@ export class DataSourcePicker extends PureComponent<Props, State> { |
|
|
|
|
ref={elem => (this.searchInput = elem)} |
|
|
|
|
onChange={this.onSearchQueryChange} |
|
|
|
|
value={searchQuery} |
|
|
|
|
onKeyDown={evt => { |
|
|
|
|
onKeyDown(evt, this.maxSelectedIndex, () => { |
|
|
|
|
const { onChangeDataSource, selected } = this.props; |
|
|
|
|
const ds = this.getDataSources()[selected]; |
|
|
|
|
onChangeDataSource(ds); |
|
|
|
|
}); |
|
|
|
|
}} |
|
|
|
|
/> |
|
|
|
|
<i className="gf-form-input-icon fa fa-search" /> |
|
|
|
|
</label> |
|
|
|
@ -95,3 +119,6 @@ export class DataSourcePicker extends PureComponent<Props, State> { |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
export default DataSourcePicker; |
|
|
|
|