From 6f98d81db1c9d5b3434faeb5eb8b760638e94a49 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Wed, 10 Apr 2019 12:04:11 +0200 Subject: [PATCH] fixed input time --- front/core/components/input-number/index.js | 53 ++++++++++++++++--- .../components/input-number/index.spec.js | 4 +- modules/agency/front/basic-data/index.html | 20 +++---- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/front/core/components/input-number/index.js b/front/core/components/input-number/index.js index 458c6d61f..efae9c74a 100644 --- a/front/core/components/input-number/index.js +++ b/front/core/components/input-number/index.js @@ -21,6 +21,8 @@ export default class InputNumber extends Input { if (!isNaN(this.value)) this.input.value = this.value; + this.validateValue(); + this.emit('change', {event}); }); @@ -42,8 +44,11 @@ export default class InputNumber extends Input { * @param {Number} value - Value */ set value(value) { + if (!this.hasOwnProperty('_value') && value) + this.input.value = value; + this._value = value; - this.hasValue = this._value !== null; + this.hasValue = !(value === null || value === undefined || value === ''); if (this.hasValue) this.element.classList.add('not-empty'); @@ -102,6 +107,10 @@ export default class InputNumber extends Input { this.input.step = value; } + get validationError() { + return this.input.validationMessage; + } + /** * Increases the input value */ @@ -131,12 +140,42 @@ export default class InputNumber extends Input { */ validateValue() { if (!this.hasValidValue()) { - this.element.querySelector('.infix') - .classList.add('invalid', 'validated'); - } else { - this.element.querySelector('.infix') - .classList.remove('invalid', 'validated'); - } + this.hideError(); + this.showError(); + } else + this.hideError(); + } + + /** + * Shows the input validation error + */ + showError() { + const infixElement = this.element.querySelector('.infix'); + const infixClassList = infixElement.classList; + + const errorSpan = document.createElement('span'); + errorSpan.className = 'mdl-textfield__error'; + + const errorText = document.createTextNode(this.validationError); + + errorSpan.append(errorText); + infixElement.append(errorSpan); + + infixClassList.add('validated', 'invalid'); + } + + /** + * Hides the input validation error + */ + hideError() { + const infixElement = this.element.querySelector('.infix'); + const infixClassList = infixElement.classList; + const errorElement = this.element.querySelector('.infix span.mdl-textfield__error'); + + if (errorElement) + errorElement.remove(); + + infixClassList.remove('validated', 'invalid'); } } diff --git a/front/core/components/input-number/index.spec.js b/front/core/components/input-number/index.spec.js index 18d51512e..3da82fe21 100644 --- a/front/core/components/input-number/index.spec.js +++ b/front/core/components/input-number/index.spec.js @@ -50,7 +50,7 @@ describe('Component vnInputNumber', () => { controller.validateValue(); let classes = controller.element.querySelector('.infix').classList.toString(); - expect(classes).not.toContain('invalid validated'); + expect(classes).not.toContain('validated invalid'); }); it(`should call hasValidValue() and add the class invalid and validated`, () => { @@ -60,7 +60,7 @@ describe('Component vnInputNumber', () => { controller.validateValue(); let classes = controller.element.querySelector('.infix').classList.toString(); - expect(classes).toContain('invalid validated'); + expect(classes).toContain('validated invalid'); }); }); }); diff --git a/modules/agency/front/basic-data/index.html b/modules/agency/front/basic-data/index.html index b6f615cef..7241e1c9d 100644 --- a/modules/agency/front/basic-data/index.html +++ b/modules/agency/front/basic-data/index.html @@ -32,31 +32,27 @@ - + model="$ctrl.zone.travelingDays" + min="0" step="1"> + model="$ctrl.zone.hour"> + model="$ctrl.zone.price" + min="0" step="0.01"> + model="$ctrl.zone.bonus" + min="0" step="0.01">