Skip to content

Commit

Permalink
Merge 4e1855a into 3784cec
Browse files Browse the repository at this point in the history
  • Loading branch information
vitkon committed Nov 26, 2017
2 parents 3784cec + 4e1855a commit ff79e3e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ It does not modify the component class passed to it; instead, it *returns* a new

- `initialModel: Partial<T>` — object provides initial values to the form fields
- `middleware: (props: T) => any` — function transforms props passed to the wrapped component
- `onInputBlur: (e: React.ForcusEvent<any>) => any` — function is called on every blur on an input field within the form. Adding a custom `onBlur` to the input field itself is not recommended, use this method instead
- `onInputBlur: (e: React.ForcusEvent<any>, formState?: {form: Partial<IFormState>}) => any` — function is called on every blur of an input field within the form. Adding a custom `onBlur` to the input field itself is not recommended, use this method instead

## Validation

Expand Down
20 changes: 12 additions & 8 deletions src/FormContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { flow, isNil } from 'lodash';
import * as validation from './validate';
import { IFormConfig } from './interfaces';
import { IFormConfig, IFormState } from './interfaces';

const hoistNonReactStatics = require('hoist-non-react-statics');

Expand Down Expand Up @@ -83,7 +83,9 @@ const makeWrapper = <T extends {}>(config: IFormConfig) => (WrappedComponent: an
this.setFieldToTouched(target.name as keyof T);

if (config.onInputBlur) {
config.onInputBlur(e);
config.onInputBlur(e, {
form: this.getFormState()
});
}
}

Expand All @@ -103,15 +105,17 @@ const makeWrapper = <T extends {}>(config: IFormConfig) => (WrappedComponent: an
});
}
}
})
});

private getFormState = (): Partial<IFormState> => ({
model: this.state.model,
inputs: this.state.inputs,
touched: this.state.touched
});

render() {
const nextProps = Object.assign({}, this.props, {
form: {
model: this.state.model,
inputs: this.state.inputs,
touched: this.state.touched,
},
form: this.getFormState(),
formMethods: {
bindInput: this.bindInput,
bindToChangeEvent: this.bindToChangeEvent,
Expand Down
23 changes: 14 additions & 9 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ export type ErrorMessage = {
[name: string]: string | undefined
}

export type ValidationRule = <T = any>(prop: keyof T, errorMessage: string, attr?: any) => [
export type ValidationRule = <T = any>(prop: keyof T, errorMessage?: string, attr?: any) => [
Validator,
ErrorMessage
];

export interface IFormState {
model: any;
inputs: any;
isValid?: boolean;
validationErrors: { [key: string]: string };
touched: { [key: string]: boolean };
}

export interface IFormProps<T = any> {
form: {
model: any;
inputs: any;
isValid?: boolean;
validationErrors: { [key: string]: string };
touched: { [key: string]: boolean };
};
form: IFormState;
formMethods: {
bindInput: (name: string) => any;
bindToChangeEvent: (e: React.ChangeEvent<any>) => void;
Expand All @@ -34,6 +36,9 @@ export interface IFormProps<T = any> {

export interface IFormConfig<T = any> {
initialModel?: Partial<T>;
onInputBlur?: (e: React.FocusEvent<any>) => any;
onInputBlur?: (
e: React.FocusEvent<any>,
formState?: {form: Partial<IFormState>}
) => any;
middleware?: (props: T) => any;
}

0 comments on commit ff79e3e

Please sign in to comment.