Skip to content

Commit

Permalink
Update FieldConfig interface to accept first the field value type bef…
Browse files Browse the repository at this point in the history
…ore the Form
  • Loading branch information
sebelga committed Oct 2, 2020
1 parent 7def426 commit 24e358d
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { useFormContext } from '../form_context';

export interface Props<T> {
path: string;
config?: FieldConfig<any, T>;
config?: FieldConfig<T>;
defaultValue?: T;
component?: FunctionComponent<any>;
componentProps?: Record<string, any>;
Expand All @@ -52,12 +52,12 @@ function UseFieldComp<T = unknown>(props: Props<T>) {
const ComponentToRender = component ?? 'input';
const propsToForward = { ...componentProps, ...rest };

const fieldConfig: FieldConfig<any, T> & { initialValue?: T } =
const fieldConfig: FieldConfig<T> & { initialValue?: T } =
config !== undefined
? { ...config }
: ({
...form.__readFieldConfigFromSchema(path),
} as Partial<FieldConfig<any, T>>);
} as Partial<FieldConfig<T>>);

if (defaultValue !== undefined) {
// update the form "defaultValue" ref object so when/if we reset the form we can go back to this value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface InternalFieldConfig<T> {
export const useField = <T>(
form: FormHook,
path: string,
config: FieldConfig<any, T> & InternalFieldConfig<T> = {},
config: FieldConfig<T> & InternalFieldConfig<T> = {},
valueChangeListener?: (value: T) => void
) => {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ export const useFormData = <T extends object = FormData>(options: Options = {}):
? (watch as string[])
: ([watch] as string[]);

if (valuesToWatchArray.some((value) => previousRawData.current[value] !== raw[value])) {
if (
valuesToWatchArray.some(
(value) => previousRawData.current[value] !== raw[value as keyof T]
)
) {
previousRawData.current = raw;
// Only update the state if one of the field we watch has changed.
setFormData(raw);
Expand Down
12 changes: 8 additions & 4 deletions src/plugins/es_ui_shared/static/forms/hook_form_lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export interface FormHook<T extends FormData = FormData, I extends FormData = T>
}

export type FormSchema<T extends FormData = FormData> = {
[K in keyof T]?: FieldConfig<T, T[K]> | FormSchema<T[K]>;
[K in keyof T]?: FieldConfig<T[K], T> | FormSchema<T[K]>;
};

export interface FormConfig<T extends FormData = FormData, I extends FormData = T> {
Expand Down Expand Up @@ -135,7 +135,7 @@ export interface FieldHook<T = unknown> {
__serializeValue: (rawValue?: T) => unknown;
}

export interface FieldConfig<T extends FormData = any, ValueType = unknown> {
export interface FieldConfig<ValueType = unknown, T extends FormData = FormData> {
readonly label?: string;
readonly labelAppend?: string | ReactNode;
readonly helpText?: string | ReactNode;
Expand Down Expand Up @@ -177,7 +177,7 @@ export interface ValidationFuncArg<T extends FormData, V = unknown> {
errors: readonly ValidationError[];
}

export type ValidationFunc<T extends FormData = any, E = string, V = unknown> = (
export type ValidationFunc<T extends FormData = any, E extends string = string, V = unknown> = (
data: ValidationFuncArg<T, V>
) => ValidationError<E> | void | undefined | Promise<ValidationError<E> | void | undefined>;

Expand All @@ -198,7 +198,11 @@ type FormatterFunc = (value: any, formData: FormData) => unknown;
// string | number | boolean | string[] ...
type FieldValue = unknown;

export interface ValidationConfig<T extends FormData = any, E = string, V = unknown> {
export interface ValidationConfig<
T extends FormData = any,
E extends string = string,
V = unknown
> {
validator: ValidationFunc<T, E, V>;
type?: string;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const { isJsonField } = fieldValidators;
* We use it to store custom defined parameters in a field called "otherTypeJson".
*/

const fieldConfig: FieldConfig<any, any> = {
const fieldConfig: FieldConfig<any> = {
label: i18n.translate('xpack.idxMgmt.mappingsEditor.otherTypeJsonFieldLabel', {
defaultMessage: 'Type Parameters JSON',
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const getTypeLabelFromField = (field: Field) => {
export const getFieldConfig = <T = unknown>(
param: ParameterName,
prop?: string
): FieldConfig<any, T> => {
): FieldConfig<T> => {
if (prop !== undefined) {
if (
!(PARAMETERS_DEFINITION[param] as any).props ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export interface DataTypeDefinition {
export interface ParameterDefinition {
title?: string;
description?: JSX.Element | string;
fieldConfig: FieldConfig<any, any>;
fieldConfig: FieldConfig<any>;
schema?: any;
props?: { [key: string]: ParameterDefinition };
documentation?: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { TextEditor } from '../../field_components';
import { to, from, EDITOR_PX_HEIGHT } from '../shared';

const ignoreFailureConfig: FieldConfig<any, any> = {
const ignoreFailureConfig: FieldConfig<any> = {
defaultValue: false,
deserializer: to.booleanOrUndef,
serializer: from.undefinedIfValue(false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { XJsonEditor } from '../field_components';
import { Fields } from '../processor_form.container';
import { EDITOR_PX_HEIGHT } from './shared';

const customConfig: FieldConfig<any, any> = {
const customConfig: FieldConfig<any> = {
type: FIELD_TYPES.TEXT,
label: i18n.translate('xpack.ingestPipelines.pipelineEditor.customForm.optionsFieldLabel', {
defaultMessage: 'Configuration',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ export const EDITOR_PX_HEIGHT = {
large: 300,
};

export type FieldsConfig = Record<string, FieldConfig<any, any>>;
export type FieldsConfig = Record<string, FieldConfig<any>>;

export type FormFieldsComponent = FunctionComponent<{ initialFieldValues?: Record<string, any> }>;
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ const i18nTexts = {
),
};

const documentFieldConfig: FieldConfig<any, string> = {
const documentFieldConfig: FieldConfig<string> = {
label: i18n.translate(
'xpack.ingestPipelines.testPipelineFlyout.documentsForm.documentsFieldLabel',
{
Expand Down

0 comments on commit 24e358d

Please sign in to comment.