Skip to content

Commit

Permalink
refactor(core/presentation): Split forms interfaces into three files:
Browse files Browse the repository at this point in the history
- /forms/fields/interface.ts
- /forms/inputs/interface.ts
- /forms/layouts/interface.ts
  • Loading branch information
christopherthielen committed Oct 10, 2019
1 parent 2d6c9e7 commit eafb077
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 71 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import * as React from 'react';

import { IFormInputProps, IFormInputValidation } from '../inputs';
import { ILayoutProps } from '../layouts';
import { IValidator } from '../validation';

/** These props are used by FormFields such as FormikFormField and FormField */
export interface ICommonFormFieldProps {
/** The Form Input component (or render function) */
input: React.ComponentType<IFormInputProps>;

/**
* An optional layout component (or render function).
* If none is provided, the default layout (from context) will be used
*/
layout?: React.ComponentType<ILayoutProps>;

/** An inline validator function or functions */
validate?: IValidator | IValidator[];

/**
* A string (or React Node) containing an explicit validation message for the Form Field.
*
* This string can be constructed using the helper methods from core/presentation i.e.:
* errorMessage('Something went wrong!')
*
* Alternatively, a react node can be provided.
*/
validationMessage?: IFormInputValidation['messageNode'];

/** An explicit 'touched' status for the Form Field */
touched?: IFormInputValidation['touched'];

// The following props are a subset of layout props that are accepted by a FormField or FormikFormField

/** A boolean marking this field as required */
required?: ILayoutProps['required'];

/** A string (or ReactNode) containing the field's label */
label?: ILayoutProps['label'];

/** A ReactNode containing the field's help component */
help?: ILayoutProps['help'];

/** A ReactNode containing the field's actions, such as 'delete item' */
actions?: ILayoutProps['actions'];
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';

import { orEmptyString, validationClassName } from './utils';
import { IFormInputProps, OmitControlledInputPropsFrom } from '../interface';
import { IFormInputProps, OmitControlledInputPropsFrom } from './interface';

interface ICheckBoxInputProps extends IFormInputProps, OmitControlledInputPropsFrom<React.InputHTMLAttributes<any>> {
inputClassName?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { IFormInputProps, OmitControlledInputPropsFrom } from '../interface';
import { IFormInputProps, OmitControlledInputPropsFrom } from './interface';

import { createFakeReactSyntheticEvent, isStringArray, orEmptyString, validationClassName } from './utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { defaults } from 'lodash';
import { DayPickerInputProps } from 'react-day-picker';
import DayPicker from 'react-day-picker/DayPickerInput';

import { IFormInputProps } from '../interface';
import { IFormInputProps } from './interface';
import { createFakeReactSyntheticEvent, orEmptyString, validationClassName } from './utils';

import 'react-day-picker/lib/style.css';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import memoizeOne from 'memoize-one';

import { orEmptyString, validationClassName } from './utils';
import { composeValidators, IValidator, Validators } from '../validation';
import { IFormInputProps, OmitControlledInputPropsFrom } from '../interface';
import { IFormInputProps, OmitControlledInputPropsFrom } from './interface';

import './NumberInput.css';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Option } from 'react-select';
import { Markdown, OmitControlledInputPropsFrom } from 'core/presentation';

import { isStringArray, orEmptyString, validationClassName } from './utils';
import { IFormInputProps } from '../interface';
import { IFormInputProps } from './interface';

interface IRadioButtonInputProps
extends IFormInputProps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Option } from 'react-select';
import { OmitControlledInputPropsFrom, StringsAsOptions } from 'core/presentation';

import { isStringArray, orEmptyString, validationClassName } from './utils';
import { IFormInputProps } from '../interface';
import { IFormInputProps } from './interface';

export interface ISelectInputProps
extends IFormInputProps,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';

import { orEmptyString, validationClassName } from './utils';
import { IFormInputProps, OmitControlledInputPropsFrom } from '../interface';
import { IFormInputProps, OmitControlledInputPropsFrom } from './interface';

interface ITextAreaInputProps extends IFormInputProps, OmitControlledInputPropsFrom<React.TextareaHTMLAttributes<any>> {
inputClassName?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';

import { orEmptyString, validationClassName } from './utils';
import { IFormInputProps, OmitControlledInputPropsFrom } from '../interface';
import { IFormInputProps, OmitControlledInputPropsFrom } from './interface';

interface ITextInputProps extends IFormInputProps, OmitControlledInputPropsFrom<React.InputHTMLAttributes<any>> {
inputClassName?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export * from './StringsAsOptions';
export * from './TextAreaInput';
export * from './TextInput';
export * from './expression';
export * from './interface';
export * from './utils';
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as React from 'react';

import { FieldProps } from 'formik';
import { IValidationCategory, IValidator } from '../validation';

/**
* These props are used by controlled components, such as <input> or Input components like TextInput
* Some of the typings reference the typings supplied by formik FieldProps
*/
export interface IControlledInputProps {
value: FieldProps['field']['value'];

onChange(e: React.ChangeEvent<any>): void;

onBlur: FieldProps['field']['onBlur'];
name: FieldProps['field']['name'];
}

export type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
export type OmitControlledInputPropsFrom<T> = Omit<T, keyof IControlledInputProps>;

/** These props are used by Input components, such as TextInput */
export interface IFormInputValidation {
touched?: boolean;
category?: IValidationCategory;
messageNode?: React.ReactNode;
hidden?: boolean;
addValidator?: (validator: IValidator) => void;
removeValidator?: (validator: IValidator) => void;
}

/** These props are used by Form Input components, such as TextInput */
export interface IFormInputProps extends Partial<IControlledInputProps> {
validation?: IFormInputValidation;
inputClassName?: string;
}
61 changes: 0 additions & 61 deletions app/scripts/modules/core/src/presentation/forms/interface.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { IFieldLayoutProps } from '../interface';
import { ILayoutProps } from './interface';
import { StandardFieldLayout } from './StandardFieldLayout';

export const LayoutContext = React.createContext<React.ComponentType<IFieldLayoutProps>>(StandardFieldLayout);
export const LayoutContext = React.createContext<React.ComponentType<ILayoutProps>>(StandardFieldLayout);
export const { Provider: LayoutProvider, Consumer: LayoutConsumer } = LayoutContext;
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './StandardFieldLayout';
export * from './LayoutContext';
export * from './ResponsiveFieldLayout';
export * from './interface';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as React from 'react';

import { IFormInputValidation } from '../inputs';

/** These props are used by FieldLayout components, such as StandardFieldLayout */
export interface ILayoutProps {
required?: boolean;
label?: React.ReactNode;
help?: React.ReactNode;
actions?: React.ReactNode;
input: React.ReactNode;
validation?: IFormInputValidation;
}

0 comments on commit eafb077

Please sign in to comment.