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