Skip to content

Commit

Permalink
refactor(input): minor refactor masked integer input
Browse files Browse the repository at this point in the history
  • Loading branch information
shrpne committed Aug 17, 2022
1 parent 4ee910f commit bf276e9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 67 deletions.
2 changes: 1 addition & 1 deletion components/CoinLockForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<InputMaskedInteger
class="form-field__input" v-check-empty
v-model="form.dueBlock"
@blur="$v.form.dueBlock.$touch()"
@blur="$v.form.dueBlock.$touch(); $emit('my-blur')"
/>
<span class="form-field__label">{{ $td('Due block', 'form.lock-due-block') }}</span>
</label>
Expand Down
8 changes: 5 additions & 3 deletions components/common/InputMaskedAmount.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import {IMaskDirective} from 'vue-imask';
export default {
ideFix: null,
imaskAmount: {
mask: Number,
scale: 18, // digits after point, 0 for integers
Expand All @@ -26,6 +25,9 @@
type: [String, Number],
},
},
emits: [
'input',
],
data() {
return {
// inner value set by imask
Expand All @@ -41,7 +43,7 @@
imaskOptions() {
return {
...this.$options.imaskAmount,
scale: this.scale || this.$options.imaskAmount.scale,
scale: this.scale ?? this.$options.imaskAmount.scale,
};
},
},
Expand Down Expand Up @@ -75,5 +77,5 @@
</script>
<template>
<input type="text" autocapitalize="off" inputmode="decimal" v-imask="imaskOptions" v-on="listeners" @accept="onAcceptInput" ref="input"/>
<input type="text" autocapitalize="off" inputmode="decimal" v-imask="imaskOptions" v-on="listeners" @accept="onAcceptInput($event)" ref="input"/>
</template>
91 changes: 28 additions & 63 deletions components/common/InputMaskedInteger.vue
Original file line number Diff line number Diff line change
@@ -1,69 +1,34 @@
<script>
import 'core-js/features/global-this.js';
import {IMaskDirective} from 'vue-imask';
import InputMaskedAmount from '~/components/common/InputMaskedAmount.vue';
export default {
ideFix: null,
imaskAmount: {
mask: Number,
scale: 0, // digits after point, 0 for integers
signed: false, // disallow negative
thousandsSeparator: '', // any single char
padFractionalZeros: false, // if true, then pads zeros at end to the length of scale
normalizeZeros: false, // appends or removes zeros at ends
radix: '.', // fractional delimiter
mapToRadix: [','], // symbols to process as radix
},
directives: {
imask: IMaskDirective,
},
props: {
value: {
type: [String, Number],
default: '',
},
},
data() {
return {
// inner value set by imask
maskedValue: '',
};
},
computed: {
// pass native events
inputListeners: function() {
let listeners = Object.assign({}, this.$listeners);
// input event fires separately
delete listeners.input;
return listeners;
},
},
watch: {
value(newVal) {
// typed value has to be updated if prop value changed programmatically
if (newVal !== this.maskedValue) {
this.updateMaskState(newVal);
}
},
},
mounted() {
this.updateMaskState(this.value);
},
methods: {
updateMaskState(value) {
this.$refs.input.maskRef.typedValue = value;
const maskedValue = this.$refs.input.maskRef._value;
const cursorPos = maskedValue.length;
this.$refs.input.maskRef._selection = {start: cursorPos, end: cursorPos};
},
onAcceptInput(e) {
this.maskedValue = e.detail._unmaskedValue;
this.$emit('input', e.detail._unmaskedValue);
},
},
};
export default {
components: {
InputMaskedAmount,
},
props: {
value: {
type: [String, Number],
default: '',
},
},
computed: {
// pass native events
inputListeners: function() {
let listeners = Object.assign({}, this.$listeners);
// input event fires separately
delete listeners.input;
return listeners;
},
},
};
</script>

<template>
<input type="text" autocapitalize="off" inputmode="numeric" v-imask="$options.imaskAmount" v-on="inputListeners" @accept="onAcceptInput" ref="input"/>
<InputMaskedAmount
inputmode="numeric"
:value="value"
:scale="0"
v-on="inputListeners"
@input="$emit('input', $event)"
/>
</template>

0 comments on commit bf276e9

Please sign in to comment.