From bfeb2866374d31267a64295cdba71dd85879658d Mon Sep 17 00:00:00 2001 From: Maxim Mordasov Date: Thu, 18 Apr 2024 14:58:32 +0100 Subject: [PATCH] Replace GForm with react-hook-form (#4171) # What this PR does Replace GForm with react-hook-form ## Which issue(s) this PR closes Closes https://github.com/grafana/oncall/issues/4142 ## Checklist - [ ] Unit, integration, and e2e (if applicable) tests updated - [x] Documentation added (or `pr:no public docs` PR label added if not required) - [x] Added the relevant release notes label (see labels prefixed w/ `release:`). These labels dictate how your PR will show up in the autogenerated release notes. --- .../src/components/GForm/GForm.module.scss | 7 - grafana-plugin/src/components/GForm/GForm.tsx | 306 -------- .../src/components/GForm/GForm.types.ts | 34 - .../ManualAlertGroup.config.ts | 17 - .../ManualAlertGroup/ManualAlertGroup.tsx | 97 ++- .../AddResponders/AddResponders.helpers.ts | 2 +- .../src/containers/GSelect/GSelect.tsx | 2 +- .../MaintenanceForm.config.tsx | 86 --- .../MaintenanceForm/MaintenanceForm.tsx | 172 ++++- .../OutgoingWebhookForm.config.tsx | 193 ----- .../OutgoingWebhookForm.styles.ts | 13 + .../OutgoingWebhookForm.tsx | 689 ++++++++---------- .../OutgoingWebhookForm.types.ts | 7 + .../OutgoingWebhookFormFields.tsx | 363 +++++++++ .../WebhookPresetBlocks.tsx | 60 ++ .../containers/RemoteSelect/RemoteSelect.tsx | 2 +- .../ScheduleForm/ScheduleForm.config.ts | 204 ------ .../containers/ScheduleForm/ScheduleForm.tsx | 380 +++++++++- .../alert_receive_channel.ts | 4 +- .../outgoing_webhook/outgoing_webhook.ts | 9 - .../src/pages/integration/Integration.tsx | 3 +- 21 files changed, 1305 insertions(+), 1345 deletions(-) delete mode 100644 grafana-plugin/src/components/GForm/GForm.module.scss delete mode 100644 grafana-plugin/src/components/GForm/GForm.tsx delete mode 100644 grafana-plugin/src/components/GForm/GForm.types.ts delete mode 100644 grafana-plugin/src/components/ManualAlertGroup/ManualAlertGroup.config.ts delete mode 100644 grafana-plugin/src/containers/MaintenanceForm/MaintenanceForm.config.tsx delete mode 100644 grafana-plugin/src/containers/OutgoingWebhookForm/OutgoingWebhookForm.config.tsx create mode 100644 grafana-plugin/src/containers/OutgoingWebhookForm/OutgoingWebhookForm.styles.ts create mode 100644 grafana-plugin/src/containers/OutgoingWebhookForm/OutgoingWebhookFormFields.tsx create mode 100644 grafana-plugin/src/containers/OutgoingWebhookForm/WebhookPresetBlocks.tsx delete mode 100644 grafana-plugin/src/containers/ScheduleForm/ScheduleForm.config.ts diff --git a/grafana-plugin/src/components/GForm/GForm.module.scss b/grafana-plugin/src/components/GForm/GForm.module.scss deleted file mode 100644 index 66629f6a..00000000 --- a/grafana-plugin/src/components/GForm/GForm.module.scss +++ /dev/null @@ -1,7 +0,0 @@ -.collapse { - margin-bottom: 16px; -} - -.label { - margin-bottom: 16px; -} diff --git a/grafana-plugin/src/components/GForm/GForm.tsx b/grafana-plugin/src/components/GForm/GForm.tsx deleted file mode 100644 index 317ca06e..00000000 --- a/grafana-plugin/src/components/GForm/GForm.tsx +++ /dev/null @@ -1,306 +0,0 @@ -import React from 'react'; - -import { Field, Form, FormFieldErrors, Input, InputControl, Label, Select, Switch, TextArea } from '@grafana/ui'; -import { capitalCase } from 'change-case'; -import cn from 'classnames/bind'; -import { isEmpty } from 'lodash-es'; - -import { Collapse } from 'components/Collapse/Collapse'; -import { FormItem, FormItemType } from 'components/GForm/GForm.types'; -import { MonacoEditor } from 'components/MonacoEditor/MonacoEditor'; -import { MONACO_READONLY_CONFIG } from 'components/MonacoEditor/MonacoEditor.config'; -import { Text } from 'components/Text/Text'; -import { GSelect } from 'containers/GSelect/GSelect'; -import { RemoteSelect } from 'containers/RemoteSelect/RemoteSelect'; - -import styles from './GForm.module.scss'; - -const cx = cn.bind(styles); - -export interface CustomFieldSectionRendererProps { - control: any; - formItem: FormItem; - errors: any; - register: any; - setValue: (fieldName: string, fieldValue: any) => void; - getValues: (fieldName: string | string[]) => T; -} - -interface GFormProps { - form: { name: string; fields: FormItem[] }; - data: any; - onSubmit: (data: any) => void; - onChange?: (formIsValid: boolean) => void; - - customFieldSectionRenderer?: React.FC; - onFieldRender?: ( - formItem: FormItem, - disabled: boolean, - renderedControl: React.ReactElement, - values: any, - setValue: (value: string) => void - ) => React.ReactElement; -} - -const nullNormalizer = (value: string) => { - return value || null; -}; - -function renderFormControl( - formItem: FormItem, - register: any, - control: any, - disabled: boolean, - onChangeFn: (field, value) => void -) { - switch (formItem.type) { - case FormItemType.Input: - return ( - onChangeFn(undefined, value)} - /> - ); - - case FormItemType.Password: - return ( - onChangeFn(undefined, value)} - /> - ); - - case FormItemType.TextArea: - return ( -