mirror of https://github.com/grafana/grafana
Add multiselect options ui (#31501)
* add multiselect options ui * Change import * Add addMultiSelect method to ui builderpull/27323/head^2
parent
7428668835
commit
89fa4acafc
@ -0,0 +1,69 @@ |
|||||||
|
import React from 'react'; |
||||||
|
import { FieldConfigEditorProps, SelectFieldConfigSettings, SelectableValue } from '@grafana/data'; |
||||||
|
import { MultiSelect } from '../Select/Select'; |
||||||
|
|
||||||
|
interface State<T> { |
||||||
|
isLoading: boolean; |
||||||
|
options: Array<SelectableValue<T>>; |
||||||
|
} |
||||||
|
|
||||||
|
type Props<T> = FieldConfigEditorProps<T[], SelectFieldConfigSettings<T>>; |
||||||
|
|
||||||
|
export class MultiSelectValueEditor<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; |
||||||
|
return ( |
||||||
|
<MultiSelect<T> |
||||||
|
isLoading={isLoading} |
||||||
|
value={value} |
||||||
|
defaultValue={value} |
||||||
|
allowCustomValue={settings?.allowCustomValue} |
||||||
|
onChange={(e) => { |
||||||
|
onChange(e.map((v) => v.value).flatMap((v) => (v !== undefined ? [v] : []))); |
||||||
|
}} |
||||||
|
options={options} |
||||||
|
/> |
||||||
|
); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue