mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting/fix folder creation dropdown (#54687)
*Refactor FolderPicker to be functional component. * Add customAdd optional property to FolderPicker so we can add new values in the alert view but keeping the same behaviour in the rest of the ui. * Add test for being able to add folder when no folders found.
This commit is contained in:
parent
ab774b47fb
commit
7a6f452f13
@ -2725,10 +2725,6 @@ exports[`better eslint`] = {
|
|||||||
"public/app/core/components/RolePicker/RolePickerMenu.tsx:5381": [
|
"public/app/core/components/RolePicker/RolePickerMenu.tsx:5381": [
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
|
[0, 0, 0, "Unexpected any. Specify a different type.", "0"]
|
||||||
],
|
],
|
||||||
"public/app/core/components/Select/FolderPicker.tsx:5381": [
|
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
|
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "1"]
|
|
||||||
],
|
|
||||||
"public/app/core/components/Select/MetricSelect.test.tsx:5381": [
|
"public/app/core/components/Select/MetricSelect.test.tsx:5381": [
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
|
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],
|
||||||
[0, 0, 0, "Unexpected any. Specify a different type.", "1"]
|
[0, 0, 0, "Unexpected any. Specify a different type.", "1"]
|
||||||
|
@ -1,25 +1,28 @@
|
|||||||
|
import { css } from '@emotion/css';
|
||||||
import { t } from '@lingui/macro';
|
import { t } from '@lingui/macro';
|
||||||
import { debounce } from 'lodash';
|
import { debounce } from 'lodash';
|
||||||
import React, { PureComponent } from 'react';
|
import React, { useState, useEffect, useMemo, useCallback, FormEvent } from 'react';
|
||||||
|
import { useAsync } from 'react-use';
|
||||||
|
|
||||||
import { AppEvents, SelectableValue } from '@grafana/data';
|
import { AppEvents, SelectableValue, GrafanaTheme2 } from '@grafana/data';
|
||||||
import { selectors } from '@grafana/e2e-selectors';
|
import { selectors } from '@grafana/e2e-selectors';
|
||||||
import { ActionMeta, AsyncSelect, LoadOptionsCallback } from '@grafana/ui';
|
import { useStyles2, ActionMeta, AsyncSelect, Input, InputActionMeta } from '@grafana/ui';
|
||||||
|
import appEvents from 'app/core/app_events';
|
||||||
import { contextSrv } from 'app/core/services/context_srv';
|
import { contextSrv } from 'app/core/services/context_srv';
|
||||||
import { createFolder, getFolderById, searchFolders } from 'app/features/manage-dashboards/state/actions';
|
import { createFolder, getFolderById, searchFolders } from 'app/features/manage-dashboards/state/actions';
|
||||||
import { DashboardSearchHit } from 'app/features/search/types';
|
import { DashboardSearchHit } from 'app/features/search/types';
|
||||||
|
import { AccessControlAction, PermissionLevelString } from 'app/types';
|
||||||
import { AccessControlAction, PermissionLevelString } from '../../../types';
|
|
||||||
import appEvents from '../../app_events';
|
|
||||||
|
|
||||||
export type FolderPickerFilter = (hits: DashboardSearchHit[]) => DashboardSearchHit[];
|
export type FolderPickerFilter = (hits: DashboardSearchHit[]) => DashboardSearchHit[];
|
||||||
|
|
||||||
|
export const ADD_NEW_FOLER_OPTION = '+ Add new';
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
onChange: ($folder: { title: string; id: number }) => void;
|
onChange: ($folder: { title: string; id: number }) => void;
|
||||||
enableCreateNew?: boolean;
|
enableCreateNew?: boolean;
|
||||||
rootName?: string;
|
rootName?: string;
|
||||||
enableReset?: boolean;
|
enableReset?: boolean;
|
||||||
dashboardId?: any;
|
dashboardId?: number | string;
|
||||||
initialTitle?: string;
|
initialTitle?: string;
|
||||||
initialFolderId?: number;
|
initialFolderId?: number;
|
||||||
permissionLevel?: Exclude<PermissionLevelString, PermissionLevelString.Admin>;
|
permissionLevel?: Exclude<PermissionLevelString, PermissionLevelString.Admin>;
|
||||||
@ -28,6 +31,7 @@ export interface Props {
|
|||||||
showRoot?: boolean;
|
showRoot?: boolean;
|
||||||
onClear?: () => void;
|
onClear?: () => void;
|
||||||
accessControlMetadata?: boolean;
|
accessControlMetadata?: boolean;
|
||||||
|
customAdd?: boolean;
|
||||||
/**
|
/**
|
||||||
* Skips loading all folders in order to find the folder matching
|
* Skips loading all folders in order to find the folder matching
|
||||||
* the folder where the dashboard is stored.
|
* the folder where the dashboard is stored.
|
||||||
@ -38,139 +42,84 @@ export interface Props {
|
|||||||
/** The id of the search input. Use this to set a matching label with htmlFor */
|
/** The id of the search input. Use this to set a matching label with htmlFor */
|
||||||
inputId?: string;
|
inputId?: string;
|
||||||
}
|
}
|
||||||
|
export type SelectedFolder = SelectableValue<number>;
|
||||||
|
const VALUE_FOR_ADD = -10;
|
||||||
|
|
||||||
interface State {
|
export function FolderPicker(props: Props) {
|
||||||
folder: SelectableValue<number> | null;
|
const {
|
||||||
}
|
dashboardId,
|
||||||
|
allowEmpty,
|
||||||
|
onChange,
|
||||||
|
filter,
|
||||||
|
enableCreateNew,
|
||||||
|
inputId,
|
||||||
|
onClear,
|
||||||
|
enableReset,
|
||||||
|
initialFolderId,
|
||||||
|
initialTitle,
|
||||||
|
permissionLevel,
|
||||||
|
rootName,
|
||||||
|
showRoot,
|
||||||
|
skipInitialLoad,
|
||||||
|
accessControlMetadata,
|
||||||
|
customAdd,
|
||||||
|
} = props;
|
||||||
|
const isClearable = typeof onClear === 'function';
|
||||||
|
const [folder, setFolder] = useState<SelectedFolder | null>(null);
|
||||||
|
const [isCreatingNew, setIsCreatingNew] = useState(false);
|
||||||
|
const styles = useStyles2(getStyles);
|
||||||
|
const [inputValue, setInputValue] = useState('');
|
||||||
|
|
||||||
export class FolderPicker extends PureComponent<Props, State> {
|
const getOptions = useCallback(
|
||||||
debouncedSearch: any;
|
async (query: string) => {
|
||||||
|
const searchHits = await searchFolders(query, permissionLevel, accessControlMetadata);
|
||||||
|
const options: Array<SelectableValue<number>> = mapSearchHitsToOptions(searchHits, filter);
|
||||||
|
|
||||||
constructor(props: Props) {
|
const hasAccess =
|
||||||
super(props);
|
contextSrv.hasAccess(AccessControlAction.DashboardsWrite, contextSrv.isEditor) ||
|
||||||
|
contextSrv.hasAccess(AccessControlAction.DashboardsCreate, contextSrv.isEditor);
|
||||||
|
|
||||||
this.state = {
|
if (hasAccess && rootName?.toLowerCase().startsWith(query.toLowerCase()) && showRoot) {
|
||||||
folder: null,
|
options.unshift({ label: rootName, value: 0 });
|
||||||
};
|
}
|
||||||
|
|
||||||
this.debouncedSearch = debounce(this.loadOptions, 300, {
|
if (
|
||||||
leading: true,
|
enableReset &&
|
||||||
trailing: true,
|
query === '' &&
|
||||||
});
|
initialTitle !== '' &&
|
||||||
}
|
!options.find((option) => option.label === initialTitle)
|
||||||
|
) {
|
||||||
static defaultProps: Partial<Props> = {
|
Boolean(initialTitle) && options.unshift({ label: initialTitle, value: initialFolderId });
|
||||||
rootName: 'General',
|
}
|
||||||
enableReset: false,
|
if (enableCreateNew && customAdd) {
|
||||||
initialTitle: '',
|
return [...options, { value: VALUE_FOR_ADD, label: ADD_NEW_FOLER_OPTION, title: query }];
|
||||||
enableCreateNew: false,
|
} else {
|
||||||
permissionLevel: PermissionLevelString.Edit,
|
return options;
|
||||||
allowEmpty: false,
|
}
|
||||||
showRoot: true,
|
},
|
||||||
};
|
[
|
||||||
|
|
||||||
componentDidMount = async () => {
|
|
||||||
if (this.props.skipInitialLoad) {
|
|
||||||
const folder = await getInitialValues({
|
|
||||||
getFolder: getFolderById,
|
|
||||||
folderId: this.props.initialFolderId,
|
|
||||||
folderName: this.props.initialTitle,
|
|
||||||
});
|
|
||||||
this.setState({ folder });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await this.loadInitialValue();
|
|
||||||
};
|
|
||||||
|
|
||||||
// when debouncing, we must use the callback form of react-select's loadOptions so we don't
|
|
||||||
// drop results for user input. This must not return a promise/use await.
|
|
||||||
loadOptions = (query: string, callback: LoadOptionsCallback<number>): void => {
|
|
||||||
this.searchFolders(query).then(callback);
|
|
||||||
};
|
|
||||||
|
|
||||||
private searchFolders = async (query: string) => {
|
|
||||||
const {
|
|
||||||
rootName,
|
|
||||||
enableReset,
|
enableReset,
|
||||||
|
initialFolderId,
|
||||||
initialTitle,
|
initialTitle,
|
||||||
permissionLevel,
|
permissionLevel,
|
||||||
filter,
|
rootName,
|
||||||
accessControlMetadata,
|
|
||||||
initialFolderId,
|
|
||||||
showRoot,
|
showRoot,
|
||||||
} = this.props;
|
accessControlMetadata,
|
||||||
|
filter,
|
||||||
|
enableCreateNew,
|
||||||
|
customAdd,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
const searchHits = await searchFolders(query, permissionLevel, accessControlMetadata);
|
const debouncedSearch = useMemo(() => {
|
||||||
const options: Array<SelectableValue<number>> = mapSearchHitsToOptions(searchHits, filter);
|
return debounce(getOptions, 300, { leading: true });
|
||||||
|
}, [getOptions]);
|
||||||
|
|
||||||
const hasAccess =
|
const loadInitialValue = async () => {
|
||||||
contextSrv.hasAccess(AccessControlAction.DashboardsWrite, contextSrv.isEditor) ||
|
|
||||||
contextSrv.hasAccess(AccessControlAction.DashboardsCreate, contextSrv.isEditor);
|
|
||||||
|
|
||||||
if (hasAccess && rootName?.toLowerCase().startsWith(query.toLowerCase()) && showRoot) {
|
|
||||||
options.unshift({ label: rootName, value: 0 });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
enableReset &&
|
|
||||||
query === '' &&
|
|
||||||
initialTitle !== '' &&
|
|
||||||
!options.find((option) => option.label === initialTitle)
|
|
||||||
) {
|
|
||||||
options.unshift({ label: initialTitle, value: initialFolderId });
|
|
||||||
}
|
|
||||||
|
|
||||||
return options;
|
|
||||||
};
|
|
||||||
|
|
||||||
onFolderChange = (newFolder: SelectableValue<number>, actionMeta: ActionMeta) => {
|
|
||||||
if (!newFolder) {
|
|
||||||
newFolder = { value: 0, label: this.props.rootName };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (actionMeta.action === 'clear' && this.props.onClear) {
|
|
||||||
this.props.onClear();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.setState(
|
|
||||||
{
|
|
||||||
folder: newFolder,
|
|
||||||
},
|
|
||||||
() => this.props.onChange({ id: newFolder.value!, title: newFolder.label! })
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
createNewFolder = async (folderName: string) => {
|
|
||||||
const newFolder = await createFolder({ title: folderName });
|
|
||||||
let folder: SelectableValue<number> = { value: -1, label: 'Not created' };
|
|
||||||
|
|
||||||
if (newFolder.id > -1) {
|
|
||||||
appEvents.emit(AppEvents.alertSuccess, ['Folder Created', 'OK']);
|
|
||||||
folder = { value: newFolder.id, label: newFolder.title };
|
|
||||||
|
|
||||||
this.setState(
|
|
||||||
{
|
|
||||||
folder: newFolder,
|
|
||||||
},
|
|
||||||
() => {
|
|
||||||
this.onFolderChange(folder, { action: 'create-option', option: folder });
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
appEvents.emit(AppEvents.alertError, ['Folder could not be created']);
|
|
||||||
}
|
|
||||||
|
|
||||||
return folder;
|
|
||||||
};
|
|
||||||
|
|
||||||
private loadInitialValue = async () => {
|
|
||||||
const { initialTitle, rootName, initialFolderId, enableReset, dashboardId } = this.props;
|
|
||||||
const resetFolder: SelectableValue<number> = { label: initialTitle, value: undefined };
|
const resetFolder: SelectableValue<number> = { label: initialTitle, value: undefined };
|
||||||
const rootFolder: SelectableValue<number> = { label: rootName, value: 0 };
|
const rootFolder: SelectableValue<number> = { label: rootName, value: 0 };
|
||||||
|
|
||||||
const options = await this.searchFolders('');
|
const options = await getOptions('');
|
||||||
|
|
||||||
let folder: SelectableValue<number> | null = null;
|
let folder: SelectableValue<number> | null = null;
|
||||||
|
|
||||||
@ -182,7 +131,7 @@ export class FolderPicker extends PureComponent<Props, State> {
|
|||||||
folder = options.find((option) => option.id === initialFolderId) || null;
|
folder = options.find((option) => option.id === initialFolderId) || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!folder && !this.props.allowEmpty) {
|
if (!folder && !allowEmpty) {
|
||||||
if (contextSrv.isEditor) {
|
if (contextSrv.isEditor) {
|
||||||
folder = rootFolder;
|
folder = rootFolder;
|
||||||
} else {
|
} else {
|
||||||
@ -195,25 +144,137 @@ export class FolderPicker extends PureComponent<Props, State> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
!isCreatingNew && setFolder(folder);
|
||||||
this.setState(
|
|
||||||
{
|
|
||||||
folder,
|
|
||||||
},
|
|
||||||
() => {
|
|
||||||
// if this is not the same as our initial value notify parent
|
|
||||||
if (folder && folder.value !== initialFolderId) {
|
|
||||||
this.props.onChange({ id: folder.value!, title: folder.label! });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
render() {
|
useEffect(() => {
|
||||||
const { folder } = this.state;
|
// if this is not the same as our initial value notify parent
|
||||||
const { enableCreateNew, inputId, onClear } = this.props;
|
if (folder && folder.value !== initialFolderId) {
|
||||||
const isClearable = typeof onClear === 'function';
|
!isCreatingNew && onChange({ id: folder.value!, title: folder.label! });
|
||||||
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
|
}, [folder, initialFolderId]);
|
||||||
|
|
||||||
|
// initial values for dropdown
|
||||||
|
useAsync(async () => {
|
||||||
|
if (skipInitialLoad) {
|
||||||
|
const folder = await getInitialValues({
|
||||||
|
getFolder: getFolderById,
|
||||||
|
folderId: initialFolderId,
|
||||||
|
folderName: initialTitle,
|
||||||
|
});
|
||||||
|
setFolder(folder);
|
||||||
|
}
|
||||||
|
|
||||||
|
await loadInitialValue();
|
||||||
|
}, [skipInitialLoad, initialFolderId, initialTitle]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (folder && folder.id === VALUE_FOR_ADD) {
|
||||||
|
setIsCreatingNew(true);
|
||||||
|
}
|
||||||
|
}, [folder]);
|
||||||
|
|
||||||
|
const onFolderChange = useCallback(
|
||||||
|
(newFolder: SelectableValue<number>, actionMeta: ActionMeta) => {
|
||||||
|
const value = newFolder.value;
|
||||||
|
if (value === VALUE_FOR_ADD) {
|
||||||
|
setFolder({
|
||||||
|
id: VALUE_FOR_ADD,
|
||||||
|
title: inputValue,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
if (!newFolder) {
|
||||||
|
newFolder = { value: 0, label: rootName };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (actionMeta.action === 'clear' && onClear) {
|
||||||
|
onClear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setFolder(newFolder);
|
||||||
|
onChange({ id: newFolder.value!, title: newFolder.label! });
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[onChange, onClear, rootName, inputValue]
|
||||||
|
);
|
||||||
|
|
||||||
|
const createNewFolder = useCallback(
|
||||||
|
async (folderName: string) => {
|
||||||
|
const newFolder = await createFolder({ title: folderName });
|
||||||
|
let folder: SelectableValue<number> = { value: -1, label: 'Not created' };
|
||||||
|
|
||||||
|
if (newFolder.id > -1) {
|
||||||
|
appEvents.emit(AppEvents.alertSuccess, ['Folder Created', 'OK']);
|
||||||
|
folder = { value: newFolder.id, label: newFolder.title };
|
||||||
|
|
||||||
|
setFolder(newFolder);
|
||||||
|
onFolderChange(folder, { action: 'create-option', option: folder });
|
||||||
|
} else {
|
||||||
|
appEvents.emit(AppEvents.alertError, ['Folder could not be created']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return folder;
|
||||||
|
},
|
||||||
|
[onFolderChange]
|
||||||
|
);
|
||||||
|
|
||||||
|
const onKeyDown = useCallback(
|
||||||
|
(event: React.KeyboardEvent) => {
|
||||||
|
switch (event.key) {
|
||||||
|
case 'Enter': {
|
||||||
|
createNewFolder(folder?.title!);
|
||||||
|
setIsCreatingNew(false);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 'Escape': {
|
||||||
|
setFolder({ value: 0, label: rootName });
|
||||||
|
setIsCreatingNew(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[createNewFolder, folder, rootName]
|
||||||
|
);
|
||||||
|
|
||||||
|
const onNewFolderChange = (e: FormEvent<HTMLInputElement>) => {
|
||||||
|
const value = e.currentTarget.value;
|
||||||
|
setFolder({ id: -1, title: value });
|
||||||
|
};
|
||||||
|
|
||||||
|
const onBlur = () => {
|
||||||
|
setFolder({ value: 0, label: rootName });
|
||||||
|
setIsCreatingNew(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
const onInputChange = (value: string, { action }: InputActionMeta) => {
|
||||||
|
if (action === 'input-change') {
|
||||||
|
setInputValue((ant) => value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action === 'menu-close') {
|
||||||
|
setInputValue((_) => value);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (isCreatingNew) {
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Input
|
||||||
|
aria-label={'aria-label'}
|
||||||
|
width={30}
|
||||||
|
autoFocus={true}
|
||||||
|
value={folder?.title || ''}
|
||||||
|
onChange={onNewFolderChange}
|
||||||
|
onKeyDown={onKeyDown}
|
||||||
|
placeholder="Press enter to confirm new folder."
|
||||||
|
onBlur={onBlur}
|
||||||
|
/>
|
||||||
|
<div className={styles.newFolder}>Press enter to create the new folder.</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
} else {
|
||||||
return (
|
return (
|
||||||
<div data-testid={selectors.components.FolderPicker.containerV2}>
|
<div data-testid={selectors.components.FolderPicker.containerV2}>
|
||||||
<AsyncSelect
|
<AsyncSelect
|
||||||
@ -222,11 +283,13 @@ export class FolderPicker extends PureComponent<Props, State> {
|
|||||||
loadingMessage={t({ id: 'folder-picker.loading', message: 'Loading folders...' })}
|
loadingMessage={t({ id: 'folder-picker.loading', message: 'Loading folders...' })}
|
||||||
defaultOptions
|
defaultOptions
|
||||||
defaultValue={folder}
|
defaultValue={folder}
|
||||||
|
inputValue={inputValue}
|
||||||
|
onInputChange={onInputChange}
|
||||||
value={folder}
|
value={folder}
|
||||||
allowCustomValue={enableCreateNew}
|
allowCustomValue={enableCreateNew && !customAdd}
|
||||||
loadOptions={this.debouncedSearch}
|
loadOptions={debouncedSearch}
|
||||||
onChange={this.onFolderChange}
|
onChange={onFolderChange}
|
||||||
onCreateOption={this.createNewFolder}
|
onCreateOption={createNewFolder}
|
||||||
isClearable={isClearable}
|
isClearable={isClearable}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@ -238,7 +301,6 @@ function mapSearchHitsToOptions(hits: DashboardSearchHit[], filter?: FolderPicke
|
|||||||
const filteredHits = filter ? filter(hits) : hits;
|
const filteredHits = filter ? filter(hits) : hits;
|
||||||
return filteredHits.map((hit) => ({ label: hit.title, value: hit.id }));
|
return filteredHits.map((hit) => ({ label: hit.title, value: hit.id }));
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Args {
|
interface Args {
|
||||||
getFolder: typeof getFolderById;
|
getFolder: typeof getFolderById;
|
||||||
folderId?: number;
|
folderId?: number;
|
||||||
@ -257,3 +319,11 @@ export async function getInitialValues({ folderName, folderId, getFolder }: Args
|
|||||||
const folderDto = await getFolder(folderId);
|
const folderDto = await getFolder(folderId);
|
||||||
return { label: folderDto.title, value: folderId };
|
return { label: folderDto.title, value: folderId };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getStyles = (theme: GrafanaTheme2) => ({
|
||||||
|
newFolder: css`
|
||||||
|
color: ${theme.colors.warning.main};
|
||||||
|
font-size: ${theme.typography.bodySmall.fontSize};
|
||||||
|
padding-top: ${theme.spacing(1)};
|
||||||
|
`,
|
||||||
|
});
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { Matcher, render, waitFor, screen } from '@testing-library/react';
|
import { Matcher, render, waitFor, screen, within } from '@testing-library/react';
|
||||||
import userEvent, { PointerEventsCheckLevel } from '@testing-library/user-event';
|
import userEvent, { PointerEventsCheckLevel } from '@testing-library/user-event';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { Provider } from 'react-redux';
|
import { Provider } from 'react-redux';
|
||||||
@ -7,7 +7,9 @@ import { selectOptionInTest } from 'test/helpers/selectOptionInTest';
|
|||||||
import { byLabelText, byRole, byTestId, byText } from 'testing-library-selector';
|
import { byLabelText, byRole, byTestId, byText } from 'testing-library-selector';
|
||||||
|
|
||||||
import { DataSourceInstanceSettings } from '@grafana/data';
|
import { DataSourceInstanceSettings } from '@grafana/data';
|
||||||
|
import { selectors } from '@grafana/e2e-selectors';
|
||||||
import { locationService, setDataSourceSrv } from '@grafana/runtime';
|
import { locationService, setDataSourceSrv } from '@grafana/runtime';
|
||||||
|
import { ADD_NEW_FOLER_OPTION } from 'app/core/components/Select/FolderPicker';
|
||||||
import { contextSrv } from 'app/core/services/context_srv';
|
import { contextSrv } from 'app/core/services/context_srv';
|
||||||
import { DashboardSearchHit } from 'app/features/search/types';
|
import { DashboardSearchHit } from 'app/features/search/types';
|
||||||
import { configureStore } from 'app/store/configureStore';
|
import { configureStore } from 'app/store/configureStore';
|
||||||
@ -79,6 +81,7 @@ const ui = {
|
|||||||
dataSource: byTestId('datasource-picker'),
|
dataSource: byTestId('datasource-picker'),
|
||||||
folder: byTestId('folder-picker'),
|
folder: byTestId('folder-picker'),
|
||||||
namespace: byTestId('namespace-picker'),
|
namespace: byTestId('namespace-picker'),
|
||||||
|
folderContainer: byTestId(selectors.components.FolderPicker.containerV2),
|
||||||
group: byTestId('group-picker'),
|
group: byTestId('group-picker'),
|
||||||
annotationKey: (idx: number) => byTestId(`annotation-key-${idx}`),
|
annotationKey: (idx: number) => byTestId(`annotation-key-${idx}`),
|
||||||
annotationValue: (idx: number) => byTestId(`annotation-value-${idx}`),
|
annotationValue: (idx: number) => byTestId(`annotation-value-${idx}`),
|
||||||
@ -480,6 +483,13 @@ describe('RuleEditor', () => {
|
|||||||
await userEvent.click(ui.buttons.save.get());
|
await userEvent.click(ui.buttons.save.get());
|
||||||
await waitFor(() => expect(mocks.api.setRulerRuleGroup).toHaveBeenCalled());
|
await waitFor(() => expect(mocks.api.setRulerRuleGroup).toHaveBeenCalled());
|
||||||
|
|
||||||
|
//check that '+ Add new' option is in folders drop down even if we don't have values
|
||||||
|
const folderInput = await ui.inputs.folderContainer.find();
|
||||||
|
mocks.searchFolders.mockResolvedValue([] as DashboardSearchHit[]);
|
||||||
|
await renderRuleEditor(uid);
|
||||||
|
await userEvent.click(within(folderInput).getByRole('combobox'));
|
||||||
|
expect(screen.getByText(ADD_NEW_FOLER_OPTION)).toBeInTheDocument();
|
||||||
|
|
||||||
expect(mocks.api.setRulerRuleGroup).toHaveBeenCalledWith(
|
expect(mocks.api.setRulerRuleGroup).toHaveBeenCalledWith(
|
||||||
{ dataSourceName: GRAFANA_RULES_SOURCE_NAME, apiVersion: 'legacy' },
|
{ dataSourceName: GRAFANA_RULES_SOURCE_NAME, apiVersion: 'legacy' },
|
||||||
'Folder A',
|
'Folder A',
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import React, { FC } from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
import { FolderPicker, Props as FolderPickerProps } from 'app/core/components/Select/FolderPicker';
|
import { FolderPicker, Props as FolderPickerProps } from 'app/core/components/Select/FolderPicker';
|
||||||
import { PermissionLevelString } from 'app/types';
|
import { PermissionLevelString } from 'app/types';
|
||||||
@ -12,7 +12,8 @@ export interface RuleFolderPickerProps extends Omit<FolderPickerProps, 'initialT
|
|||||||
value?: Folder;
|
value?: Folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const RuleFolderPicker: FC<RuleFolderPickerProps> = ({ value, ...props }) => {
|
export function RuleFolderPicker(props: RuleFolderPickerProps) {
|
||||||
|
const { value } = props;
|
||||||
return (
|
return (
|
||||||
<FolderPicker
|
<FolderPicker
|
||||||
showRoot={false}
|
showRoot={false}
|
||||||
@ -22,6 +23,7 @@ export const RuleFolderPicker: FC<RuleFolderPickerProps> = ({ value, ...props })
|
|||||||
accessControlMetadata
|
accessControlMetadata
|
||||||
{...props}
|
{...props}
|
||||||
permissionLevel={PermissionLevelString.View}
|
permissionLevel={PermissionLevelString.View}
|
||||||
|
customAdd={true}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user