2023-03-14 07:38:21 -07:00
|
|
|
import React, { useMemo } from 'react';
|
2022-04-22 14:33:13 +01:00
|
|
|
|
|
|
|
|
import { SelectableValue } from '@grafana/data';
|
|
|
|
|
|
2021-05-17 10:39:42 +03:00
|
|
|
import { Annotation, annotationLabels } from '../../utils/constants';
|
2021-04-14 15:57:36 +03:00
|
|
|
|
2022-04-22 14:33:13 +01:00
|
|
|
import { SelectWithAdd } from './SelectWIthAdd';
|
|
|
|
|
|
2021-04-14 15:57:36 +03:00
|
|
|
interface Props {
|
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
|
existingKeys: string[];
|
|
|
|
|
|
|
|
|
|
value?: string;
|
|
|
|
|
width?: number;
|
|
|
|
|
className?: string;
|
2021-11-02 15:27:07 +00:00
|
|
|
'aria-label'?: string;
|
2021-04-14 15:57:36 +03:00
|
|
|
}
|
|
|
|
|
|
2023-03-14 07:38:21 -07:00
|
|
|
export const AnnotationKeyInput = ({ value, existingKeys, 'aria-label': ariaLabel, ...rest }: Props) => {
|
2021-04-14 15:57:36 +03:00
|
|
|
const annotationOptions = useMemo(
|
2021-04-16 14:57:33 +03:00
|
|
|
(): SelectableValue[] =>
|
2021-05-17 10:39:42 +03:00
|
|
|
Object.values(Annotation)
|
|
|
|
|
.filter((key) => !existingKeys.includes(key)) // remove keys already taken in other annotations
|
|
|
|
|
.map((key) => ({ value: key, label: annotationLabels[key] })),
|
2021-04-14 15:57:36 +03:00
|
|
|
[existingKeys]
|
|
|
|
|
);
|
|
|
|
|
|
2021-04-16 14:57:33 +03:00
|
|
|
return (
|
|
|
|
|
<SelectWithAdd
|
2021-11-02 15:27:07 +00:00
|
|
|
aria-label={ariaLabel}
|
2021-04-16 14:57:33 +03:00
|
|
|
value={value}
|
|
|
|
|
options={annotationOptions}
|
2021-05-17 10:39:42 +03:00
|
|
|
custom={!!value && !(Object.values(Annotation) as string[]).includes(value)}
|
2021-04-16 14:57:33 +03:00
|
|
|
{...rest}
|
|
|
|
|
/>
|
|
|
|
|
);
|
2021-04-14 15:57:36 +03:00
|
|
|
};
|