mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Performance: Standardize lodash imports to use destructured members Changes lodash imports of the form `import x from 'lodash/x'` to `import { x } from 'lodash'` to reduce bundle size. * Remove unnecessary _ import from Graph component * Enforce lodash import style * Fix remaining lodash imports
83 lines
2.1 KiB
TypeScript
83 lines
2.1 KiB
TypeScript
import React, { PureComponent } from 'react';
|
|
|
|
import { difference } from 'lodash';
|
|
|
|
import { Select } from '../Select/Select';
|
|
|
|
import { fieldReducers, SelectableValue } from '@grafana/data';
|
|
|
|
interface Props {
|
|
placeholder?: string;
|
|
onChange: (stats: string[]) => void;
|
|
stats: string[];
|
|
allowMultiple?: boolean;
|
|
defaultStat?: string;
|
|
className?: string;
|
|
menuPlacement?: 'auto' | 'bottom' | 'top';
|
|
}
|
|
|
|
export class StatsPicker extends PureComponent<Props> {
|
|
static defaultProps: Partial<Props> = {
|
|
allowMultiple: false,
|
|
};
|
|
|
|
componentDidMount() {
|
|
this.checkInput();
|
|
}
|
|
|
|
componentDidUpdate(prevProps: Props) {
|
|
this.checkInput();
|
|
}
|
|
|
|
checkInput = () => {
|
|
const { stats, allowMultiple, defaultStat, onChange } = this.props;
|
|
|
|
const current = fieldReducers.list(stats);
|
|
if (current.length !== stats.length) {
|
|
const found = current.map((v) => v.id);
|
|
const notFound = difference(stats, found);
|
|
console.warn('Unknown stats', notFound, stats);
|
|
onChange(current.map((stat) => stat.id));
|
|
}
|
|
|
|
// Make sure there is only one
|
|
if (!allowMultiple && stats.length > 1) {
|
|
console.warn('Removing extra stat', stats);
|
|
onChange([stats[0]]);
|
|
}
|
|
|
|
// Set the reducer from callback
|
|
if (defaultStat && stats.length < 1) {
|
|
onChange([defaultStat]);
|
|
}
|
|
};
|
|
|
|
onSelectionChange = (item: SelectableValue<string>) => {
|
|
const { onChange } = this.props;
|
|
if (Array.isArray(item)) {
|
|
onChange(item.map((v) => v.value));
|
|
} else {
|
|
onChange(item && item.value ? [item.value] : []);
|
|
}
|
|
};
|
|
|
|
render() {
|
|
const { stats, allowMultiple, defaultStat, placeholder, className, menuPlacement } = this.props;
|
|
|
|
const select = fieldReducers.selectOptions(stats);
|
|
return (
|
|
<Select
|
|
value={select.current}
|
|
className={className}
|
|
isClearable={!defaultStat}
|
|
isMulti={allowMultiple}
|
|
isSearchable={true}
|
|
options={select.options}
|
|
placeholder={placeholder}
|
|
onChange={this.onSelectionChange}
|
|
menuPlacement={menuPlacement}
|
|
/>
|
|
);
|
|
}
|
|
}
|