diff --git a/package.json b/package.json index a4b96dc53c33..c0c98c0b21ac 100644 --- a/package.json +++ b/package.json @@ -378,7 +378,7 @@ "react-dropzone": "^14.2.3", "react-grid-layout": "1.4.2", "react-highlight-words": "0.20.0", - "react-hook-form": "7.5.3", + "react-hook-form": "^7.49.2", "react-i18next": "^12.0.0", "react-inlinesvg": "3.0.2", "react-loading-skeleton": "3.3.1", diff --git a/packages/grafana-ui/package.json b/packages/grafana-ui/package.json index 93b852f4f8bb..7f496e3718ff 100644 --- a/packages/grafana-ui/package.json +++ b/packages/grafana-ui/package.json @@ -92,7 +92,7 @@ "react-custom-scrollbars-2": "4.5.0", "react-dropzone": "14.2.3", "react-highlight-words": "0.20.0", - "react-hook-form": "7.5.3", + "react-hook-form": "^7.49.2", "react-i18next": "^12.0.0", "react-inlinesvg": "3.0.2", "react-loading-skeleton": "3.3.1", diff --git a/packages/grafana-ui/src/components/Forms/Form.tsx b/packages/grafana-ui/src/components/Forms/Form.tsx index d0362bd0824a..468ad8266e85 100644 --- a/packages/grafana-ui/src/components/Forms/Form.tsx +++ b/packages/grafana-ui/src/components/Forms/Form.tsx @@ -1,6 +1,6 @@ import { css } from '@emotion/css'; import React, { HTMLProps, useEffect } from 'react'; -import { useForm, Mode, DeepPartial, UnpackNestedValue, SubmitHandler, FieldValues } from 'react-hook-form'; +import { useForm, Mode, DefaultValues, SubmitHandler, FieldValues } from 'react-hook-form'; import { FormAPI } from '../../types'; @@ -8,7 +8,7 @@ interface FormProps extends Omit>; + defaultValues?: DefaultValues; onSubmit: SubmitHandler; children: (api: FormAPI) => React.ReactNode; /** Sets max-width for container. Use it instead of setting individual widths on inputs.*/ diff --git a/public/app/features/alerting/components/NotificationChannelOptions.tsx b/public/app/features/alerting/components/NotificationChannelOptions.tsx index c94fd9948934..80bd740fdcf9 100644 --- a/public/app/features/alerting/components/NotificationChannelOptions.tsx +++ b/public/app/features/alerting/components/NotificationChannelOptions.tsx @@ -58,7 +58,7 @@ export const NotificationChannelOptions = ({ label={option.label} description={option.description} invalid={errors.settings && !!errors.settings[option.propertyName]} - error={errors.settings && errors.settings[option.propertyName]?.message} + error={errors.settings && String(errors.settings[option.propertyName]?.message || '')} > {secureFields && secureFields[option.propertyName] ? ( { const styles = useStyles2(getStyles); - const { formState, register, setValue } = useFormContext(); + const { formState, register, setValue } = useFormContext(); const { fields: timeIntervals, append: addTimeInterval, remove: removeTimeInterval, - } = useFieldArray({ + } = useFieldArray({ name: 'time_intervals', }); @@ -43,7 +43,11 @@ export const MuteTimingTimeInterval = () => { return (
- + } width={50} diff --git a/public/app/features/alerting/unified/components/mute-timings/MuteTimingTimeRange.tsx b/public/app/features/alerting/unified/components/mute-timings/MuteTimingTimeRange.tsx index 9ce8a96faaea..89e05f13b779 100644 --- a/public/app/features/alerting/unified/components/mute-timings/MuteTimingTimeRange.tsx +++ b/public/app/features/alerting/unified/components/mute-timings/MuteTimingTimeRange.tsx @@ -28,7 +28,7 @@ export const MuteTimingTimeRange = ({ intervalIndex }: Props) => { }); const formErrors = formState.errors.time_intervals?.[intervalIndex]; - const timeRangeInvalid = formErrors?.times?.some((value) => value?.start_time || value?.end_time) ?? false; + const timeRangeInvalid = formErrors?.times?.some?.((value) => value?.start_time || value?.end_time) ?? false; return (
diff --git a/public/app/features/alerting/unified/components/notification-policies/EditNotificationPolicyForm.tsx b/public/app/features/alerting/unified/components/notification-policies/EditNotificationPolicyForm.tsx index d0f3e8c8b313..842c90c52dbe 100644 --- a/public/app/features/alerting/unified/components/notification-policies/EditNotificationPolicyForm.tsx +++ b/public/app/features/alerting/unified/components/notification-policies/EditNotificationPolicyForm.tsx @@ -258,7 +258,7 @@ export const AmRoutesExpandedForm = ({ > { + validate: (value = '') => { const groupInterval = getValues('groupIntervalValue'); return repeatIntervalValidator(value, groupInterval); }, diff --git a/public/app/features/alerting/unified/components/receivers/TemplateForm.tsx b/public/app/features/alerting/unified/components/receivers/TemplateForm.tsx index c8001c7dae57..e5319d49b40b 100644 --- a/public/app/features/alerting/unified/components/receivers/TemplateForm.tsx +++ b/public/app/features/alerting/unified/components/receivers/TemplateForm.tsx @@ -147,7 +147,7 @@ export const TemplateForm = ({ existing, alertManagerSourceName, config, provena watch, } = formApi; - const validateNameIsUnique: Validate = (name: string) => { + const validateNameIsUnique: Validate = (name: string) => { return !config.template_files[name] || existing?.name === name ? true : 'Another template with this name already exists.'; diff --git a/public/app/features/alerting/unified/components/receivers/form/ChannelSubForm.tsx b/public/app/features/alerting/unified/components/receivers/form/ChannelSubForm.tsx index 8fd3f9056a16..995b81b3c253 100644 --- a/public/app/features/alerting/unified/components/receivers/form/ChannelSubForm.tsx +++ b/public/app/features/alerting/unified/components/receivers/form/ChannelSubForm.tsx @@ -69,7 +69,8 @@ export function ChannelSubForm({ // Prevent forgetting about initial values when switching the integration type and the oncall integration type useEffect(() => { // Restore values when switching back from a changed integration to the default one - const subscription = watch((_, { name, type, value }) => { + const subscription = watch((v, { name, type }) => { + const value = name ? v[name] : ''; if (initialValues && name === fieldName('type') && value === initialValues.type && type === 'change') { setValue(fieldName('settings'), initialValues.settings); } diff --git a/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx b/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx index 48ccc6e5bc2d..50621e5c619b 100644 --- a/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx +++ b/public/app/features/alerting/unified/components/receivers/form/ReceiverForm.tsx @@ -83,7 +83,7 @@ export function ReceiverForm({ const { fields, append, remove } = useControlledFieldArray({ name: 'items', formAPI, softDelete: true }); - const validateNameIsAvailable: Validate = useCallback( + const validateNameIsAvailable: Validate> = useCallback( (name: string) => takenReceiverNames.map((name) => name.trim().toLowerCase()).includes(name.trim().toLowerCase()) ? 'Another receiver with this name already exists.' diff --git a/public/app/features/alerting/unified/components/rule-editor/LabelsField.tsx b/public/app/features/alerting/unified/components/rule-editor/LabelsField.tsx index 49e01b94660a..907e3c4b53e3 100644 --- a/public/app/features/alerting/unified/components/rule-editor/LabelsField.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/LabelsField.tsx @@ -1,9 +1,19 @@ import { css, cx } from '@emotion/css'; import React, { FC, useCallback, useEffect, useMemo, useState } from 'react'; -import { FieldArrayMethodProps, useFieldArray, useFormContext } from 'react-hook-form'; +import { useFieldArray, UseFieldArrayAppend, useFormContext } from 'react-hook-form'; import { GrafanaTheme2, SelectableValue } from '@grafana/data'; -import { Button, Field, InlineLabel, Input, LoadingPlaceholder, Stack, Text, useStyles2 } from '@grafana/ui'; +import { + Button, + Field, + InlineLabel, + Input, + InputControl, + LoadingPlaceholder, + Stack, + Text, + useStyles2, +} from '@grafana/ui'; import { useDispatch } from 'app/types'; import { useUnifiedAlertingSelector } from '../../hooks/useUnifiedAlertingSelector'; @@ -85,10 +95,7 @@ const RemoveButton: FC<{ ); const AddButton: FC<{ - append: ( - value: Partial<{ key: string; value: string }> | Array>, - options?: FieldArrayMethodProps | undefined - ) => void; + append: UseFieldArrayAppend; className: string; }> = ({ append, className }) => (