From 140f74542f12bb721e00da2a7ac51f1791f8951e Mon Sep 17 00:00:00 2001 From: Clarity-89 Date: Thu, 30 Nov 2023 08:32:08 +0200 Subject: [PATCH 01/18] Update RHF to latest --- package.json | 2 +- packages/grafana-ui/package.json | 2 +- yarn.lock | 14 +++++++------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index ad528dbd3c72..1d388f2c775f 100644 --- a/package.json +++ b/package.json @@ -374,7 +374,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.48.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 acfb1f294e66..cd58d1c2e53b 100644 --- a/packages/grafana-ui/package.json +++ b/packages/grafana-ui/package.json @@ -91,7 +91,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.48.2", "react-i18next": "^12.0.0", "react-inlinesvg": "3.0.2", "react-loading-skeleton": "3.3.1", diff --git a/yarn.lock b/yarn.lock index 59d7a6b1e1e2..bb7a8da32bdf 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3461,7 +3461,7 @@ __metadata: react-dom: "npm:18.2.0" react-dropzone: "npm:14.2.3" react-highlight-words: "npm:0.20.0" - react-hook-form: "npm:7.5.3" + react-hook-form: "npm:^7.48.2" react-i18next: "npm:^12.0.0" react-inlinesvg: "npm:3.0.2" react-loading-skeleton: "npm:3.3.1" @@ -17555,7 +17555,7 @@ __metadata: react-dropzone: "npm:^14.2.3" react-grid-layout: "npm:1.4.2" react-highlight-words: "npm:0.20.0" - react-hook-form: "npm:7.5.3" + react-hook-form: "npm:^7.48.2" react-i18next: "npm:^12.0.0" react-inlinesvg: "npm:3.0.2" react-loading-skeleton: "npm:3.3.1" @@ -25543,12 +25543,12 @@ __metadata: languageName: node linkType: hard -"react-hook-form@npm:7.5.3": - version: 7.5.3 - resolution: "react-hook-form@npm:7.5.3" +"react-hook-form@npm:^7.48.2": + version: 7.48.2 + resolution: "react-hook-form@npm:7.48.2" peerDependencies: - react: ^16.8.0 || ^17 - checksum: ee359714c538ee8f328e147732aba629269849e159a88f96662c2e2d8ca175a6015e5364304bb8bada0249de0c335bafa64835abe09b5db895b702bce9159912 + react: ^16.8.0 || ^17 || ^18 + checksum: 6cb494f359675e78c14c49a33d1c568df1a03db4c750eab18d6554e622fd64e04195c57efa138db03f8988587d132c57d2a7f5f3dd9157be996cc9acd4362170 languageName: node linkType: hard From efe0276636e008475412ea50b1f7d495d4b060af Mon Sep 17 00:00:00 2001 From: Clarity-89 Date: Thu, 30 Nov 2023 08:39:02 +0200 Subject: [PATCH 02/18] Update Form types --- packages/grafana-ui/src/components/Forms/Form.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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.*/ From 6cf9191ba9c8d3e53963820c91c7f57a1c7d5e5e Mon Sep 17 00:00:00 2001 From: Clarity-89 Date: Thu, 30 Nov 2023 09:42:11 +0200 Subject: [PATCH 03/18] Fix alerting types --- .../alerting/components/NotificationChannelOptions.tsx | 2 +- .../components/mute-timings/MuteTimingTimeInterval.tsx | 10 +++++++--- .../EditNotificationPolicyForm.tsx | 2 +- .../unified/components/receivers/TemplateForm.tsx | 2 +- .../components/receivers/form/ChannelSubForm.tsx | 3 ++- .../unified/components/receivers/form/ReceiverForm.tsx | 2 +- .../unified/components/rule-editor/LabelsField.tsx | 9 +++------ .../rule-editor/alert-rule-form/AlertRuleForm.tsx | 4 ++-- .../features/alerting/unified/types/receiver-form.ts | 2 +- public/app/features/alerting/unified/utils/amroutes.ts | 8 ++++---- 10 files changed, 23 insertions(+), 21 deletions(-) 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/notification-policies/EditNotificationPolicyForm.tsx b/public/app/features/alerting/unified/components/notification-policies/EditNotificationPolicyForm.tsx index a18c85dcd917..10754b55457d 100644 --- a/public/app/features/alerting/unified/components/notification-policies/EditNotificationPolicyForm.tsx +++ b/public/app/features/alerting/unified/components/notification-policies/EditNotificationPolicyForm.tsx @@ -257,7 +257,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..a6c7c92f1f45 100644 --- a/public/app/features/alerting/unified/components/rule-editor/LabelsField.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/LabelsField.tsx @@ -1,6 +1,6 @@ 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'; @@ -85,10 +85,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 }) => (