From 56bc9dd4e5f3e190a978391efc04a1465d846319 Mon Sep 17 00:00:00 2001 From: leigh-mil Date: Sat, 15 Jun 2019 13:24:17 -0400 Subject: [PATCH] update components to emit valid on field-change and use in TO form --- js/components/date_selector.js | 32 ++++++++++++++++++++------ js/components/text_input.js | 3 ++- js/components/upload_input.js | 14 +++++++++++ js/mixins/form.js | 2 ++ templates/components/date_picker.html | 9 ++++++-- templates/components/text_input.html | 2 +- templates/components/upload_input.html | 2 ++ templates/task_orders/edit.html | 31 +++++++++++++++++-------- 8 files changed, 75 insertions(+), 20 deletions(-) diff --git a/js/components/date_selector.js b/js/components/date_selector.js index 6603cced..02466fb6 100644 --- a/js/components/date_selector.js +++ b/js/components/date_selector.js @@ -24,6 +24,10 @@ export default { type: Boolean, default: false, }, + optional: { + type: Boolean, + default: true, + }, }, data: function() { @@ -35,6 +39,14 @@ export default { } }, + created: function() { + emitEvent('field-mount', this, { + optional: this.optional, + name: this.name, + valid: this.isDateValid, + }) + }, + watch: { month(newMonth, oldMonth) { if (!!newMonth && newMonth.length > 2) { @@ -84,7 +96,7 @@ export default { isYearValid: function() { // Emit a change event var valid = parseInt(this.year) >= 1 - this._emitChange('year', this.year, valid) + // this._emitChange('year', this.year, valid) return valid }, @@ -106,9 +118,9 @@ export default { isDateValid: function() { return ( - this.day && - this.month && - this.year && + !!this.day && + !!this.month && + !!this.year && this.isDayValid && this.isMonthValid && this.isYearValid && @@ -141,13 +153,19 @@ export default { }, methods: { - _emitChange: function(name, value, valid) { + onInput: function(e) { + console.log('emitting event') emitEvent('field-change', this, { - value: value, - name: name, + value: e.target.value, + name: this.name, watch: this.watch, + valid: this.isDateValid, }) }, + + _emitChange: function(name, value, valid) { + emitEvent('field-change', this, { value, name }) + }, }, render: function(createElement) { diff --git a/js/components/text_input.js b/js/components/text_input.js index c49bb075..4c22b6c4 100644 --- a/js/components/text_input.js +++ b/js/components/text_input.js @@ -31,6 +31,7 @@ export default { type: Boolean, default: false, }, + optional: Boolean, }, data: function() { @@ -124,7 +125,7 @@ export default { // Emit a change event emitEvent('field-change', this, { value: this._rawValue(value), - valid, + valid: this._isValid(value), name: this.name, watch: this.watch, }) diff --git a/js/components/upload_input.js b/js/components/upload_input.js index 93c0eb8f..9e848b4f 100644 --- a/js/components/upload_input.js +++ b/js/components/upload_input.js @@ -17,6 +17,7 @@ export default { }, props: { + name: String, initialData: { type: String, }, @@ -27,6 +28,10 @@ export default { type: Boolean, default: false, }, + optional: { + type: Boolean, + default: true, + }, }, data: function() { @@ -38,6 +43,14 @@ export default { } }, + created: function() { + emitEvent('field-mount', this, { + optional: this.optional, + name: this.name, + valid: this.hasAttachment, + }) + }, + methods: { addAttachment: function(e) { this.attachment = e.target.value @@ -48,6 +61,7 @@ export default { value: e.target.value, name: this.name, watch: this.watch, + valid: this.hasAttachment, }) }, removeAttachment: function(e) { diff --git a/js/mixins/form.js b/js/mixins/form.js index d4490708..01f2f24f 100644 --- a/js/mixins/form.js +++ b/js/mixins/form.js @@ -27,6 +27,8 @@ export default { handleFieldMount: function(event) { const { name, optional, valid } = event this.fields[name] = optional || valid + const formValid = this.validateForm() + this.invalid = !formValid }, validateForm: function() { diff --git a/templates/components/date_picker.html b/templates/components/date_picker.html index ee4edbaa..d6230859 100644 --- a/templates/components/date_picker.html +++ b/templates/components/date_picker.html @@ -6,15 +6,18 @@ description=field.description, mindate=None, maxdate=None, - watch=False) -%} + watch=False, + optional=True) -%}
@@ -29,7 +32,7 @@
- +
@@ -54,6 +57,7 @@ v-bind:class="{ 'usa-input-error': (day && !isDayValid) }" v-bind:max="daysMaxCalculation" v-model="day" + v-on:change="onInput" />
@@ -66,6 +70,7 @@ v-model="year" {% if maxdate %}max="{{ maxdate.year }}"{% endif %} {% if mindate %}min="{{ mindate.year }}"{% endif %} + v-on:change="onInput" />
diff --git a/templates/components/text_input.html b/templates/components/text_input.html index acbfae80..a979bd81 100644 --- a/templates/components/text_input.html +++ b/templates/components/text_input.html @@ -14,7 +14,7 @@ initial_value='', classes='', noMaxWidth=False, - optional=False, + optional=True, showLabel=True, watch=False) -%} diff --git a/templates/components/upload_input.html b/templates/components/upload_input.html index 2463367f..0c53a2e9 100644 --- a/templates/components/upload_input.html +++ b/templates/components/upload_input.html @@ -9,6 +9,8 @@ v-bind:initial-errors='true' {% endif %} v-bind:watch='{{ watch | string | lower }}' + name='{{ field.name }}' + :optional='false' >
diff --git a/templates/task_orders/edit.html b/templates/task_orders/edit.html index fb133a4e..c05381a7 100644 --- a/templates/task_orders/edit.html +++ b/templates/task_orders/edit.html @@ -87,8 +87,8 @@
- {{ DatePicker(fields.start_date, watch=True) }} - {{ DatePicker(fields.end_date, watch=True) }} + {{ DatePicker(fields.start_date, watch=True, optional=False) }} + {{ DatePicker(fields.end_date, watch=True, optional=False) }} {{ TextInput(fields.obligated_amount, validation='dollars', watch=True) }} @@ -117,7 +117,14 @@ {% call StickyCTA(text="Add Funding") %} - +
Add your task order
- {{ TextInput(form.number, validation='taskOrderNumber') }} + {{ TextInput(form.number, validation='taskOrderNumber', optional=False) }}
@@ -167,7 +174,7 @@
- +
@@ -233,7 +240,7 @@
- +
@@ -242,7 +249,7 @@
- +
@@ -254,6 +261,7 @@ type="number" v-bind:class="{ 'usa-input-error': (month && !isMonthValid) }" v-model="month" + v-on:change="onInput" />
@@ -267,6 +275,7 @@ v-bind:class="{ 'usa-input-error': (day && !isDayValid) }" v-bind:max="daysMaxCalculation" v-model="day" + v-on:change="onInput" />
@@ -277,6 +286,7 @@ maxlength="4" type="number" v-model="year" + v-on:change="onInput" />
@@ -288,7 +298,7 @@
- +
@@ -297,7 +307,7 @@
- +
@@ -309,6 +319,7 @@ type="number" v-bind:class="{ 'usa-input-error': (month && !isMonthValid) }" v-model="month" + v-on:change="onInput" />
@@ -322,6 +333,7 @@ v-bind:class="{ 'usa-input-error': (day && !isDayValid) }" v-bind:max="daysMaxCalculation" v-model="day" + v-on:change="onInput" />
@@ -332,6 +344,7 @@ maxlength="4" type="number" v-model="year" + v-on:change="onInput" />