mirror of https://github.com/grafana/grafana
parent
1ffac5a33d
commit
20134c902b
@ -1,97 +1,124 @@ |
|||||||
import React, { PureComponent } from 'react'; |
import React, { PureComponent } from 'react'; |
||||||
import classNames from 'classnames'; |
import classNames from 'classnames'; |
||||||
import _ from 'lodash'; |
import _ from 'lodash'; |
||||||
|
import withKeyboardNavigation from './withKeyboardNavigation'; |
||||||
import { DataSourceSelectItem } from 'app/types'; |
import { DataSourceSelectItem } from 'app/types'; |
||||||
|
|
||||||
interface Props { |
export interface Props { |
||||||
onChangeDataSource: (ds: any) => void; |
onChangeDataSource: (ds: any) => void; |
||||||
datasources: DataSourceSelectItem[]; |
datasources: DataSourceSelectItem[]; |
||||||
|
selected?: number; |
||||||
|
onKeyDown?: (evt: any, maxSelectedIndex: number, onEnterAction: () => void) => void; |
||||||
|
onMouseEnter?: (select: number) => void; |
||||||
} |
} |
||||||
|
|
||||||
interface State { |
interface State { |
||||||
searchQuery: string; |
searchQuery: string; |
||||||
} |
} |
||||||
|
|
||||||
export class DataSourcePicker extends PureComponent<Props, State> { |
export const DataSourcePicker = withKeyboardNavigation( |
||||||
searchInput: HTMLElement; |
class DataSourcePicker extends PureComponent<Props, State> { |
||||||
|
searchInput: HTMLElement; |
||||||
|
|
||||||
constructor(props) { |
constructor(props) { |
||||||
super(props); |
super(props); |
||||||
this.state = { |
this.state = { |
||||||
searchQuery: '', |
searchQuery: '', |
||||||
}; |
}; |
||||||
} |
} |
||||||
|
|
||||||
getDataSources() { |
getDataSources() { |
||||||
const { searchQuery } = this.state; |
const { searchQuery } = this.state; |
||||||
const regex = new RegExp(searchQuery, 'i'); |
const regex = new RegExp(searchQuery, 'i'); |
||||||
const { datasources } = this.props; |
const { datasources } = this.props; |
||||||
|
|
||||||
const filtered = datasources.filter(item => { |
const filtered = datasources.filter(item => { |
||||||
return regex.test(item.name) || regex.test(item.meta.name); |
return regex.test(item.name) || regex.test(item.meta.name); |
||||||
}); |
}); |
||||||
|
|
||||||
return filtered; |
return filtered; |
||||||
} |
} |
||||||
|
|
||||||
|
get maxSelectedIndex() { |
||||||
|
const filtered = this.getDataSources(); |
||||||
|
return filtered.length - 1; |
||||||
|
} |
||||||
|
|
||||||
renderDataSource = (ds: DataSourceSelectItem, index: number) => { |
renderDataSource = (ds: DataSourceSelectItem, index: number) => { |
||||||
const { onChangeDataSource } = this.props; |
const { onChangeDataSource, selected, onMouseEnter } = this.props; |
||||||
const onClick = () => onChangeDataSource(ds); |
const onClick = () => onChangeDataSource(ds); |
||||||
const cssClass = classNames({ |
const isSelected = selected === index; |
||||||
'ds-picker-list__item': true, |
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} |
||||||
|
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> |
||||||
|
); |
||||||
|
}; |
||||||
|
|
||||||
return ( |
componentDidMount() { |
||||||
<div key={index} className={cssClass} title={ds.name} onClick={onClick}> |
setTimeout(() => { |
||||||
<img className="ds-picker-list__img" src={ds.meta.info.logos.small} /> |
this.searchInput.focus(); |
||||||
<div className="ds-picker-list__name">{ds.name}</div> |
}, 300); |
||||||
</div> |
} |
||||||
); |
|
||||||
}; |
|
||||||
|
|
||||||
componentDidMount() { |
onSearchQueryChange = evt => { |
||||||
setTimeout(() => { |
const value = evt.target.value; |
||||||
this.searchInput.focus(); |
this.setState(prevState => ({ |
||||||
}, 300); |
...prevState, |
||||||
} |
searchQuery: value, |
||||||
|
})); |
||||||
|
}; |
||||||
|
|
||||||
onSearchQueryChange = evt => { |
renderFilters() { |
||||||
const value = evt.target.value; |
const { searchQuery } = this.state; |
||||||
this.setState(prevState => ({ |
const { onKeyDown } = this.props; |
||||||
...prevState, |
return ( |
||||||
searchQuery: value, |
<> |
||||||
})); |
<label className="gf-form--has-input-icon"> |
||||||
}; |
<input |
||||||
|
type="text" |
||||||
|
className="gf-form-input width-13" |
||||||
|
placeholder="" |
||||||
|
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> |
||||||
|
</> |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
renderFilters() { |
render() { |
||||||
const { searchQuery } = this.state; |
return ( |
||||||
return ( |
<> |
||||||
<> |
<div className="cta-form__bar"> |
||||||
<label className="gf-form--has-input-icon"> |
{this.renderFilters()} |
||||||
<input |
<div className="gf-form--grow" /> |
||||||
type="text" |
</div> |
||||||
className="gf-form-input width-13" |
<div className="ds-picker-list">{this.getDataSources().map(this.renderDataSource)}</div> |
||||||
placeholder="" |
</> |
||||||
ref={elem => (this.searchInput = elem)} |
); |
||||||
onChange={this.onSearchQueryChange} |
} |
||||||
value={searchQuery} |
|
||||||
/> |
|
||||||
<i className="gf-form-input-icon fa fa-search" /> |
|
||||||
</label> |
|
||||||
</> |
|
||||||
); |
|
||||||
} |
} |
||||||
|
); |
||||||
|
|
||||||
render() { |
export default DataSourcePicker; |
||||||
return ( |
|
||||||
<> |
|
||||||
<div className="cta-form__bar"> |
|
||||||
{this.renderFilters()} |
|
||||||
<div className="gf-form--grow" /> |
|
||||||
</div> |
|
||||||
<div className="ds-picker-list">{this.getDataSources().map(this.renderDataSource)}</div> |
|
||||||
</> |
|
||||||
); |
|
||||||
} |
|
||||||
} |
|
||||||
|
Loading…
Reference in new issue