From e9e438ee2f720231c412e0693342f2f3d6055d5a Mon Sep 17 00:00:00 2001 From: Alex Khomenko Date: Mon, 24 May 2021 15:09:33 +0300 Subject: [PATCH] Form: Expose all return values from useForm (#34380) --- packages/grafana-ui/src/components/Forms/Form.tsx | 4 ++-- packages/grafana-ui/src/types/forms.ts | 5 +---- .../features/alerting/components/NotificationChannelForm.tsx | 5 +++-- .../alerting/components/NotificationChannelOptions.tsx | 5 +++-- .../manage-dashboards/components/ImportDashboardForm.tsx | 2 +- 5 files changed, 10 insertions(+), 11 deletions(-) diff --git a/packages/grafana-ui/src/components/Forms/Form.tsx b/packages/grafana-ui/src/components/Forms/Form.tsx index c3b4eeb0913..b10051597ae 100644 --- a/packages/grafana-ui/src/components/Forms/Form.tsx +++ b/packages/grafana-ui/src/components/Forms/Form.tsx @@ -24,7 +24,7 @@ export function Form({ maxWidth = 600, ...htmlProps }: FormProps) { - const { handleSubmit, register, control, trigger, getValues, formState, watch, setValue } = useForm({ + const { handleSubmit, trigger, formState, ...rest } = useForm({ mode: validateOn, defaultValues, }); @@ -45,7 +45,7 @@ export function Form({ onSubmit={handleSubmit(onSubmit)} {...htmlProps} > - {children({ register, errors: formState.errors, control, getValues, formState, watch, setValue })} + {children({ errors: formState.errors, formState, ...rest })} ); } diff --git a/packages/grafana-ui/src/types/forms.ts b/packages/grafana-ui/src/types/forms.ts index 15684115dab..c757bee9223 100644 --- a/packages/grafana-ui/src/types/forms.ts +++ b/packages/grafana-ui/src/types/forms.ts @@ -1,10 +1,7 @@ import { UseFormReturn, FieldValues, FieldErrors } from 'react-hook-form'; export { SubmitHandler as FormsOnSubmit, FieldErrors as FormFieldErrors } from 'react-hook-form'; -export type FormAPI = Pick< - UseFormReturn, - 'register' | 'control' | 'formState' | 'getValues' | 'watch' | 'setValue' -> & { +export type FormAPI = Omit, 'trigger' | 'handleSubmit'> & { errors: FieldErrors; }; diff --git a/public/app/features/alerting/components/NotificationChannelForm.tsx b/public/app/features/alerting/components/NotificationChannelForm.tsx index 14b9fe9979f..a9abc49a523 100644 --- a/public/app/features/alerting/components/NotificationChannelForm.tsx +++ b/public/app/features/alerting/components/NotificationChannelForm.tsx @@ -9,7 +9,8 @@ import { ChannelSettings } from './ChannelSettings'; import config from 'app/core/config'; -interface Props extends Omit, 'formState' | 'setValue'> { +interface Props + extends Pick, 'control' | 'errors' | 'register' | 'watch' | 'getValues'> { selectableChannels: Array>; selectedChannel?: NotificationChannelType; imageRendererAvailable: boolean; @@ -19,7 +20,7 @@ interface Props extends Omit, 'formState' | 'set } export interface NotificationSettingsProps - extends Omit, 'formState' | 'watch' | 'getValues' | 'setValue'> { + extends Pick, 'control' | 'errors' | 'register'> { currentFormValues: NotificationChannelDTO; } diff --git a/public/app/features/alerting/components/NotificationChannelOptions.tsx b/public/app/features/alerting/components/NotificationChannelOptions.tsx index f268a4d5886..9cdb59a61d5 100644 --- a/public/app/features/alerting/components/NotificationChannelOptions.tsx +++ b/public/app/features/alerting/components/NotificationChannelOptions.tsx @@ -1,10 +1,11 @@ import React, { FC } from 'react'; import { SelectableValue } from '@grafana/data'; -import { Button, Checkbox, Field, FormAPI, Input } from '@grafana/ui'; +import { Button, Checkbox, Field, Input } from '@grafana/ui'; import { OptionElement } from './OptionElement'; import { NotificationChannelDTO, NotificationChannelOption, NotificationChannelSecureFields } from '../../../types'; +import { NotificationSettingsProps } from './NotificationChannelForm'; -interface Props extends Omit, 'formState' | 'getValues' | 'watch' | 'setValue'> { +interface Props extends NotificationSettingsProps { selectedChannelOptions: NotificationChannelOption[]; currentFormValues: NotificationChannelDTO; secureFields: NotificationChannelSecureFields; diff --git a/public/app/features/manage-dashboards/components/ImportDashboardForm.tsx b/public/app/features/manage-dashboards/components/ImportDashboardForm.tsx index 43ba364b8ba..865a75a07f5 100644 --- a/public/app/features/manage-dashboards/components/ImportDashboardForm.tsx +++ b/public/app/features/manage-dashboards/components/ImportDashboardForm.tsx @@ -15,7 +15,7 @@ import { FolderPicker } from 'app/core/components/Select/FolderPicker'; import { DashboardInput, DashboardInputs, DataSourceInput, ImportDashboardDTO } from '../state/reducers'; import { validateTitle, validateUid } from '../utils/validation'; -interface Props extends Omit, 'formState' | 'setValue'> { +interface Props extends Pick, 'register' | 'errors' | 'control' | 'getValues' | 'watch'> { uidReset: boolean; inputs: DashboardInputs; initialFolderId: number;