diff --git a/public/app/features/alerting/unified/components/notification-policies/EditDefaultPolicyForm.tsx b/public/app/features/alerting/unified/components/notification-policies/EditDefaultPolicyForm.tsx index fbfab072ad5..2ae8f5f58d7 100644 --- a/public/app/features/alerting/unified/components/notification-policies/EditDefaultPolicyForm.tsx +++ b/public/app/features/alerting/unified/components/notification-policies/EditDefaultPolicyForm.tsx @@ -1,6 +1,7 @@ import React, { ReactNode, useState } from 'react'; +import { useForm, Controller } from 'react-hook-form'; -import { Collapse, Field, Form, InputControl, Link, MultiSelect, Select, useStyles2 } from '@grafana/ui'; +import { Collapse, Field, Link, MultiSelect, Select, useStyles2 } from '@grafana/ui'; import { RouteWithID } from 'app/plugins/datasource/alertmanager/types'; import { FormAmRoute } from '../../types/amroutes'; @@ -41,125 +42,131 @@ export const AmRootRouteForm = ({ const [groupByOptions, setGroupByOptions] = useState(stringsToSelectableValues(route.group_by)); const defaultValues = amRouteToFormAmRoute(route); - + const { + handleSubmit, + register, + control, + formState: { errors }, + setValue, + getValues, + } = useForm({ + defaultValues: { + ...defaultValues, + overrideTimings: true, + overrideGrouping: true, + }, + }); return ( -
- {({ register, control, errors, setValue, getValues }) => ( + + <> - - <> -
- ( - - {/* @ts-ignore-check: react-hook-form made me do this */} - - {({ fields, append, remove }) => ( - <> - -
Matching labels
- {fields.length === 0 && ( - - )} - {fields.length > 0 && ( -
- {fields.map((field, index) => { - return ( - - - - - - ( - - - remove(index)}> - Remove - - - ); - })} -
- )} - + + + + ( + + + remove(index)}> + Remove +
+ ); + })} +
+ )} + + + + + ( + onChange(mapSelectValueToString(value))} - options={receiversWithOnCallOnTop} - isClearable - /> - )} - control={control} - name="receiver" - /> - - - - - - - - {watch().overrideGrouping && ( - - { - if (!value || value.length === 0) { - return 'At least one group by option is required.'; - } - return true; - }, - }} - render={({ field: { onChange, ref, ...field }, fieldState: { error } }) => ( - <> - { - setGroupByOptions((opts) => [...opts, stringToSelectableValue(opt)]); - - // @ts-ignore-check: react-hook-form made me do this - setValue('groupBy', [...field.value, opt]); - }} - onChange={(value) => onChange(mapMultiSelectValueToStrings(value))} - options={[...commonGroupByOptions, ...groupByOptions]} - /> - {error && {error.message}} - - )} - control={control} - name="groupBy" - /> - - )} - - - - {watch().overrideTimings && ( - <> - - - - - - - - { - const groupInterval = getValues('groupIntervalValue'); - return repeatIntervalValidator(value, groupInterval); - }, - })} - aria-label={routeTimingsFields.repeatInterval.ariaLabel} - className={formStyles.promDurationInput} - /> - - - )} + control={control} + name="groupBy" + /> +
+ )} + + + + {watch().overrideTimings && ( + <> - ( - onChange(mapMultiSelectValueToStrings(value))} - options={muteTimingOptions} - /> - )} - control={control} - name="muteTimeIntervals" + + + + + + + { + const groupInterval = getValues('groupIntervalValue'); + return repeatIntervalValidator(value, groupInterval); + }, + })} + aria-label={routeTimingsFields.repeatInterval.ariaLabel} + className={formStyles.promDurationInput} /> - {actionButtons} )} - + + ( + onChange(mapMultiSelectValueToStrings(value))} + options={muteTimingOptions} + /> + )} + control={control} + name="muteTimeIntervals" + /> + + {actionButtons} + ); };