Skip to content

Commit

Permalink
rename FormWithWarm to FormWithWarnings
Browse files Browse the repository at this point in the history
  • Loading branch information
maximpn committed Dec 6, 2024
1 parent cefdd61 commit b5e7635
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import type { FormHook, FormData, ValidationError } from '../../../shared_imports';

export interface FormHookWithWarn<T extends FormData = FormData, I extends FormData = T>
export interface FormHookWithWarnings<T extends FormData = FormData, I extends FormData = T>
extends FormHook<T, I> {
getValidationWarnings(): ValidationError[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
* 2.0.
*/

export type * from './form_hook_with_warn';
export * from './use_form_with_warn';
export type * from './form_hook_with_warnings';
export * from './use_form_with_warnings';
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { TextField } from '@kbn/es-ui-shared-plugin/static/forms/components';
import type { FieldConfig } from '../../../shared_imports';
import { Form, UseField } from '../../../shared_imports';
import type { FormWithWarnSubmitHandler } from './use_form_with_warn';
import { useFormWithWarn } from './use_form_with_warn';
import type { FormWithWarningsSubmitHandler } from './use_form_with_warnings';
import { useFormWithWarnings } from './use_form_with_warnings';

describe('useFormWithWarn', () => {
describe('isValid', () => {
Expand Down Expand Up @@ -174,12 +174,12 @@ describe('useFormWithWarn', () => {
});

interface TestFormProps {
onSubmit?: FormWithWarnSubmitHandler;
onSubmit?: FormWithWarningsSubmitHandler;
warningValidationCodes: string[];
}

function TestForm({ onSubmit, warningValidationCodes }: TestFormProps): JSX.Element {
const { form } = useFormWithWarn({
const { form } = useFormWithWarnings({
onSubmit,
options: {
warningValidationCodes,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { isEmpty } from 'lodash';
import type { FormHook } from '../../../shared_imports';
import { useForm, type FormConfig, type FormData } from '../../../shared_imports';
import type { FormHookWithWarn } from './form_hook_with_warn';
import type { FormHookWithWarnings } from './form_hook_with_warnings';
import { extractValidationResults } from './extract_validation_results';
import type { ValidationResults } from './validation_results';

export type FormWithWarnSubmitHandler<T extends FormData = FormData> = (
export type FormWithWarningsSubmitHandler<T extends FormData = FormData> = (
formData: T,
isValid: boolean,
validationResults: ValidationResults
) => Promise<void>;

interface FormWithWarnConfig<T extends FormData = FormData, I extends FormData = T>
interface FormWithWarningsConfig<T extends FormData = FormData, I extends FormData = T>
extends Omit<FormConfig<T, I>, 'onSubmit'> {
onSubmit?: FormWithWarnSubmitHandler<T>;
onSubmit?: FormWithWarningsSubmitHandler<T>;
options: FormConfig['options'] & {
warningValidationCodes: Readonly<string[]>;
};
}

interface UseFormWithWarnReturn<T extends FormData = FormData, I extends FormData = T> {
form: FormHookWithWarn<T, I>;
interface UseFormWithWarningsReturn<T extends FormData = FormData, I extends FormData = T> {
form: FormHookWithWarnings<T, I>;
}

/**
Expand All @@ -49,15 +49,15 @@ interface UseFormWithWarnReturn<T extends FormData = FormData, I extends FormDat
* `message` and `code` fields from a validator function. Attempts to reuse `__isBlocking__` internal
* field lead to inconsistent behavior.
*
* `useFormWithWarn` implements warnings (non blocking errors) on top of `FormHook` using validation
* `useFormWithWarnings` implements warnings (non blocking errors) on top of `FormHook` using validation
* error codes as a flexible way to determine whether an error is a blocking error or it's a warning.
* It provides little interface extension to simplify errors and warnings consumption
*
* In some cases business logic requires implementing functionality to allow users perform an action
* despite non-critical validation errors a.k.a. warnings. Usually it's also required to inform users
* about warnings they got before proceeding for example via a modal.
*
* Since `FormHook` returned by `useForm` lacks of such functionality `useFormWithWarn` is here to
* Since `FormHook` returned by `useForm` lacks of such functionality `useFormWithWarnings` is here to
* provide warnings functionality. It could be used and passed as `FormHook` when warnings functionality
* isn't required making absolutely transparent.
*
Expand All @@ -68,9 +68,9 @@ interface UseFormWithWarnReturn<T extends FormData = FormData, I extends FormDat
*
* There is a ticket to move this functionality to Form lib https://github.com/elastic/kibana/issues/203097.
*/
export function useFormWithWarn<T extends FormData = FormData, I extends FormData = T>(
formConfig: FormWithWarnConfig<T, I>
): UseFormWithWarnReturn<T, I> {
export function useFormWithWarnings<T extends FormData = FormData, I extends FormData = T>(
formConfig: FormWithWarningsConfig<T, I>
): UseFormWithWarningsReturn<T, I> {
const {
onSubmit,
options: { warningValidationCodes },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { useState, useMemo, useEffect } from 'react';
import type { DataViewBase } from '@kbn/es-query';
import { useFormWithWarn } from '../../../common/hooks/use_form_with_warn';
import { useFormWithWarnings } from '../../../common/hooks/use_form_with_warnings';
import { isThreatMatchRule } from '../../../../common/detection_engine/utils';
import type {
AboutStepRule,
Expand Down Expand Up @@ -46,7 +46,7 @@ export const useRuleForms = ({
triggersActionsUi: { actionTypeRegistry },
} = useKibana().services;
// DEFINE STEP FORM
const { form: defineStepForm } = useFormWithWarn<DefineStepRule>({
const { form: defineStepForm } = useFormWithWarnings<DefineStepRule>({
defaultValue: defineStepDefault,
options: { stripEmptyFields: false, warningValidationCodes: NON_BLOCKING_ERROR_CODES },
schema: defineRuleSchema,
Expand All @@ -66,7 +66,7 @@ export const useRuleForms = ({
() => (isThreatMatchRule(defineStepData.ruleType) ? threatMatchAboutSchema : aboutRuleSchema),
[defineStepData.ruleType]
);
const { form: aboutStepForm } = useFormWithWarn<AboutStepRule>({
const { form: aboutStepForm } = useFormWithWarnings<AboutStepRule>({
defaultValue: aboutStepDefault,
options: { stripEmptyFields: false, warningValidationCodes: NON_BLOCKING_ERROR_CODES },
schema: typeDependentAboutRuleSchema,
Expand All @@ -77,7 +77,7 @@ export const useRuleForms = ({
const aboutStepData = 'name' in aboutStepFormData ? aboutStepFormData : aboutStepDefault;

// SCHEDULE STEP FORM
const { form: scheduleStepForm } = useFormWithWarn<ScheduleStepRule>({
const { form: scheduleStepForm } = useFormWithWarnings<ScheduleStepRule>({
defaultValue: scheduleStepDefault,
options: { stripEmptyFields: false, warningValidationCodes: NON_BLOCKING_ERROR_CODES },
schema: scheduleRuleSchema,
Expand All @@ -90,7 +90,7 @@ export const useRuleForms = ({

// ACTIONS STEP FORM
const schema = useMemo(() => getActionsRuleSchema({ actionTypeRegistry }), [actionTypeRegistry]);
const { form: actionsStepForm } = useFormWithWarn<ActionsStepRule>({
const { form: actionsStepForm } = useFormWithWarnings<ActionsStepRule>({
defaultValue: actionsStepDefault,
options: { stripEmptyFields: false, warningValidationCodes: NON_BLOCKING_ERROR_CODES },
schema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import React, { useCallback, useEffect } from 'react';
import { EuiButtonEmpty, EuiFlexGroup } from '@elastic/eui';
import { extractValidationMessages } from '../../../../../../rule_creation/logic/extract_validation_messages';
import type { FormWithWarnSubmitHandler } from '../../../../../../../common/hooks/use_form_with_warn';
import { useFormWithWarn } from '../../../../../../../common/hooks/use_form_with_warn';
import type { FormWithWarningsSubmitHandler } from '../../../../../../../common/hooks/use_form_with_warnings';
import { useFormWithWarnings } from '../../../../../../../common/hooks/use_form_with_warnings';
import { Form } from '../../../../../../../shared_imports';
import type { FormSchema, FormData } from '../../../../../../../shared_imports';
import type {
Expand Down Expand Up @@ -66,7 +66,7 @@ export function RuleFieldEditFormWrapper({

const { modal, confirmValidationErrors } = useConfirmValidationErrorsModal();

const handleSubmit = useCallback<FormWithWarnSubmitHandler>(
const handleSubmit = useCallback<FormWithWarningsSubmitHandler>(
async (formData: FormData, isValid: boolean, { warnings }) => {
const warningMessages = extractValidationMessages(warnings, ERROR_CODE_FIELD_NAME_MAP);

Expand All @@ -90,7 +90,7 @@ export function RuleFieldEditFormWrapper({
]
);

const { form } = useFormWithWarn({
const { form } = useFormWithWarnings({
schema: ruleFieldFormSchema,
defaultValue: getDefaultValue(fieldName, finalDiffableRule),
deserializer: deserialize,
Expand Down

0 comments on commit b5e7635

Please sign in to comment.