Fixing validation issues in components
This commit is contained in:
parent
a1868f490b
commit
c89b0d4bef
@ -1,6 +1,7 @@
|
|||||||
import { OHIF } from 'meteor/ohif:core';
|
import { OHIF } from 'meteor/ohif:core';
|
||||||
import { Template } from 'meteor/templating';
|
import { Template } from 'meteor/templating';
|
||||||
import { Tracker } from 'meteor/tracker';
|
import { Tracker } from 'meteor/tracker';
|
||||||
|
import { Spacebars } from 'meteor/spacebars';
|
||||||
import { _ } from 'meteor/underscore';
|
import { _ } from 'meteor/underscore';
|
||||||
import { $ } from 'meteor/jquery';
|
import { $ } from 'meteor/jquery';
|
||||||
|
|
||||||
@ -26,7 +27,7 @@ OHIF.mixins.form = new OHIF.Mixin({
|
|||||||
instance.data.pathKey = '';
|
instance.data.pathKey = '';
|
||||||
|
|
||||||
// Debound the observer call to prevent tons of re-rendering
|
// Debound the observer call to prevent tons of re-rendering
|
||||||
component.validationRan = _.debounce(() => {
|
component.validationRan = _.throttle(() => {
|
||||||
// Enable reactivity by changing a Tracker.Dependency observer
|
// Enable reactivity by changing a Tracker.Dependency observer
|
||||||
component.validationObserver.changed();
|
component.validationObserver.changed();
|
||||||
}, 200);
|
}, 200);
|
||||||
@ -42,7 +43,7 @@ OHIF.mixins.form = new OHIF.Mixin({
|
|||||||
|
|
||||||
// Focus the first error field if some validation failed
|
// Focus the first error field if some validation failed
|
||||||
if (component.schema && component.schema._invalidKeys.length) {
|
if (component.schema && component.schema._invalidKeys.length) {
|
||||||
instance.$('.state-error :input:first').focus();
|
Tracker.afterFlush(() => instance.$('.state-error :input:first').focus());
|
||||||
}
|
}
|
||||||
|
|
||||||
return validationResult;
|
return validationResult;
|
||||||
|
|||||||
@ -75,7 +75,7 @@ OHIF.mixins.formItem = new OHIF.Mixin({
|
|||||||
// Set the component in error state and display the error message
|
// Set the component in error state and display the error message
|
||||||
component.error = errorMessage => {
|
component.error = errorMessage => {
|
||||||
// Set the component error state
|
// Set the component error state
|
||||||
component.state('error', errorMessage);
|
component.state('error', !!errorMessage);
|
||||||
|
|
||||||
// Set or remove the error message
|
// Set or remove the error message
|
||||||
if (errorMessage) {
|
if (errorMessage) {
|
||||||
@ -93,8 +93,8 @@ OHIF.mixins.formItem = new OHIF.Mixin({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create the tooltip
|
// Destroy the tooltip if created and create again
|
||||||
component.$wrapper.tooltip({
|
component.$wrapper.tooltip('destroy').tooltip({
|
||||||
trigger: 'manual',
|
trigger: 'manual',
|
||||||
title: message
|
title: message
|
||||||
}).tooltip('show');
|
}).tooltip('show');
|
||||||
@ -127,7 +127,7 @@ OHIF.mixins.formItem = new OHIF.Mixin({
|
|||||||
// Show the tooltip with the error message
|
// Show the tooltip with the error message
|
||||||
component.toggleTooltip(true, errorMessage);
|
component.toggleTooltip(true, errorMessage);
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
|
|
||||||
// Search for the parent form component
|
// Search for the parent form component
|
||||||
component.getForm = () => {
|
component.getForm = () => {
|
||||||
@ -227,13 +227,42 @@ OHIF.mixins.formItem = new OHIF.Mixin({
|
|||||||
|
|
||||||
// Add the pathKey to the wrapper element
|
// Add the pathKey to the wrapper element
|
||||||
component.$wrapper.attr('data-key', instance.data.pathKey);
|
component.$wrapper.attr('data-key', instance.data.pathKey);
|
||||||
|
|
||||||
|
// Get the component's form
|
||||||
|
const form = component.getForm();
|
||||||
|
|
||||||
|
// Observer for changes and revalidate the component
|
||||||
|
instance.autorun(computation => {
|
||||||
|
component.changeObserver.depend();
|
||||||
|
|
||||||
|
// Stop here if it is the first run
|
||||||
|
if (computation.firstRun) return;
|
||||||
|
|
||||||
|
// Revalidate the component if form is already validated
|
||||||
|
if (form && form.isValidatedAlready) {
|
||||||
|
component.validate();
|
||||||
|
}
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
onDestroyed() {
|
onDestroyed() {
|
||||||
const instance = Template.instance();
|
const instance = Template.instance();
|
||||||
|
const component = instance.component;
|
||||||
|
|
||||||
// Register the component in the parent component
|
// Get the component's form for further use
|
||||||
instance.component.unregisterSelf();
|
const form = component.getForm();
|
||||||
|
|
||||||
|
// Unregister the component in the parent component
|
||||||
|
component.unregisterSelf();
|
||||||
|
|
||||||
|
// Remove the component tooltip, error state and message
|
||||||
|
component.error(false);
|
||||||
|
component.toggleTooltip(false);
|
||||||
|
|
||||||
|
// Revalidate the form to remove this component from validation results
|
||||||
|
if (form && form.isValidatedAlready) {
|
||||||
|
form.validate();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
onMixins() {
|
onMixins() {
|
||||||
@ -260,12 +289,6 @@ OHIF.mixins.formItem = new OHIF.Mixin({
|
|||||||
if (event.currentTarget === component.$element[0]) {
|
if (event.currentTarget === component.$element[0]) {
|
||||||
// Enable reactivity by changing a Tracker.Dependency observer
|
// Enable reactivity by changing a Tracker.Dependency observer
|
||||||
component.changeObserver.changed();
|
component.changeObserver.changed();
|
||||||
|
|
||||||
const form = component.getForm();
|
|
||||||
if (form && form.isValidatedAlready) {
|
|
||||||
// Revalidate the component if form is already validated
|
|
||||||
component.validate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import { OHIF } from 'meteor/ohif:core';
|
import { OHIF } from 'meteor/ohif:core';
|
||||||
import { Template } from 'meteor/templating';
|
import { Template } from 'meteor/templating';
|
||||||
|
import { ReactiveVar } from 'meteor/reactive-var';
|
||||||
import { _ } from 'meteor/underscore';
|
import { _ } from 'meteor/underscore';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -111,6 +112,9 @@ OHIF.mixins.group = new OHIF.Mixin({
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset the validation
|
||||||
|
component.schema.resetValidation();
|
||||||
|
|
||||||
// Validate the component itself if it has a key
|
// Validate the component itself if it has a key
|
||||||
if (instance.data.pathKey && !validateSelf()) {
|
if (instance.data.pathKey && !validateSelf()) {
|
||||||
result = false;
|
result = false;
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
{{#each message in this.messages}}
|
{{#each message in this.messages}}
|
||||||
<div class="message">{{{message}}}</div>
|
<div class="message">{{{message}}}</div>
|
||||||
{{else}}
|
{{else}}
|
||||||
{{#let message=(choose this.message 'An error has ocurred.')}}
|
{{#let message=(choose this.reason this.message 'An error has ocurred.')}}
|
||||||
<div class="message">{{message}}</div>
|
<div class="message">{{message}}</div>
|
||||||
{{/let}}
|
{{/let}}
|
||||||
{{/each}}
|
{{/each}}
|
||||||
|
|||||||
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
label.wrapperLabel
|
label.wrapperLabel
|
||||||
cursor: pointer
|
cursor: pointer
|
||||||
|
|
||||||
|
label.wrapperLabel:not(.checkboxLabel)
|
||||||
|
cursor: pointer
|
||||||
display: flex
|
display: flex
|
||||||
flex-direction: column
|
flex-direction: column
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user