Files
grafana/public/app/core/components/Select/DataSourcePicker.tsx

95 lines
2.2 KiB
TypeScript
Raw Normal View History

2018-12-11 18:49:48 +01:00
// Libraries
2018-11-09 15:49:55 +01:00
import React, { PureComponent } from 'react';
2018-12-11 18:49:48 +01:00
// Components
import { Select } from '@grafana/ui';
import { SelectableValue, DataSourceSelectItem } from '@grafana/data';
import { selectors } from '@grafana/e2e-selectors';
2018-11-09 15:49:55 +01:00
export interface Props {
2018-12-14 14:05:47 +01:00
onChange: (ds: DataSourceSelectItem) => void;
datasources: DataSourceSelectItem[];
current?: DataSourceSelectItem | null;
hideTextValue?: boolean;
2018-12-12 08:33:49 +01:00
onBlur?: () => void;
autoFocus?: boolean;
openMenuOnFocus?: boolean;
showLoading?: boolean;
Migration: Migrate Dashboard Import to React (#22338) * first things * introduce headers and moving buttons * adding reducer and action for gcom dashboard * action working * continue building on import form * change dashboard title * add prop to not render a label * first things * introduce headers and moving buttons * adding reducer and action for gcom dashboard * action working * continue building on import form * change dashboard title * add prop to not render a label * import form layout * break out form to component * add actions and reader for file upload * fix upload issue * modified data types to handle both gcom and file upload * import dashboard json * save dashboard * start change uid * change dashboard uid * fix spacing and date format * fix import from json * handle uid and title change * revert change in panelinspect * redo fileupload component * after review * redo forms to use Forms functionality * first attempt on async validation * use ternary on uid input * removed unused actions, fixed async validation on form * post form if invalid, break out form to component * sync file with master * fix after merge * nits * export formapi type * redo page to use forms validation * fix inputs and validation * readd post * add guards on data source and constants * type checks and strict nulls * strict nulls * validate onchange and fix import button when valid * shorten validate call * reexport OnSubmit type * add comment for overwrite useEffect * move validation functions to util * fix button imports * remove angular import * move title and uid validation
2020-03-31 16:29:44 +02:00
placeholder?: string;
invalid?: boolean;
}
2018-11-09 15:49:55 +01:00
2018-12-11 18:49:48 +01:00
export class DataSourcePicker extends PureComponent<Props> {
static defaultProps: Partial<Props> = {
2018-12-12 08:33:49 +01:00
autoFocus: false,
openMenuOnFocus: false,
Migration: Migrate Dashboard Import to React (#22338) * first things * introduce headers and moving buttons * adding reducer and action for gcom dashboard * action working * continue building on import form * change dashboard title * add prop to not render a label * first things * introduce headers and moving buttons * adding reducer and action for gcom dashboard * action working * continue building on import form * change dashboard title * add prop to not render a label * import form layout * break out form to component * add actions and reader for file upload * fix upload issue * modified data types to handle both gcom and file upload * import dashboard json * save dashboard * start change uid * change dashboard uid * fix spacing and date format * fix import from json * handle uid and title change * revert change in panelinspect * redo fileupload component * after review * redo forms to use Forms functionality * first attempt on async validation * use ternary on uid input * removed unused actions, fixed async validation on form * post form if invalid, break out form to component * sync file with master * fix after merge * nits * export formapi type * redo page to use forms validation * fix inputs and validation * readd post * add guards on data source and constants * type checks and strict nulls * strict nulls * validate onchange and fix import button when valid * shorten validate call * reexport OnSubmit type * add comment for overwrite useEffect * move validation functions to util * fix button imports * remove angular import * move title and uid validation
2020-03-31 16:29:44 +02:00
placeholder: 'Select datasource',
2018-12-12 08:33:49 +01:00
};
searchInput: HTMLElement;
2018-11-09 15:49:55 +01:00
constructor(props: Props) {
super(props);
}
2018-11-09 15:49:55 +01:00
onChange = (item: SelectableValue<string>) => {
2018-12-11 18:49:48 +01:00
const ds = this.props.datasources.find(ds => ds.name === item.value);
Migration: Migrate Dashboard Import to React (#22338) * first things * introduce headers and moving buttons * adding reducer and action for gcom dashboard * action working * continue building on import form * change dashboard title * add prop to not render a label * first things * introduce headers and moving buttons * adding reducer and action for gcom dashboard * action working * continue building on import form * change dashboard title * add prop to not render a label * import form layout * break out form to component * add actions and reader for file upload * fix upload issue * modified data types to handle both gcom and file upload * import dashboard json * save dashboard * start change uid * change dashboard uid * fix spacing and date format * fix import from json * handle uid and title change * revert change in panelinspect * redo fileupload component * after review * redo forms to use Forms functionality * first attempt on async validation * use ternary on uid input * removed unused actions, fixed async validation on form * post form if invalid, break out form to component * sync file with master * fix after merge * nits * export formapi type * redo page to use forms validation * fix inputs and validation * readd post * add guards on data source and constants * type checks and strict nulls * strict nulls * validate onchange and fix import button when valid * shorten validate call * reexport OnSubmit type * add comment for overwrite useEffect * move validation functions to util * fix button imports * remove angular import * move title and uid validation
2020-03-31 16:29:44 +02:00
if (ds) {
this.props.onChange(ds);
}
};
2018-11-09 15:49:55 +01:00
2018-12-11 18:49:48 +01:00
render() {
Migration: Migrate Dashboard Import to React (#22338) * first things * introduce headers and moving buttons * adding reducer and action for gcom dashboard * action working * continue building on import form * change dashboard title * add prop to not render a label * first things * introduce headers and moving buttons * adding reducer and action for gcom dashboard * action working * continue building on import form * change dashboard title * add prop to not render a label * import form layout * break out form to component * add actions and reader for file upload * fix upload issue * modified data types to handle both gcom and file upload * import dashboard json * save dashboard * start change uid * change dashboard uid * fix spacing and date format * fix import from json * handle uid and title change * revert change in panelinspect * redo fileupload component * after review * redo forms to use Forms functionality * first attempt on async validation * use ternary on uid input * removed unused actions, fixed async validation on form * post form if invalid, break out form to component * sync file with master * fix after merge * nits * export formapi type * redo page to use forms validation * fix inputs and validation * readd post * add guards on data source and constants * type checks and strict nulls * strict nulls * validate onchange and fix import button when valid * shorten validate call * reexport OnSubmit type * add comment for overwrite useEffect * move validation functions to util * fix button imports * remove angular import * move title and uid validation
2020-03-31 16:29:44 +02:00
const {
datasources,
current,
autoFocus,
hideTextValue,
onBlur,
openMenuOnFocus,
showLoading,
placeholder,
invalid,
Migration: Migrate Dashboard Import to React (#22338) * first things * introduce headers and moving buttons * adding reducer and action for gcom dashboard * action working * continue building on import form * change dashboard title * add prop to not render a label * first things * introduce headers and moving buttons * adding reducer and action for gcom dashboard * action working * continue building on import form * change dashboard title * add prop to not render a label * import form layout * break out form to component * add actions and reader for file upload * fix upload issue * modified data types to handle both gcom and file upload * import dashboard json * save dashboard * start change uid * change dashboard uid * fix spacing and date format * fix import from json * handle uid and title change * revert change in panelinspect * redo fileupload component * after review * redo forms to use Forms functionality * first attempt on async validation * use ternary on uid input * removed unused actions, fixed async validation on form * post form if invalid, break out form to component * sync file with master * fix after merge * nits * export formapi type * redo page to use forms validation * fix inputs and validation * readd post * add guards on data source and constants * type checks and strict nulls * strict nulls * validate onchange and fix import button when valid * shorten validate call * reexport OnSubmit type * add comment for overwrite useEffect * move validation functions to util * fix button imports * remove angular import * move title and uid validation
2020-03-31 16:29:44 +02:00
} = this.props;
2018-11-09 15:49:55 +01:00
2018-12-11 18:49:48 +01:00
const options = datasources.map(ds => ({
value: ds.name,
label: ds.name,
2018-12-11 22:17:32 +01:00
imgUrl: ds.meta.info.logos.small,
}));
2018-11-09 15:49:55 +01:00
2018-12-12 08:33:49 +01:00
const value = current && {
label: current.name.substr(0, 37),
2018-12-11 22:17:32 +01:00
value: current.name,
imgUrl: current.meta.info.logos.small,
loading: showLoading,
hideText: hideTextValue,
2018-12-11 22:17:32 +01:00
};
2018-12-11 18:49:48 +01:00
return (
<div aria-label={selectors.components.DataSourcePicker.container}>
<Select
className="ds-picker select-container"
isMulti={false}
isClearable={false}
backspaceRemovesValue={false}
onChange={this.onChange}
options={options}
autoFocus={autoFocus}
onBlur={onBlur}
openMenuOnFocus={openMenuOnFocus}
maxMenuHeight={500}
menuPlacement="bottom"
placeholder={placeholder}
noOptionsMessage="No datasources found"
value={value}
invalid={invalid}
/>
</div>
);
2018-11-09 15:49:55 +01:00
}
}
2018-11-09 15:49:55 +01:00
export default DataSourcePicker;