Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/FormContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ const makeWrapper = <T extends {}>(config: IFormConfig<T>) => (WrappedComponent:
};
}

setModel = (model: { [name in keyof T]: any }) => {
this.setState({ model });
setModel = (model: { [name in keyof T]: any }, setTouched: boolean = false) => {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method has one single responsibility to set the model. I think setTouched shouldn't reside in this method.

let touched: { [key: string]: boolean } = this.state.touched;

if (!setTouched) {
this.setState({ model });
} else {
for (var field in model) {
touched[field] = true;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a setFieldToTouched method exposed as public API on the form. Will it help to solve the problem?

}
this.setState({ model, touched });
}
return model;
};

Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/FormContainer.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,27 @@ describe('Form container', () => {
});
});

describe('setModel', () => {
it('should set the model and optionally set the touched field', () => {
const { wrapperComponent, wrappedComponent } = setupTest();
let state: any = wrapperComponent.state();

const setModel = wrappedComponent.props().formMethods.setModel;

let data = { foo: 2 };
setModel(data);
state = wrapperComponent.state();
expect(state.model).toEqual(data);
expect(state.touched).toEqual({});

let data = { bar: 1234 };
setModel(data, true);
state = wrapperComponent.state();
expect(state.model).toEqual(data);
expect(state.touched).toEqual({ bar: true });
});
});

describe('bindInput', () => {
it('should have an input with a name and a value', () => {
const { input } = setupTest();
Expand Down