admin
2020-12-08 7f2d1daf6af6ecebad84d80de46a0e5024bbf5da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import fmtUnit from '../_util/fmtUnit';
Component({
  data: {
    opaReduce: 1,
    opaAdd: 1
  },
  props: {
    className: '',
    min: 0,
    max: 100000,
    disabled: false,
    value: 10,
    readOnly: false,
    showNumber: false,
    inputWidth: fmtUnit('36px'),
    step: 1,
    onChange: function onChange() {},
    controlled: true,
    enableNative: undefined // false 处理 fixed 定位后输入框内容闪动的问题
 
  },
  didMount: function didMount() {
    var _this$props = this.props,
        value = _this$props.value,
        min = _this$props.min,
        max = _this$props.max;
    this.setData({
      value: Math.min(Math.max(min, value), max)
    });
  },
  didUpdate: function didUpdate(preProps) {
    var _this$props2 = this.props,
        value = _this$props2.value,
        min = _this$props2.min,
        max = _this$props2.max;
 
    if (preProps.value !== value) {
      var newValue = Math.min(Math.max(min, value), max);
      this.setData({
        value: newValue
      });
      this.resetFn(newValue);
    }
  },
  methods: {
    changeFn: function changeFn(ev) {
      var _this$props3 = this.props,
          min = _this$props3.min,
          max = _this$props3.max,
          onChange = _this$props3.onChange,
          disabled = _this$props3.disabled,
          step = _this$props3.step;
      var evType = ev.target.dataset.type;
      var _this$data = this.data,
          opaReduce = _this$data.opaReduce,
          opaAdd = _this$data.opaAdd,
          value = _this$data.value;
 
      if (!disabled) {
        if (evType === 'reduce') {
          if (value > min) {
            opaAdd = 1;
            value = Math.max(min, this.getCalculateValue('reduce', +value, +step));
            opaReduce = value === min ? 0.4 : 1;
          }
        } else {
          /* eslint-disable no-lonely-if */
          if (value < max) {
            opaReduce = 1;
            value = Math.min(this.getCalculateValue('add', +value, +step), max);
            opaAdd = value === max ? 0.4 : 1;
          }
        }
 
        this.setData({
          value: value,
          opaAdd: opaAdd,
          opaReduce: opaReduce
        });
        onChange(value, 'click');
      }
    },
    onInput: function onInput(e) {
      var max = this.props.max;
      var value = e.detail.value;
 
      if (value >= max) {
        e.detail.value = max;
        this.setData({
          value: max
        });
      }
 
      this.resetFn(Number(value), 'input');
    },
    onBlur: function onBlur(event) {
      var value = event.detail.value;
      var max = this.props.max;
 
      if (value >= max) {
        event.detail.value = max;
        this.setData({
          value: max
        });
      }
 
      this.resetFn(Number(value), 'input');
    },
    resetFn: function resetFn(value, mode) {
      var _this$props4 = this.props,
          max = _this$props4.max,
          min = _this$props4.min,
          onChange = _this$props4.onChange;
      var calculatedVal = value;
      var opaAdd = 1;
      var opaReduce = 1;
 
      if (value >= max) {
        calculatedVal = max;
        opaAdd = 0.4;
      } else if (value <= min) {
        calculatedVal = min;
        opaReduce = 0.4;
      }
 
      this.setData({
        value: calculatedVal,
        opaAdd: opaAdd,
        opaReduce: opaReduce
      });
      onChange(calculatedVal, mode);
    },
    getCalculateValue: function getCalculateValue(type, arg1, arg2) {
      var numFloat = arg1.toString().split('.')[1] || '';
      var num2Float = arg2.toString().split('.')[1] || '';
      var length = Math.max(numFloat.length, num2Float.length);
      var times = Math.pow(10, length);
      return type === 'reduce' ? ((+arg1 * times - +arg2 * times) / times).toFixed(length) : ((+arg1 * times + +arg2 * times) / times).toFixed(length);
    }
  }
});