From c8729c4599f5b3bc1c922ff1f098fb89afc2606c Mon Sep 17 00:00:00 2001 From: mplacek Date: Wed, 6 Sep 2017 08:24:59 +0200 Subject: [PATCH 1/3] Added onError event handling --- src/components/Form.jsx | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/components/Form.jsx b/src/components/Form.jsx index 7aafcc6..5fb5c72 100644 --- a/src/components/Form.jsx +++ b/src/components/Form.jsx @@ -45,6 +45,7 @@ class Form extends React.Component { componentWillMount() { this.storage.listen(this.setStateModel); if (this.eventsListener) { + this.eventsListener.registerEvent('onError'); this.eventsListener.registerEventListener('submit', this.submitListener); this.eventsListener.registerEventListener('validate', this.validateListener); this.eventsListener.registerEventListener('reset', this.resetListener); @@ -55,6 +56,7 @@ class Form extends React.Component { this.storage.unlisten(this.setStateModel); this.storage.setModel({}); if (this.eventsListener) { + this.eventsListener.unregisterEvent('onError'); this.eventsListener.unregisterEventListener('submit', this.submitListener); this.eventsListener.unregisterEventListener('validate', this.validateListener); this.eventsListener.unregisterEventListener('reset', this.resetListener); @@ -135,6 +137,7 @@ class Form extends React.Component { runSubmit(errors, modelData) { const model = cloneObject(modelData); if (Object.keys(errors).length > 0) { + if (this.eventsListener) this.eventsListener.callEvent('onError', errors); if (this.props.onError) this.props.onError(errors, model); return; } From 69e1bd7cc7108d86381d458a978d5de48325e7eb Mon Sep 17 00:00:00 2001 From: mplacek Date: Wed, 6 Sep 2017 15:55:22 +0200 Subject: [PATCH 2/3] Changes after CR --- src/components/Form.jsx | 4 +--- src/components/FormEventsListener.js | 1 + 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/components/Form.jsx b/src/components/Form.jsx index 5fb5c72..b948117 100644 --- a/src/components/Form.jsx +++ b/src/components/Form.jsx @@ -45,7 +45,6 @@ class Form extends React.Component { componentWillMount() { this.storage.listen(this.setStateModel); if (this.eventsListener) { - this.eventsListener.registerEvent('onError'); this.eventsListener.registerEventListener('submit', this.submitListener); this.eventsListener.registerEventListener('validate', this.validateListener); this.eventsListener.registerEventListener('reset', this.resetListener); @@ -56,7 +55,6 @@ class Form extends React.Component { this.storage.unlisten(this.setStateModel); this.storage.setModel({}); if (this.eventsListener) { - this.eventsListener.unregisterEvent('onError'); this.eventsListener.unregisterEventListener('submit', this.submitListener); this.eventsListener.unregisterEventListener('validate', this.validateListener); this.eventsListener.unregisterEventListener('reset', this.resetListener); @@ -137,7 +135,7 @@ class Form extends React.Component { runSubmit(errors, modelData) { const model = cloneObject(modelData); if (Object.keys(errors).length > 0) { - if (this.eventsListener) this.eventsListener.callEvent('onError', errors); + if (this.eventsListener) this.eventsListener.callEvent('error', errors); if (this.props.onError) this.props.onError(errors, model); return; } diff --git a/src/components/FormEventsListener.js b/src/components/FormEventsListener.js index 640e37e..c905ff5 100644 --- a/src/components/FormEventsListener.js +++ b/src/components/FormEventsListener.js @@ -5,6 +5,7 @@ class FormEventsListener { validate: [], changeModel: [], reset: [], + error: [], }; } registerEvent(name) { From fb203db1caf267d9165324e5c4d32b81d3bd9e61 Mon Sep 17 00:00:00 2001 From: mplacek Date: Wed, 6 Sep 2017 16:01:11 +0200 Subject: [PATCH 3/3] Updated test --- tests/FormEventsListener.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/FormEventsListener.test.js b/tests/FormEventsListener.test.js index c9c3ce7..c988324 100644 --- a/tests/FormEventsListener.test.js +++ b/tests/FormEventsListener.test.js @@ -17,7 +17,7 @@ describe('FormEventsListener', () => { it('should not unregister event if not exists', () => { eventsListener.unregisterEvent('afterSubmit'); - expect(Object.keys(eventsListener.eventsListeners).length).toBe(4); + expect(Object.keys(eventsListener.eventsListeners).length).toBe(5); }); it('should register event listener', () => {