import React, { FC, useCallback } from 'react'; import { Button, Field, FieldArray, InputControl, Label, TextArea, useStyles } from '@grafana/ui'; import { GrafanaTheme } from '@grafana/data'; import { css, cx } from '@emotion/css'; import { useFormContext } from 'react-hook-form'; import { RuleFormValues } from '../../types/rule-form'; import { AnnotationKeyInput } from './AnnotationKeyInput'; const AnnotationsField: FC = () => { const styles = useStyles(getStyles); const { control, register, watch, formState: { errors }, } = useFormContext(); const annotations = watch('annotations') as RuleFormValues['annotations']; const existingKeys = useCallback( (index: number): string[] => annotations.filter((_, idx: number) => idx !== index).map(({ key }) => key), [annotations] ); return ( <> {({ fields, append, remove }) => { return (
{fields.map((field, index) => (
( )} control={control} rules={{ required: { value: !!annotations[index]?.value, message: 'Required.' } }} />