Files
grafana/public/app/core/components/Select/DashboardPicker.tsx
Alex Khomenko 6f1a38cbe2 Variables: Extend option pickers to accept custom onChange callback (#30913)
* Rename DashboardPicker props

* Extend variable option pickers to allow providing custom onchage callback

* Update public/app/features/variables/pickers/OptionsPicker/actions.ts

Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>

* Update public/app/features/variables/pickers/OptionsPicker/actions.ts

Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>

* Update public/app/features/variables/pickers/OptionsPicker/actions.ts

Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>

* Update public/app/features/variables/textbox/TextBoxVariablePicker.tsx

Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>

* Codeformat

Co-authored-by: Hugo Häggmark <hugo.haggmark@grafana.com>
2021-02-12 16:00:12 +02:00

47 lines
1.3 KiB
TypeScript

import React, { FC } from 'react';
import debounce from 'debounce-promise';
import { SelectableValue } from '@grafana/data';
import { AsyncSelect } from '@grafana/ui';
import { backendSrv } from 'app/core/services/backend_srv';
import { DashboardSearchHit } from 'app/features/search/types';
import { DashboardDTO } from 'app/types';
export interface Props {
onChange: (dashboard: DashboardDTO) => void;
value?: SelectableValue;
width?: number;
isClearable?: boolean;
invalid?: boolean;
disabled?: boolean;
}
const getDashboards = (query = '') => {
return backendSrv.search({ type: 'dash-db', query }).then((result: DashboardSearchHit[]) => {
return result.map((item: DashboardSearchHit) => ({
id: item.id,
uid: item.uid,
value: item.id,
label: `${item?.folderTitle ?? 'General'}/${item.title}`,
}));
});
};
export const DashboardPicker: FC<Props> = ({ onChange, value, width, isClearable = false, invalid, disabled }) => {
const debouncedSearch = debounce(getDashboards, 300);
return (
<AsyncSelect
width={width}
isClearable={isClearable}
defaultOptions={true}
loadOptions={debouncedSearch}
onChange={onChange}
placeholder="Select dashboard"
noOptionsMessage="No dashboards found"
value={value}
invalid={invalid}
disabled={disabled}
/>
);
};