| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- import { VantComponent } from '../common/component'; // Note that the bitwise operators and shift operators operate on 32-bit ints
- // so in that case, the max safe integer is 2^31-1, or 2147483647
- var MAX = 2147483647;
- VantComponent({
- field: true,
- classes: ['input-class', 'plus-class', 'minus-class'],
- props: {
- value: null,
- integer: Boolean,
- disabled: Boolean,
- asyncChange: Boolean,
- disableInput: Boolean,
- min: {
- type: null,
- value: 1
- },
- max: {
- type: null,
- value: MAX
- },
- step: {
- type: null,
- value: 1
- }
- },
- computed: {
- minusDisabled: function minusDisabled() {
- return this.data.disabled || this.data.value <= this.data.min;
- },
- plusDisabled: function plusDisabled() {
- return this.data.disabled || this.data.value >= this.data.max;
- }
- },
- watch: {
- value: function value(_value) {
- if (_value !== '') {
- this.set({
- value: this.range(_value)
- });
- }
- }
- },
- data: {
- focus: false
- },
- created: function created() {
- this.set({
- value: this.range(this.data.value)
- });
- },
- methods: {
- onFocus: function onFocus() {
- this.setData({
- focus: true
- });
- },
- // limit value range
- range: function range(value) {
- return Math.max(Math.min(this.data.max, value), this.data.min);
- },
- onInput: function onInput(event) {
- var _ref = event.detail || {},
- _ref$value = _ref.value,
- value = _ref$value === void 0 ? '' : _ref$value;
- this.triggerInput(value);
- },
- onChange: function onChange(type) {
- if (this.data[type + "Disabled"]) {
- this.$emit('overlimit', type);
- return;
- }
- var diff = type === 'minus' ? -this.data.step : +this.data.step;
- var value = Math.round((this.data.value + diff) * 100) / 100;
- this.triggerInput(this.range(value));
- this.$emit(type);
- },
- onBlur: function onBlur(event) {
- var value = this.range(this.data.value);
- this.triggerInput(value);
- this.$emit('blur', event);
- },
- onMinus: function onMinus() {
- this.onChange('minus');
- },
- onPlus: function onPlus() {
- this.onChange('plus');
- },
- triggerInput: function triggerInput(value) {
- this.set({
- value: this.data.asyncChange ? this.data.value : value
- });
- this.$emit('change', value);
- }
- }
- });
|