index.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. var component_1 = require("../common/component");
  15. var validator_1 = require("../common/validator");
  16. var LONG_PRESS_START_TIME = 600;
  17. var LONG_PRESS_INTERVAL = 200;
  18. // add num and avoid float number
  19. function add(num1, num2) {
  20. var cardinal = Math.pow(10, 10);
  21. return Math.round((num1 + num2) * cardinal) / cardinal;
  22. }
  23. function equal(value1, value2) {
  24. return String(value1) === String(value2);
  25. }
  26. component_1.VantComponent({
  27. field: true,
  28. classes: ['input-class', 'plus-class', 'minus-class'],
  29. props: {
  30. value: {
  31. type: null,
  32. observer: 'observeValue',
  33. },
  34. integer: {
  35. type: Boolean,
  36. observer: 'check',
  37. },
  38. disabled: Boolean,
  39. inputWidth: String,
  40. buttonSize: String,
  41. asyncChange: Boolean,
  42. disableInput: Boolean,
  43. decimalLength: {
  44. type: Number,
  45. value: null,
  46. observer: 'check',
  47. },
  48. min: {
  49. type: null,
  50. value: 1,
  51. observer: 'check',
  52. },
  53. max: {
  54. type: null,
  55. value: Number.MAX_SAFE_INTEGER,
  56. observer: 'check',
  57. },
  58. step: {
  59. type: null,
  60. value: 1,
  61. },
  62. showPlus: {
  63. type: Boolean,
  64. value: true,
  65. },
  66. showMinus: {
  67. type: Boolean,
  68. value: true,
  69. },
  70. disablePlus: Boolean,
  71. disableMinus: Boolean,
  72. longPress: {
  73. type: Boolean,
  74. value: true,
  75. },
  76. theme: String,
  77. },
  78. data: {
  79. currentValue: '',
  80. },
  81. created: function () {
  82. this.setData({
  83. currentValue: this.format(this.data.value),
  84. });
  85. },
  86. methods: {
  87. observeValue: function () {
  88. var _a = this.data, value = _a.value, currentValue = _a.currentValue;
  89. if (!equal(value, currentValue)) {
  90. this.setData({ currentValue: this.format(value) });
  91. }
  92. },
  93. check: function () {
  94. var val = this.format(this.data.currentValue);
  95. if (!equal(val, this.data.currentValue)) {
  96. this.setData({ currentValue: val });
  97. }
  98. },
  99. isDisabled: function (type) {
  100. var _a = this.data, disabled = _a.disabled, disablePlus = _a.disablePlus, disableMinus = _a.disableMinus, currentValue = _a.currentValue, max = _a.max, min = _a.min;
  101. if (type === 'plus') {
  102. return disabled || disablePlus || currentValue >= max;
  103. }
  104. return disabled || disableMinus || currentValue <= min;
  105. },
  106. onFocus: function (event) {
  107. this.$emit('focus', event.detail);
  108. },
  109. onBlur: function (event) {
  110. var value = this.format(event.detail.value);
  111. this.emitChange(value);
  112. this.$emit('blur', __assign(__assign({}, event.detail), { value: value }));
  113. },
  114. // filter illegal characters
  115. filter: function (value) {
  116. value = String(value).replace(/[^0-9.-]/g, '');
  117. if (this.data.integer && value.indexOf('.') !== -1) {
  118. value = value.split('.')[0];
  119. }
  120. return value;
  121. },
  122. // limit value range
  123. format: function (value) {
  124. value = this.filter(value);
  125. // format range
  126. value = value === '' ? 0 : +value;
  127. value = Math.max(Math.min(this.data.max, value), this.data.min);
  128. // format decimal
  129. if (validator_1.isDef(this.data.decimalLength)) {
  130. value = value.toFixed(this.data.decimalLength);
  131. }
  132. return value;
  133. },
  134. onInput: function (event) {
  135. var _a = (event.detail || {}).value, value = _a === void 0 ? '' : _a;
  136. // allow input to be empty
  137. if (value === '') {
  138. return;
  139. }
  140. var formatted = this.filter(value);
  141. // limit max decimal length
  142. if (validator_1.isDef(this.data.decimalLength) && formatted.indexOf('.') !== -1) {
  143. var pair = formatted.split('.');
  144. formatted = pair[0] + "." + pair[1].slice(0, this.data.decimalLength);
  145. }
  146. this.emitChange(formatted);
  147. },
  148. emitChange: function (value) {
  149. if (!this.data.asyncChange) {
  150. this.setData({ currentValue: value });
  151. }
  152. this.$emit('change', value);
  153. },
  154. onChange: function () {
  155. var type = this.type;
  156. if (this.isDisabled(type)) {
  157. this.$emit('overlimit', type);
  158. return;
  159. }
  160. var diff = type === 'minus' ? -this.data.step : +this.data.step;
  161. var value = this.format(add(+this.data.currentValue, diff));
  162. this.emitChange(value);
  163. this.$emit(type);
  164. },
  165. longPressStep: function () {
  166. var _this = this;
  167. this.longPressTimer = setTimeout(function () {
  168. _this.onChange();
  169. _this.longPressStep();
  170. }, LONG_PRESS_INTERVAL);
  171. },
  172. onTap: function (event) {
  173. var type = event.currentTarget.dataset.type;
  174. this.type = type;
  175. this.onChange();
  176. },
  177. onTouchStart: function (event) {
  178. var _this = this;
  179. if (!this.data.longPress) {
  180. return;
  181. }
  182. clearTimeout(this.longPressTimer);
  183. var type = event.currentTarget.dataset.type;
  184. this.type = type;
  185. this.isLongPress = false;
  186. this.longPressTimer = setTimeout(function () {
  187. _this.isLongPress = true;
  188. _this.onChange();
  189. _this.longPressStep();
  190. }, LONG_PRESS_START_TIME);
  191. },
  192. onTouchEnd: function () {
  193. if (!this.data.longPress) {
  194. return;
  195. }
  196. clearTimeout(this.longPressTimer);
  197. },
  198. },
  199. });