mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* Select: Don't portal by default * Select: Portal all the Selects * Fix indendentation in this comment * Select: Remove @example docs until formatting is correct * Docs: Add some documentation for the Select changes * Update docs/sources/whatsnew/whats-new-in-v8-1.md Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> * Update docs/sources/whatsnew/whats-new-in-v8-1.md Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> * Update packages/grafana-ui/src/components/Select/types.ts Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> * Update public/app/core/components/TransformersUI/configFromQuery/ConfigFromQueryTransformerEditor.tsx Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> * Update public/app/core/components/TransformersUI/configFromQuery/ConfigFromQueryTransformerEditor.tsx Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> * Update public/app/core/components/TransformersUI/configFromQuery/ConfigFromQueryTransformerEditor.tsx Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> * Update public/app/core/components/TransformersUI/prepareTimeSeries/PrepareTimeSeriesEditor.tsx Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> * Docs: Variants instead of varients * Update public/app/core/components/TransformersUI/configFromQuery/ConfigFromQueryTransformerEditor.tsx Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com> Co-authored-by: Ursula Kallio <73951760+osg-grafana@users.noreply.github.com>
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
import { SelectableValue } from '@grafana/data';
|
|
import { Field, InputControl, Select } from '@grafana/ui';
|
|
import { ExpressionDatasourceID } from 'app/features/expressions/ExpressionDatasource';
|
|
import React, { FC, useEffect, useMemo } from 'react';
|
|
import { useFormContext } from 'react-hook-form';
|
|
import { RuleFormValues } from '../../types/rule-form';
|
|
|
|
export const ConditionField: FC = () => {
|
|
const {
|
|
watch,
|
|
setValue,
|
|
formState: { errors },
|
|
} = useFormContext<RuleFormValues>();
|
|
|
|
const queries = watch('queries');
|
|
const condition = watch('condition');
|
|
|
|
const options = useMemo(
|
|
(): SelectableValue[] =>
|
|
queries
|
|
.filter((q) => !!q.refId)
|
|
.map((q) => ({
|
|
value: q.refId,
|
|
label: q.refId,
|
|
})),
|
|
[queries]
|
|
);
|
|
|
|
// reset condition if option no longer exists or if it is unset, but there are options available
|
|
useEffect(() => {
|
|
const expressions = queries.filter((query) => query.model.datasource === ExpressionDatasourceID);
|
|
if (condition && !options.find(({ value }) => value === condition)) {
|
|
setValue('condition', expressions.length ? expressions[expressions.length - 1].refId : null);
|
|
} else if (!condition && expressions.length) {
|
|
setValue('condition', expressions[expressions.length - 1].refId);
|
|
}
|
|
}, [condition, options, queries, setValue]);
|
|
|
|
return (
|
|
<Field
|
|
label="Condition"
|
|
description="The query or expression that will be alerted on"
|
|
error={errors.condition?.message}
|
|
invalid={!!errors.condition?.message}
|
|
>
|
|
<InputControl
|
|
name="condition"
|
|
render={({ field: { onChange, ref, ...field } }) => (
|
|
<Select
|
|
menuShouldPortal
|
|
{...field}
|
|
width={42}
|
|
options={options}
|
|
onChange={(v: SelectableValue) => onChange(v?.value ?? null)}
|
|
noOptionsMessage="No queries defined"
|
|
/>
|
|
)}
|
|
rules={{
|
|
required: {
|
|
value: true,
|
|
message: 'Please select the condition to alert on',
|
|
},
|
|
}}
|
|
/>
|
|
</Field>
|
|
);
|
|
};
|