index.js 6.5 KB

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. (0, 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. alwaysEmbed: Boolean,
  78. },
  79. data: {
  80. currentValue: '',
  81. },
  82. created: function () {
  83. this.setData({
  84. currentValue: this.format(this.data.value),
  85. });
  86. },
  87. methods: {
  88. observeValue: function () {
  89. var _a = this.data, value = _a.value, currentValue = _a.currentValue;
  90. if (!equal(value, currentValue)) {
  91. this.setData({ currentValue: this.format(value) });
  92. }
  93. },
  94. check: function () {
  95. var val = this.format(this.data.currentValue);
  96. if (!equal(val, this.data.currentValue)) {
  97. this.setData({ currentValue: val });
  98. }
  99. },
  100. isDisabled: function (type) {
  101. var _a = this.data, disabled = _a.disabled, disablePlus = _a.disablePlus, disableMinus = _a.disableMinus, currentValue = _a.currentValue, max = _a.max, min = _a.min;
  102. if (type === 'plus') {
  103. return disabled || disablePlus || currentValue >= max;
  104. }
  105. return disabled || disableMinus || currentValue <= min;
  106. },
  107. onFocus: function (event) {
  108. this.$emit('focus', event.detail);
  109. },
  110. onBlur: function (event) {
  111. var value = this.format(event.detail.value);
  112. this.emitChange(value);
  113. this.$emit('blur', __assign(__assign({}, event.detail), { value: value }));
  114. },
  115. // filter illegal characters
  116. filter: function (value) {
  117. value = String(value).replace(/[^0-9.-]/g, '');
  118. if (this.data.integer && value.indexOf('.') !== -1) {
  119. value = value.split('.')[0];
  120. }
  121. return value;
  122. },
  123. // limit value range
  124. format: function (value) {
  125. value = this.filter(value);
  126. // format range
  127. value = value === '' ? 0 : +value;
  128. value = Math.max(Math.min(this.data.max, value), this.data.min);
  129. // format decimal
  130. if ((0, validator_1.isDef)(this.data.decimalLength)) {
  131. value = value.toFixed(this.data.decimalLength);
  132. }
  133. return value;
  134. },
  135. onInput: function (event) {
  136. var _a = (event.detail || {}).value, value = _a === void 0 ? '' : _a;
  137. // allow input to be empty
  138. if (value === '') {
  139. return;
  140. }
  141. var formatted = this.filter(value);
  142. // limit max decimal length
  143. if ((0, validator_1.isDef)(this.data.decimalLength) && formatted.indexOf('.') !== -1) {
  144. var pair = formatted.split('.');
  145. formatted = "".concat(pair[0], ".").concat(pair[1].slice(0, this.data.decimalLength));
  146. }
  147. this.emitChange(formatted);
  148. },
  149. emitChange: function (value) {
  150. if (!this.data.asyncChange) {
  151. this.setData({ currentValue: value });
  152. }
  153. this.$emit('change', value);
  154. },
  155. onChange: function () {
  156. var type = this.type;
  157. if (this.isDisabled(type)) {
  158. this.$emit('overlimit', type);
  159. return;
  160. }
  161. var diff = type === 'minus' ? -this.data.step : +this.data.step;
  162. var value = this.format(add(+this.data.currentValue, diff));
  163. this.emitChange(value);
  164. this.$emit(type);
  165. },
  166. longPressStep: function () {
  167. var _this = this;
  168. this.longPressTimer = setTimeout(function () {
  169. _this.onChange();
  170. _this.longPressStep();
  171. }, LONG_PRESS_INTERVAL);
  172. },
  173. onTap: function (event) {
  174. var type = event.currentTarget.dataset.type;
  175. this.type = type;
  176. this.onChange();
  177. },
  178. onTouchStart: function (event) {
  179. var _this = this;
  180. if (!this.data.longPress) {
  181. return;
  182. }
  183. clearTimeout(this.longPressTimer);
  184. var type = event.currentTarget.dataset.type;
  185. this.type = type;
  186. this.isLongPress = false;
  187. this.longPressTimer = setTimeout(function () {
  188. _this.isLongPress = true;
  189. _this.onChange();
  190. _this.longPressStep();
  191. }, LONG_PRESS_START_TIME);
  192. },
  193. onTouchEnd: function () {
  194. if (!this.data.longPress) {
  195. return;
  196. }
  197. clearTimeout(this.longPressTimer);
  198. },
  199. },
  200. });