Created
April 10, 2019 15:28
-
-
Save frankdors/474dc923fb027eefb44d8a989d63486e to your computer and use it in GitHub Desktop.
My Custom Quasar Input Number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
import { QInput } from 'quasar' | |
import { Money } from 'v-money' | |
import Util from '@services/util' | |
export default { | |
// Component <money> and Directive v-money https://github.com/vuejs-tips/v-money | |
// https://github.com/quasarframework/quasar/blob/v0.17/src/components/input/QInput.js | |
// https://github.com/quasarframework/quasar/blob/v0.17/src/mixins/input.js | |
extends: QInput, | |
name: 'QInputNumber', | |
components: { Money }, | |
props: { | |
precision: { | |
type: Number, | |
default: 2 | |
}, | |
decimal: { | |
type: String, | |
default: ',' | |
}, | |
thousands: { | |
type: String, | |
default: '.' | |
}, | |
min: { | |
type: Number, | |
default: 0 | |
}, | |
max: { | |
type: Number, | |
default: Number.MAX_SAFE_INTEGER | |
}, | |
step: { | |
type: Number, | |
default: null | |
}, | |
maxLength: { | |
type: [Number, String], | |
default: null | |
}, | |
align: { | |
type: String, | |
default: 'right' | |
} | |
}, | |
data () { | |
return { | |
model: this.value, | |
lastValue: this.value | |
} | |
}, | |
mounted () { | |
// Ajuste para "autofocus" não dar erro | |
this.$refs.input.focus = function (e) { | |
this.$el.focus(e) | |
} | |
}, | |
methods: { | |
focus () { | |
if (!this.disable && this.$refs.input && this.$refs.input.$el) { | |
this.$refs.input.$el.focus() | |
} | |
}, | |
blur () { | |
this.$refs.input && this.$refs.input.$el.blur() | |
}, | |
select () { | |
this.$refs.input.$el.select() | |
}, | |
onInput (value) { | |
// console.debug('QInputNumber - onInput - Value %s - Model: %s', value, this.model) | |
// Evita menor que minimo | |
if (value < this.min) { | |
// console.warn('onInput - Volta ao minimo:', this.min) | |
value = this.min | |
} | |
if (value !== this.model && value <= this.max) { | |
// console.debug('QInputNumber - onInput - Value %s - Model: %s', value, this.model) | |
this.$emit('input', value) | |
this.$emit('change', value) | |
} | |
}, | |
preventNotNumber (event) { | |
// Plus:107|187, Minus:109|189, Comma:110|188, Period:190|190, Up:38, Down:40 | |
if ([107, 187, 109, 189, 110, 188, 190, 194, 38, 40].includes(event.keyCode)) { | |
// console.warn('preventNotNumber - value: %s - key: %s - result: OK', event.target.value, event.keyCode) | |
event.stopPropagation() | |
event.preventDefault() | |
return false | |
} | |
return true | |
}, | |
onKeydown (event) { | |
// console.debug('keydown - Key: %s - Model: %s - lastValue: %s - value: %s', event.keyCode, this.model, this.lastValue, this.value) | |
// Up:38, Down:40, Plus:107|187, Minus:109|189 | |
if (this.step && [38, 40, 107, 187, 109, 189].includes(event.keyCode)) { | |
event.stopPropagation() | |
event.preventDefault() | |
// Aplica Step | |
this.applyStep([38, 107, 187].includes(event.keyCode)) | |
return false | |
} | |
if (!this.preventNotNumber(event)) return false | |
// Se está nos Limites | |
if (Util.limitLength(event)) { | |
this.lastValue = event.target.value | |
this.$emit('keydown', event) | |
} | |
return false | |
}, | |
onKeyup (event) { | |
// console.debug('keyup - Key: %s - Model: %s - lastValue: %s - value: %s', event.keyCode, this.model, this.lastValue, this.value) | |
if (!this.preventNotNumber(event)) return | |
// Se não está nos limites | |
if (!Util.limitLength(event)) { | |
// console.warn('keyup - Volta ao valor anterior:', this.lastValue) | |
event.target.value = this.lastValue | |
this.$emit('error-max', true) | |
return | |
} | |
// Evita menor que minimo | |
if (this.min && event.target.value.toNumber() < this.min) { | |
// console.warn('keyup - Volta ao minimo:', event.target.value) | |
event.target.value = this.min.toNumeric(this.precision) | |
} | |
this.$emit('error-max', false) | |
this.$emit('keyup', event) | |
}, | |
onWheel (event) { | |
// Sem delta ou nao usa Step | |
if (!event.wheelDelta || !this.step) { | |
event.stopPropagation() | |
event.preventDefault() | |
return false | |
} | |
// Aplica Step | |
this.applyStep(event.wheelDelta > 0) | |
}, | |
increment () { | |
this.applyStep(true) | |
}, | |
decrement () { | |
this.applyStep(false) | |
}, | |
applyStep (increment = true) { | |
// Define novo valor | |
const next = increment ? this.model + this.step : this.model - this.step | |
// console.warn('applyStep ', increment ? '+' : '-', this.step, ' - next:', next, '- model:', this.model) | |
// Se dentro dos limites, usa novo valor | |
if (next >= this.min && next <= this.max) { | |
this.model = next | |
this.$emit('input', next) | |
} | |
}, | |
__getInput (h) { | |
// console.log('__getInput', this.value, this.model) | |
// @see https://vuejs.org/v2/guide/render-function.html | |
return h('money', { | |
ref: 'input', | |
staticClass: 'col q-input-target q-no-input-spinner ellipsis text-' + this.align, | |
'class': this.inputClasses, | |
attrs: Object.assign({}, this.$attrs, { | |
// type: 'tel', // this.inputType, | |
placeholder: this.inputPlaceholder, | |
disabled: this.disable, | |
readonly: this.readonly, | |
min: this.min, | |
max: this.max, | |
maxLength: this.maxLength, | |
value: this.model, | |
precision: this.precision, | |
decimal: this.decimal, | |
thousands: this.thousands, | |
autocomplete: 'off' | |
}), | |
// domProps: { value: this.model }, | |
on: { | |
input: this.onInput, | |
focus: this.__onFocus, | |
blur: this.__onInputBlur | |
}, | |
nativeOn: { | |
keydown: this.onKeydown, | |
keyup: this.onKeyup, | |
paste: this.__onPaste, | |
animationstart: this.__onAnimationStart, | |
wheel: this.onWheel | |
} | |
}) | |
} | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@services/util
where is this from?