mirror of https://github.com/grafana/grafana
Stats: include all fields (#24829)
parent
2a6ac88a73
commit
d526647005
@ -1,11 +1,75 @@ |
|||||||
import React from 'react'; |
import React from 'react'; |
||||||
import { FieldConfigEditorProps, SelectFieldConfigSettings } from '@grafana/data'; |
import { FieldConfigEditorProps, SelectFieldConfigSettings, SelectableValue } from '@grafana/data'; |
||||||
import { Select } from '../Select/Select'; |
import { Select } from '../Select/Select'; |
||||||
|
|
||||||
export function SelectValueEditor<T>({ |
interface State<T> { |
||||||
|
isLoading: boolean; |
||||||
|
options: Array<SelectableValue<T>>; |
||||||
|
} |
||||||
|
|
||||||
|
type Props<T> = FieldConfigEditorProps<T, SelectFieldConfigSettings<T>>; |
||||||
|
|
||||||
|
export class SelectValueEditor<T> extends React.PureComponent<Props<T>, State<T>> { |
||||||
|
state: State<T> = { |
||||||
|
isLoading: true, |
||||||
|
options: [], |
||||||
|
}; |
||||||
|
|
||||||
|
componentDidMount() { |
||||||
|
this.updateOptions(); |
||||||
|
} |
||||||
|
|
||||||
|
componentDidUpdate(oldProps: Props<T>) { |
||||||
|
const old = oldProps.item?.settings; |
||||||
|
const now = this.props.item?.settings; |
||||||
|
if (old !== now) { |
||||||
|
this.updateOptions(); |
||||||
|
} else if (now.getOptions) { |
||||||
|
const old = oldProps.context?.data; |
||||||
|
const now = this.props.context?.data; |
||||||
|
if (old !== now) { |
||||||
|
this.updateOptions(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
updateOptions = async () => { |
||||||
|
const { item } = this.props; |
||||||
|
const { settings } = item; |
||||||
|
let options: Array<SelectableValue<T>> = item.settings?.options || []; |
||||||
|
if (settings?.getOptions) { |
||||||
|
options = await settings.getOptions(this.props.context); |
||||||
|
} |
||||||
|
if (this.state.options !== options) { |
||||||
|
this.setState({ |
||||||
|
isLoading: false, |
||||||
|
options, |
||||||
|
}); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
render() { |
||||||
|
const { options, isLoading } = this.state; |
||||||
|
const { value, onChange, item } = this.props; |
||||||
|
|
||||||
|
const { settings } = item; |
||||||
|
const { allowCustomValue } = settings; |
||||||
|
let current = options.find(v => v.value === value); |
||||||
|
if (!current && value) { |
||||||
|
current = { |
||||||
|
label: `${value}`, |
||||||
value, |
value, |
||||||
onChange, |
}; |
||||||
item, |
} |
||||||
}: FieldConfigEditorProps<T, SelectFieldConfigSettings<T>>) { |
return ( |
||||||
return <Select<T> defaultValue={value} onChange={e => onChange(e.value)} options={item.settings?.options} />; |
<Select<T> |
||||||
|
isLoading={isLoading} |
||||||
|
value={current} |
||||||
|
defaultValue={value} |
||||||
|
allowCustomValue={allowCustomValue} |
||||||
|
onChange={e => onChange(e.value)} |
||||||
|
options={options} |
||||||
|
/> |
||||||
|
); |
||||||
|
} |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue