diff --git a/packages/client/src/login/LoginConnector.tsx b/packages/client/src/login/LoginConnector.tsx index c0fc842..d4554b6 100644 --- a/packages/client/src/login/LoginConnector.tsx +++ b/packages/client/src/login/LoginConnector.tsx @@ -27,6 +27,7 @@ export class LoginConnector extends Connector { }), dispatch => ({ onSubmit: bindActionCreators(this.loginActions.logIn, dispatch), + clearOnSuccess: true, }), withForm(LoginForm, defaultCredentials), ) diff --git a/packages/client/src/login/LoginForm.test.tsx b/packages/client/src/login/LoginForm.test.tsx index 0512578..1ebad58 100644 --- a/packages/client/src/login/LoginForm.test.tsx +++ b/packages/client/src/login/LoginForm.test.tsx @@ -33,7 +33,8 @@ describe('LoginForm', () => { data, }, {id: 123}) - node = t.render({onSuccess}).node + const r = t.render({onSuccess}) + node = r.node T.Simulate.change( node.querySelector('input[name="username"]')!, {target: {value: 'user'}} as any, @@ -44,7 +45,7 @@ describe('LoginForm', () => { ) }) - it('should submit a form', async () => { + it('should submit a form and clear it', async () => { T.Simulate.submit(node) const {req} = await http.wait() expect(req).toEqual({ @@ -53,6 +54,16 @@ describe('LoginForm', () => { data, }) expect(onSuccess.mock.calls.length).toBe(1) + expect( + (node.querySelector('input[name="username"]') as HTMLInputElement) + .value, + ) + .toEqual('') + expect( + (node.querySelector('input[name="password"]') as HTMLInputElement) + .value, + ) + .toEqual('') }) it('sets the error message on failure', async () => { diff --git a/packages/client/src/login/RegisterConnector.tsx b/packages/client/src/login/RegisterConnector.tsx index aa628e3..8b34517 100644 --- a/packages/client/src/login/RegisterConnector.tsx +++ b/packages/client/src/login/RegisterConnector.tsx @@ -26,6 +26,7 @@ export class RegisterConnector extends Connector { }), dispatch => ({ onSubmit: bindActionCreators(this.loginActions.register, dispatch), + clearOnSuccess: true, }), withForm(RegisterForm, defaultCredentials), ) diff --git a/packages/client/src/login/withForm.tsx b/packages/client/src/login/withForm.tsx index ea38c88..1ae8d44 100644 --- a/packages/client/src/login/withForm.tsx +++ b/packages/client/src/login/withForm.tsx @@ -13,6 +13,7 @@ export interface IFormHOCProps { // TODO figure out what would happen if the underlying child component // would have the same required property as the HOC, like onSuccess? onSuccess?: () => void + clearOnSuccess?: boolean } export function withForm>( @@ -30,9 +31,12 @@ export function withForm>( this.state = initialState } handleSubmit = async (e: React.FormEvent) => { - const {onSuccess} = this.props + const {clearOnSuccess, onSuccess} = this.props e.preventDefault() await this.props.onSubmit(this.state) + if (clearOnSuccess) { + this.setState(initialState) + } if (onSuccess) { onSuccess() }