commit bfa15cc01632840922978d1d3f79cfcee78417ef
parent d964a50a3a5fb9986dae553073398179089cc3c8
Author: ashermorgan <59518073+ashermorgan@users.noreply.github.com>
Date: Tue, 3 Aug 2021 20:49:30 -0700
Improve validation in IntInput component
Diffstat:
2 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/src/components/IntInput.vue b/src/components/IntInput.vue
@@ -20,6 +20,9 @@ export default {
min: {
type: Number,
default: 0,
+ validator: function(value) {
+ return value >= 0;
+ }
},
/**
@@ -28,6 +31,9 @@ export default {
max: {
type: Number,
default: null,
+ validator: function(value) {
+ return value >= 0;
+ }
},
},
@@ -70,27 +76,23 @@ export default {
// Make sure value is a number
if (isNaN(parsedValue)) {
if (newValue === '') {
- parsedValue = 0;
- this.stringValue = '0';
+ parsedValue = this.min;
}
else {
parsedValue = this.intValue;
- this.stringValue = oldValue;
}
}
// Enforce minimum and maximum
else if (this.min !== null && parsedValue < this.min) {
parsedValue = this.min;
- this.stringValue = this.min.toString();
}
else if (this.max !== null && parsedValue > this.max) {
parsedValue = this.max;
- this.stringValue = this.max.toString();
}
- // Make sure new value is correctly formatted
- else if (newValue !== parsedValue.toString()) {
+ // Update and format string value
+ if (newValue !== parsedValue.toString()) {
this.stringValue = parsedValue.toString();
}
diff --git a/tests/unit/IntInput.spec.js b/tests/unit/IntInput.spec.js
@@ -65,14 +65,14 @@ describe('IntInput.vue', () => {
expect(wrapper.emitted().input).to.be.undefined;
});
- it('should set empty input to 0', async () => {
+ it('should set empty input to minimum', async () => {
const wrapper = mount(IntInput, {
- propsData: { value: 1 }
+ propsData: { value: 5, min: 2 }
});
wrapper.find('input').element.value = '';
await wrapper.find('input').trigger('input');
- expect(wrapper.find('input').element.value).to.equal('0');
- expect(wrapper.emitted().input).to.deep.equal([[0]]);
+ expect(wrapper.find('input').element.value).to.equal('2');
+ expect(wrapper.emitted().input).to.deep.equal([[2]]);
});
it('should not allow input to be below the minimum', async () => {