Blame view

src/ui/vant-weapp/stepper/index.js 4.98 KB
simon committed
1
import { VantComponent } from '../common/component';
simon committed
2 3 4 5 6 7 8 9
import { addUnit, isDef } from '../common/utils';
const LONG_PRESS_START_TIME = 600;
const LONG_PRESS_INTERVAL = 200;
// add num and avoid float number
function add(num1, num2) {
    const cardinal = Math.pow(10, 10);
    return Math.round((num1 + num2) * cardinal) / cardinal;
}
simon committed
10 11
VantComponent({
    field: true,
simon committed
12
    classes: ['input-class', 'plus-class', 'minus-class'],
simon committed
13 14 15 16
    props: {
        value: null,
        integer: Boolean,
        disabled: Boolean,
simon committed
17 18
        inputWidth: null,
        buttonSize: null,
simon committed
19 20
        asyncChange: Boolean,
        disableInput: Boolean,
simon committed
21 22 23 24
        decimalLength: {
            type: Number,
            value: null
        },
simon committed
25 26 27 28 29 30 31 32 33 34 35 36
        min: {
            type: null,
            value: 1
        },
        max: {
            type: null,
            value: Number.MAX_SAFE_INTEGER
        },
        step: {
            type: null,
            value: 1
        },
simon committed
37 38 39 40 41 42 43
        showPlus: {
            type: Boolean,
            value: true
        },
        showMinus: {
            type: Boolean,
            value: true
simon committed
44 45 46 47 48 49 50 51
        }
    },
    watch: {
        value(value) {
            if (value === '') {
                return;
            }
            const newValue = this.range(value);
simon committed
52 53
            if (typeof newValue === 'number' && +this.data.value !== newValue) {
                this.setData({ value: newValue });
simon committed
54
            }
simon committed
55 56 57 58 59 60 61 62 63 64 65
        },
        inputWidth() {
            this.set({
                inputStyle: this.computeInputStyle()
            });
        },
        buttonSize() {
            this.set({
                inputStyle: this.computeInputStyle(),
                buttonStyle: this.computeButtonStyle()
            });
simon committed
66 67 68
        }
    },
    data: {
simon committed
69 70 71
        focus: false,
        inputStyle: '',
        buttonStyle: ''
simon committed
72 73
    },
    created() {
simon committed
74
        this.setData({
simon committed
75 76 77 78
            value: this.range(this.data.value)
        });
    },
    methods: {
simon committed
79 80 81 82 83 84
        isDisabled(type) {
            if (type === 'plus') {
                return this.data.disabled || this.data.value >= this.data.max;
            }
            return this.data.disabled || this.data.value <= this.data.min;
        },
simon committed
85 86 87 88 89 90 91 92 93 94
        onFocus(event) {
            this.$emit('focus', event.detail);
        },
        onBlur(event) {
            const value = this.range(this.data.value);
            this.triggerInput(value);
            this.$emit('blur', event.detail);
        },
        // limit value range
        range(value) {
simon committed
95 96 97 98 99 100 101 102 103
            value = String(value).replace(/[^0-9.-]/g, '');
            // format range
            value = value === '' ? 0 : +value;
            value = Math.max(Math.min(this.data.max, value), this.data.min);
            // format decimal
            if (isDef(this.data.decimalLength)) {
                value = value.toFixed(this.data.decimalLength);
            }
            return value;
simon committed
104 105 106 107 108
        },
        onInput(event) {
            const { value = '' } = event.detail || {};
            this.triggerInput(value);
        },
simon committed
109 110 111
        onChange() {
            const { type } = this;
            if (this.isDisabled(type)) {
simon committed
112 113 114 115
                this.$emit('overlimit', type);
                return;
            }
            const diff = type === 'minus' ? -this.data.step : +this.data.step;
simon committed
116
            const value = add(+this.data.value, diff);
simon committed
117 118 119
            this.triggerInput(this.range(value));
            this.$emit(type);
        },
simon committed
120 121 122 123 124
        longPressStep() {
            this.longPressTimer = setTimeout(() => {
                this.onChange();
                this.longPressStep();
            }, LONG_PRESS_INTERVAL);
simon committed
125
        },
simon committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        onTap(event) {
            const { type } = event.currentTarget.dataset;
            this.type = type;
            this.onChange();
        },
        onTouchStart(event) {
            clearTimeout(this.longPressTimer);
            const { type } = event.currentTarget.dataset;
            this.type = type;
            this.isLongPress = false;
            this.longPressTimer = setTimeout(() => {
                this.isLongPress = true;
                this.onChange();
                this.longPressStep();
            }, LONG_PRESS_START_TIME);
        },
        onTouchEnd() {
            clearTimeout(this.longPressTimer);
simon committed
144 145
        },
        triggerInput(value) {
simon committed
146
            this.setData({
simon committed
147 148 149
                value: this.data.asyncChange ? this.data.value : value
            });
            this.$emit('change', value);
simon committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        },
        computeInputStyle() {
            let style = '';
            if (this.data.inputWidth) {
                style = `width: ${addUnit(this.data.inputWidth)};`;
            }
            if (this.data.buttonSize) {
                style += `height: ${addUnit(this.data.buttonSize)};`;
            }
            return style;
        },
        computeButtonStyle() {
            let style = '';
            const size = addUnit(this.data.buttonSize);
            if (this.data.buttonSize) {
                style = `width: ${size};height: ${size};`;
            }
            return style;
simon committed
168 169 170
        }
    }
});