cleaner inputs for Wealth

This commit is contained in:
2019-06-14 14:31:46 +02:00
parent 109a01a368
commit 0e71776f4f
2 changed files with 44 additions and 19 deletions

View File

@@ -0,0 +1,28 @@
<template>
<input type="text"
class="input"
:class="{'is-danger': has_error}"
:value="value"
@input="checkError"
></input>
</template>
<script>
export default {
props: ["value"],
data () {
return { has_error: false};
},
methods: {
checkError (ev) {
const newValue = ev.target.value;
this.has_error = isNaN(newValue);
this.$emit(
'input',
// Do the conversion if valid
this.has_error ? newValue : Number(newValue)
);
}
},
}
</script>

View File

@@ -25,11 +25,7 @@
<nav class="columns is-1 is-variable is-mobile"> <nav class="columns is-1 is-variable is-mobile">
<template v-for="i in 4"> <template v-for="i in 4">
<div :key="`input-col-${i}`" class="column"> <div :key="`input-col-${i}`" class="column">
<input type="text" size="6" class="input" <NumberInput v-model="edit_values[i - 1]"></NumberInput>
:key="inputs_key"
:class="{'is-danger': form_errors[i - 1]}"
v-model.number="edit_values[i - 1]">
</input>
</div> </div>
</template> </template>
</nav> </nav>
@@ -52,34 +48,35 @@
</template> </template>
<script> <script>
import NumberInput from './NumberInput.vue'
export default { export default {
components: { NumberInput },
props: ["wealth", "edit"], props: ["wealth", "edit"],
data () { data () {
return { return {
edit_values: [0,0,0,0], edit_values: [0,0,0,0],
form_errors: [false,false,false,false],
inputs_key: 0, // Hack to re-render inputs
}; };
}, },
methods: { methods: {
updateWealth (op) { updateWealth (op) {
const values = this.edit_values; const values = this.edit_values;
this.form_errors.fill(false); // Reset error status // Is it optimal, considering NumberInput already validates numbers ?
values.forEach((v,i) => { // Check that all fields are valid numbers
if (isNaN(v)) { const success = values
console.log("error with value", v); .map(v => !isNaN(v))
this.form_errors[i] = true; .reduce(
}; (t,v) => t && v,
}); true);
const success = !this.form_errors.includes(true);
if (success) { if (success) {
console.log('updated', op, values); console.log('updated', op, values);
this.$emit('updated'); this.$emit('updated');
this.edit_values.fill(0) } this.resetValues();
else { } else {
console.log(this.form_errors); console.log('correct errors');
this.inputs_key += 1;
} }
},
resetValues () {
this.edit_values.fill(0);
} }
} }
} }