control error display for options input component
This commit is contained in:
dandds 2018-11-08 11:43:29 -05:00 committed by GitHub
commit b661c7ce4c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 13 deletions

View File

@ -2,8 +2,24 @@ export default {
name: 'optionsinput', name: 'optionsinput',
props: { props: {
name: String name: String,
initialErrors: {
type: Array,
default: () => []
}, },
initialValue: String,
},
data: function () {
const showError = (this.initialErrors && this.initialErrors.length) || false
return {
showError: showError,
showValid: !showError && !!this.initialValue,
validationError: this.initialErrors.join(' ')
}
},
methods: { methods: {
onInput: function (e) { onInput: function (e) {
@ -11,6 +27,8 @@ export default {
value: e.target.value, value: e.target.value,
name: this.name name: this.name
}) })
this.showError = false
this.showValid = true
} }
} }
} }

View File

@ -46,13 +46,13 @@
fieldset { fieldset {
input[type='radio'] { input[type='radio'] {
+ label::before { + label::before {
box-shadow: 0 0 0 1px $color-white, 0 0 0 3px $color-red; box-shadow: 0 0 0 1px $color-white, 0 0 0 3px $state-color;
} }
} }
input[type='checkbox'] { input[type='checkbox'] {
+ label::before { + label::before {
box-shadow: 0 0 0 2px $color-red; box-shadow: 0 0 0 2px $state-color;
} }
} }
} }

View File

@ -2,8 +2,14 @@
{% from "components/tooltip.html" import Tooltip %} {% from "components/tooltip.html" import Tooltip %}
{% macro OptionsInput(field, tooltip, inline=False) -%} {% macro OptionsInput(field, tooltip, inline=False) -%}
<optionsinput name='{{ field.name }}' inline-template key='{{ field.name }}'> <optionsinput
<div class='usa-input {% if field.errors %}usa-input--error{% endif %}'> name='{{ field.name }}'
inline-template
{% if field.errors %}v-bind:initial-errors='{{ field.errors | list }}'{% endif %}
{% if field.data and field.data != "None" %}v-bind:initial-value="'{{ field.data }}'"{% endif %}
key='{{ field.name }}'>
<div
v-bind:class="['usa-input', { 'usa-input--error': showError, 'usa-input--success': showValid }]">
<fieldset v-on:change="onInput" class="usa-input__choices {% if inline %}usa-input__choices--inline{% endif %}"> <fieldset v-on:change="onInput" class="usa-input__choices {% if inline %}usa-input__choices--inline{% endif %}">
<legend> <legend>
@ -16,18 +22,15 @@
<span class='usa-input__help'>{{ field.description | safe }}</span> <span class='usa-input__help'>{{ field.description | safe }}</span>
{% endif %} {% endif %}
{% if field.errors %} <span v-show='showError'>{{ Icon('alert',classes="icon-validation") }}</span>
{{ Icon('alert',classes="icon-validation") }} <span v-show='showValid'>{{ Icon('ok',classes="icon-validation") }}</span>
{% endif %}
</legend> </legend>
{{ field() }} {{ field() }}
{% if field.errors %} <template v-if='showError'>
{% for error in field.errors %} <span class='usa-input__message' v-html='validationError'></span>
<span class='usa-input__message'>{{ error }}</span> </template>
{% endfor %}
{% endif %}
</fieldset> </fieldset>
</div> </div>