import React, { PureComponent } from 'react'; import classNames from 'classnames'; import _ from 'lodash'; import { getDatasourceSrv } from 'app/features/plugins/datasource_srv'; import { DataSourceSelectItem } from 'app/types'; interface Props {} interface State { datasources: DataSourceSelectItem[]; searchQuery: string; } export class DataSourcePicker extends PureComponent { searchInput: HTMLElement; constructor(props) { super(props); this.state = { datasources: getDatasourceSrv().getMetricSources(), searchQuery: '', }; } getDataSources() { const { datasources, searchQuery } = this.state; const regex = new RegExp(searchQuery, 'i'); const filtered = datasources.filter(item => { return regex.test(item.name) || regex.test(item.meta.name); }); return _.sortBy(filtered, 'sort'); } renderDataSource = (ds: DataSourceSelectItem, index) => { const cssClass = classNames({ 'ds-picker-list__item': true, }); return (
{ds.name}
); }; componentDidMount() { setTimeout(() => { this.searchInput.focus(); }, 300); } renderFilters() { return ( <>
); } render() { return ( <>
{this.renderFilters()}
{this.getDataSources().map(this.renderDataSource)}
); } }