// Reference Types /// /** Libraries */ import { Component, FormEvent } from 'react'; import { Moment } from 'moment'; import { List, Map, Set } from 'immutable'; import { Dispatch, Action } from 'redux'; /** Types */ export type InputType = | 'text' | 'radio' | 'checkbox' | 'number' | 'email' | 'password' | 'hidden' | 'file'; export type InputTest = 'required' | 'pattern' | 'type' | 'minLength' | 'maxLength' | 'min' | 'max'; export type TestType = string | number | boolean | Function | undefined; export type DateRangeMoment = { startDate: Moment; endDate: Moment; }; export type PossibleInputValue = | number | string | boolean | Moment | DateRangeMap | undefined | List | Set; export type getInputPath = () => Array; export type inputChanged = (value: PossibleInputValue, changed?: boolean) => void; export type inputBlurred = () => void; export type setValidation = (type: string, test?: TestType) => void; export type compareAdditionalProps = (props: TProps, nextProps: TProps) => boolean; export type SetInputAction = FSA; export type SetValidationAction = FSA; export type SetInputInteractionAction = FSA; export type SetAllInputInteractionAction = FSA< SetAllInputInteractionPayload, 'SET_ALL_INPUT_INTERACTIONS' >; export type ClearAllInputsAction = FSA; export type formState = Map; export type OnSubmit = ( e: FormEvent, formData: formState | FormData, submitGeneratedForm?: T ) => void; export type InputUnionProps = | TextInputProps | TextAreaProps | SelectInputProps | SwitchProps | RadioTabsProps | DatePickerProps | DateRangeProps; export type ReducerFunc< TState = Map, TAction = | ClearAllInputsAction | SetInputAction | SetAllInputInteractionAction | SetInputInteractionAction | SetValidationAction > = (state: TState, action: TAction) => TState; export type BasicReducer = { SET_INPUT: ReducerFunc, SetInputAction>; SET_VALIDATION: ReducerFunc, SetValidationAction>; SET_INPUT_INTERACTION: ReducerFunc, SetInputInteractionAction>; SET_ALL_INPUT_INTERACTIONS: ReducerFunc, SetAllInputInteractionAction>; CLEAR_ALL_INPUTS: ReducerFunc, ClearAllInputsAction>; }; /** export interfaces */ export interface DateRangeMap extends Map {} export interface BaseReactProps { children?: React.ReactNode; key?: React.Key; className?: string; style?: Object; } export interface ValidationProps { required?: boolean; } export interface ValidationTypes { required: (value: PossibleInputValue, test: boolean, type: InputType) => boolean; pattern: (value: PossibleInputValue, test: string) => boolean; type: (value: PossibleInputValue, test: string) => boolean; minLength: (value: PossibleInputValue, test: string) => boolean; maxLength: (value: PossibleInputValue, test: string) => boolean; min: (value: PossibleInputValue, test: string) => boolean; max: (value: PossibleInputValue, test: string) => boolean; default: () => false; } export interface InputValidationProps extends ValidationProps { /** * Lowest number(including itself) possible in * the input field */ min?: string | number; /** * Highest number(including itself) possible in * the input field */ max?: string | number; /** * The minimum length of the input text */ minLength?: string | number; /** * The maximum length of the input text */ maxLength?: string | number; /** * A regular expression(regex) for the input text * so as to allow only inputs that match the pattern */ pattern?: string; } export interface CustomValidationProps extends InputValidationProps { /** * A validator function which takes in value * and returns a true or false */ customValidation?: (input: string | number) => boolean; } export interface ValidateProps extends CustomValidationProps { /** The name of the input to validate */ name: string; } export interface OnBlurEventProps { onBlur?: (event?: TBlurEvent) => void | boolean; } export interface OnChangeEventProps { onChange?: (event?: TChangeEvent) => void | boolean; } export interface FieldSetProps extends BaseReactProps, OnBlurEventProps> { id: string; name: string; } export interface InputGroupProps extends BaseReactProps { prepend?: React.ReactNode; append?: React.ReactNode; } export interface ValueProp { value?: TValue; } export interface FieldSetNameSpaceProp { fieldSetNameSpace?: string; } export interface LabelProp { /** Add a label to the input*/ label?: string; } export interface TypeProp { /** What type of input is it [hidden|text|ect] */ type?: InputType; } export interface NameProp { /** Name of the input in state; You can append [] to signify a group of inputs*/ name: string; } export interface PlaceholderProp { /** A placeholder to display for the input field*/ placeholder?: string; } export interface IdProp { /** Used to provide extra identification information when name isnt enough */ id?: string; } export interface OptionsProp { options?: List; } export interface DefaultValueProp { /** Default value for the input to display */ defaultValue?: TDefault; } export interface AdditionalCompareProps { /** Additional props to check in should component update */ additionalCompareProps?: string[]; } export interface DefaultSwitchProps { defaultChecked?: boolean | string | number | undefined; defaultSelected?: boolean | string | number | undefined; } export interface InputWrapperProps extends BaseReactProps, LabelProp, NameProp, TypeProp { /** Pass in something to be prepended to the label */ labelPrefix?: any; /** Pass in something to be appended to the label */ labelPostfix?: any; /** Hint text */ explanation?: string | React.ReactElement; } export interface BaseInputProps> extends BaseReactProps, OnChangeEventProps, OnBlurEventProps>, ValidationProps, NameProp, TypeProp, IdProp, DefaultValueProp, ValueProp { /** Automatically select this field on navigation*/ autoFocus?: boolean; } export interface OptionalValidationProps { /** Disable the input*/ disabled?: boolean; /** Don't validate the input*/ noValidate?: boolean; } export interface OptionTypes { value: string | boolean | number; children: List; } export interface TextAreaProps extends BaseInputProps, CustomValidationProps, InputWrapperProps, PlaceholderProp, AdditionalCompareProps { /** The number of rows initially shown in the text area */ rows?: number; /** The resizing behaviour of the textarea */ resize?: 'none' | 'vertical' | 'horizontal'; } export interface TextInputProps extends BaseInputProps, CustomValidationProps, InputWrapperProps, InputGroupProps, PlaceholderProp, AdditionalCompareProps { /** The corner radius for the input area */ radius?: number; /** Type of preconfigured input */ format?: 'percentage' | 'dollar' | 'euro' | 'yen' | 'search'; } export interface SelectInputProps extends BaseInputProps, CustomValidationProps, InputWrapperProps, DefaultSwitchProps, AdditionalCompareProps { /** Pass in an arrow to display at the edge of the select box */ arrow?: React.ReactNode; } export interface MultiSelectProps extends InputWrapperProps, BaseInputProps>, AdditionalCompareProps, OptionsProp { noResultsText?: string; placeholder?: string; } export interface SwitchProps extends BaseInputProps, DefaultSwitchProps, LabelProp, AdditionalCompareProps { /** Put into state as the value of the selected switch */ id: string; } export interface RadioTabsProps extends BaseReactProps, NameProp, LabelProp { radioClasses?: string; } export interface RadioTabProps extends BaseReactProps, LabelProp { /** Put into state as the value of the selected switch */ id: string; defaultSelected?: boolean; } export interface ValidationElementProps extends BaseReactProps, NameProp, AdditionalCompareProps { /** What validation attribute is the message for */ isFor: InputTest; } export interface DisplayValidationProps extends BaseReactProps, OptionalValidationProps, CustomValidationProps, TypeProp, NameProp {} export interface DropZoneProps extends BaseReactProps, NameProp, ValueProp>, AdditionalCompareProps { /** Width of the droppable area */ width?: string; /** Height of the droppable area */ height?: string; /** Can you upload multiple files*/ multiple?: boolean; /** Display a list of uploaded files*/ showList?: boolean; /** Custom onDrop handler */ onDrop?: Function; /** Placeholder text inside the dropzone */ placeholder?: string; /** A function to display the dropped files */ fileListComponent?: (files: Set, deleteFile: (index: number) => any) => any; } export interface DateWrapperProps extends InputWrapperProps, InputGroupProps, BaseReactProps, PlaceholderProp {} export interface InternalDateWrapperProps extends DateWrapperProps { valueString: string; children: React.ReactElement; } export interface CommonDateProps extends BaseReactProps, NameProp, DateWrapperProps, OnChangeEventProps, AdditionalCompareProps { date?: Moment; dateFormat?: string; firstDayOfTheWeek?: number; theme?: Object; onInit?: (date?: DateRangeMoment | Moment) => void | boolean; minDate?: string | Moment | Function; maxDate?: string | Moment | Function; serverFormat?: string; } export interface DatePickerProps extends CommonDateProps, DefaultValueProp, ValueProp {} export interface DateRangeProps extends CommonDateProps, DefaultValueProp, ValueProp { startDate?: string | Moment | Function; endDate?: string | Moment | Function; value?: DateRangeMap; } export interface ErrorWrapperProps extends BaseReactProps, TypeProp {} export interface BaseAction extends Action { type: TType; } export interface FSA extends BaseAction { payload?: TPayload; meta?: any; } export interface InputInfoProps { inputInfo: Map; } export interface NameSpaceProp { nameSpace: string; } export interface ValidationComponentProps { isFor: string; } export interface FormStateProp { FormState: Map; } export interface FormContext extends NameSpaceProp, FormStateProp { fieldSetNameSpace: string; dispatch: Function; } export interface FormStateProps { /** Can optionally be passed down by the user to intergrate with redux global state */ FormState?: formState; } export interface FormDispatchProps { /** Can optionally be passed down by the user to intergrate with redux global state */ dispatch?: any; } export interface FormState { canSubmitString: string; } export interface FormOptionalProps extends BaseReactProps { /** Accepts different mime types and ensures the user specified onSubmit is called with data in the correct format * currently supports: application/json and multipart/form-data */ encType?: 'application/json' | 'multipart/form-data'; /** Called before the form is submitted, ths is a chance to modify the contents of the payload * primarily used by the form generator */ mapOutput?: (data?: Map) => Map; /** Called once Form has ensured that all child Input components are valid */ onSubmit?: OnSubmit; } export interface FormOwnProps extends FormOptionalProps { /** Used to namespace all child input components in the redux store or local state */ name: string; } export interface FormProps extends FormOwnProps, FormStateProps, FormDispatchProps {} export interface ClearAllInputsPayload { nameSpace: string; } export interface SetInputPayload extends ClearAllInputsPayload { inputName: string[]; value: PossibleInputValue; } export interface SetValidationPayload extends ClearAllInputsPayload { inputName: string[]; type: string; test: string | boolean; } export interface SetAllInputInteractionPayload extends ClearAllInputsPayload { value: boolean; interaction: string; } export interface SetInputInteractionPayload extends SetAllInputInteractionPayload { inputName: string[]; } export interface InputBlurred extends NameProp, NameSpaceProp, IdProp, FieldSetNameSpaceProp { dispatch: Dispatch; } export interface InputChanged extends NameProp, NameSpaceProp, IdProp, FieldSetNameSpaceProp { dispatch: Dispatch; } export interface SetValidation extends NameSpaceProp, NameProp, FieldSetNameSpaceProp { dispatch: Dispatch; } export interface ValidationCloneElementProps extends InputInfoProps, TypeProp { test?: TestType; name: string; setValidation: setValidation; } export interface ValidationAdditionProps extends ValidationElementProps, ValidationCloneElementProps {} export interface ValidationInnerElementProps { displayed: boolean; className: string; children: any; } export interface PerformanceWrapperUncalledValidationHelpers { setValidation: (props: SetValidation) => setValidation; compareAdditionalProps: (props: AdditionalCompareProps) => compareAdditionalProps; } export interface PerformanceWrapperUncalledInputHelpers extends PerformanceWrapperUncalledValidationHelpers { /** Update state with a new value for this input */ inputChanged?: (props: InputChanged) => inputChanged; /** Set the inputs state */ setInputBlurred?: (props: InputBlurred) => () => void; } export interface PerformanceWrapperInputHelpers { /** Update state with a new value for this input */ inputChanged: inputChanged; /** Set the inputs state */ setInputBlurred: inputBlurred; setValidation: setValidation; compareAdditionalProps: compareAdditionalProps; } export interface PerformanceWrapperWithHandlers extends PerformanceWrapperInputHelpers {} export interface PerformanceWrapperWithProps extends InputInfoProps, DefaultValueProp { inputPath: string[]; } export interface PerformanceWrapperProps extends PerformanceWrapperWithProps, PerformanceWrapperWithHandlers, FormContext {} export interface WithHandlersGuard extends NameProp, IdProp, TypeProp, DefaultSwitchProps, DefaultValueProp, NameProp, BaseReactProps, ValueProp, IdProp, TypeProp {}