Skip to content

Commit

Permalink
Merge pull request #50 from Qvant-lab/qinputnumber_fix
Browse files Browse the repository at this point in the history
[QInputNumber] fix null value, fix "-" value, add placeholder
  • Loading branch information
cheesytim authored Jan 29, 2021
2 parents 6980985 + dfc878b commit 0bc7dd9
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 23 deletions.
23 changes: 23 additions & 0 deletions src/qComponents/QInputNumber/QInputNumber.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Component from './src/QInputNumber';

describe('QInputNumber', () => {
it('should match snapshot', () => {
const { element } = shallowMount(Component);

expect(element).toMatchSnapshot();
});

it('should match snapshot without controls', () => {
const { element } = shallowMount(Component, {
propsData: {
controls: false
}
});

expect(element).toMatchSnapshot();
});

it('data should match snapshot', () => {
expect(Component.data()).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`QInputNumber data should match snapshot 1`] = `
Object {
"number": null,
"prevNumber": null,
"userNumber": null,
}
`;

exports[`QInputNumber should match snapshot 1`] = `
<div
class="q-input-number q-input-number_with-controls"
>
<button
class="q-input-number__button_decrease q-input-number__button q-icon-minus"
/>
<q-input-stub
autocomplete="off"
class="q-input-number__input"
label=""
suffixicon=""
tabindex=""
type="number"
value=""
/>
<button
class="q-input-number__button_increase q-input-number__button q-icon-plus"
/>
</div>
`;

exports[`QInputNumber should match snapshot without controls 1`] = `
<div
class="q-input-number"
>
<!---->
<q-input-stub
autocomplete="off"
class="q-input-number__input"
label=""
suffixicon=""
tabindex=""
type="number"
value=""
/>
<!---->
</div>
`;
82 changes: 62 additions & 20 deletions src/qComponents/QInputNumber/src/QInputNumber.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
:value="currentValue"
class="q-input-number__input"
:disabled="isDisabled"
:placeholder="placeholder"
:validate-event="false"
type="number"
@blur="handleBlur"
@focus="handleFocus"
@input="handleInput"
@change="handleChange"
@input="handleChangeInput($event, 'input')"
@change="handleChangeInput($event, 'change')"
/>

<button
Expand All @@ -39,6 +42,9 @@ export default {
inject: {
qForm: {
default: null
},
qFormItem: {
default: null
}
},
Expand Down Expand Up @@ -88,6 +94,10 @@ export default {
type: Boolean,
default: true
},
placeholder: {
type: String,
default: null
},
value: {
type: [Number, String],
default: null,
Expand All @@ -96,13 +106,18 @@ export default {
return false;
}
},
validateEvent: {
type: Boolean,
default: true
}
},
data() {
return {
number: null,
userNumber: null
userNumber: null,
prevNumber: null
};
},
Expand Down Expand Up @@ -150,50 +165,77 @@ export default {
const updatedNumber = Math.round((this.number + this.step) * 100) / 100;
if (updatedNumber > this.max) return;
this.changesEmmiter(updatedNumber);
this.userNumber = updatedNumber;
this.changesEmmiter(updatedNumber, 'change');
},
handleDecreaseClick() {
const updatedNumber = Math.round((this.number - this.step) * 100) / 100;
if (updatedNumber < this.min) return;
this.changesEmmiter(updatedNumber);
this.userNumber = updatedNumber;
this.changesEmmiter(updatedNumber, 'change');
},
handleBlur(event) {
this.$emit('blur', event);
if (this.validateEvent) this.qFormItem?.validateField('blur');
},
handleFocus(event) {
this.$emit('focus', event);
},
handleInput(value) {
this.userNumber = value;
handleChangeInput(value, type) {
if (!value) {
this.userNumber = value;
this.changesEmmiter(null, type);
return;
}
this.processUserValue(value, type);
},
handleChange(value) {
processUserValue(value, type) {
const userValue = Number(value);
this.userNumber = null;
if (
!value.length ||
Number.isNaN(userValue) ||
value > this.max ||
value < this.min
) {
if (Number.isNaN(userValue) || value > this.max || value < this.min) {
return;
}
this.prevNumber = userValue;
if (type === 'change') {
this.changesEmmiter(userValue, type);
return;
}
this.changesEmmiter(userValue);
this.$emit('input', Number(userValue.toFixed(this.precision)));
if (this.validateEvent) this.qFormItem?.validateField('input');
},
changesEmmiter(value) {
this.number = Number(value.toFixed(this.precision));
changesEmmiter(value, type) {
let passedData = null;
if (value) {
this.number = Number(value.toFixed(this.precision));
passedData = this.number;
}
this.prevNumber = passedData;
if (type === 'change') {
this.$emit('input', passedData);
this.$emit('change', passedData);
if (this.validateEvent) this.qFormItem?.validateField('change');
return;
}
this.$emit('input', this.number);
this.$emit('change', this.number);
this.$emit('input', passedData);
if (this.validateEvent) this.qFormItem?.validateField('input');
}
}
};
Expand Down
17 changes: 14 additions & 3 deletions stories/components/QInputNumber.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,22 @@ export const Default = (_, { argTypes }) => ({
props: Object.keys(argTypes).filter(val => val !== 'value'),
data() {
return {
value: ''
value: 2
};
},
template: '<q-input-number v-bind="$props" v-model="value" />'
methods: {
handleEmit(value, type) {
console.log(value, type);
}
},
template: `
<q-input-number
v-bind="$props"
v-model="value"
@input="handleEmit($event, 'input')"
@change="handleEmit($event, 'change')"
/>
`
});

Default.storyName = 'Default';
Default.args = { value: 17 };

0 comments on commit 0bc7dd9

Please sign in to comment.