index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. },
  33. integer: {
  34. type: Boolean,
  35. observer: 'check',
  36. },
  37. disabled: Boolean,
  38. inputWidth: String,
  39. buttonSize: String,
  40. asyncChange: Boolean,
  41. disableInput: Boolean,
  42. decimalLength: {
  43. type: Number,
  44. value: null,
  45. observer: 'check',
  46. },
  47. min: {
  48. type: null,
  49. value: 1,
  50. observer: 'check',
  51. },
  52. max: {
  53. type: null,
  54. value: Number.MAX_SAFE_INTEGER,
  55. observer: 'check',
  56. },
  57. step: {
  58. type: null,
  59. value: 1,
  60. },
  61. showPlus: {
  62. type: Boolean,
  63. value: true,
  64. },
  65. showMinus: {
  66. type: Boolean,
  67. value: true,
  68. },
  69. disablePlus: Boolean,
  70. disableMinus: Boolean,
  71. longPress: {
  72. type: Boolean,
  73. value: true,
  74. },
  75. theme: String,
  76. alwaysEmbed: Boolean,
  77. },
  78. data: {
  79. currentValue: '',
  80. },
  81. watch: {
  82. value: function () {
  83. this.observeValue();
  84. },
  85. },
  86. created: function () {
  87. this.setData({
  88. currentValue: this.format(this.data.value),
  89. });
  90. },
  91. methods: {
  92. observeValue: function () {
  93. var value = this.data.value;
  94. this.setData({ currentValue: this.format(value) });
  95. },
  96. check: function () {
  97. var val = this.format(this.data.currentValue);
  98. if (!equal(val, this.data.currentValue)) {
  99. this.setData({ currentValue: val });
  100. }
  101. },
  102. isDisabled: function (type) {
  103. var _a = this.data, disabled = _a.disabled, disablePlus = _a.disablePlus, disableMinus = _a.disableMinus, currentValue = _a.currentValue, max = _a.max, min = _a.min;
  104. if (type === 'plus') {
  105. return disabled || disablePlus || +currentValue >= +max;
  106. }
  107. return disabled || disableMinus || +currentValue <= +min;
  108. },
  109. onFocus: function (event) {
  110. this.$emit('focus', event.detail);
  111. },
  112. onBlur: function (event) {
  113. var value = this.format(event.detail.value);
  114. this.setData({ currentValue: value });
  115. this.emitChange(value);
  116. this.$emit('blur', __assign(__assign({}, event.detail), { value: value }));
  117. },
  118. // filter illegal characters
  119. filter: function (value) {
  120. value = String(value).replace(/[^0-9.-]/g, '');
  121. if (this.data.integer && value.indexOf('.') !== -1) {
  122. value = value.split('.')[0];
  123. }
  124. return value;
  125. },
  126. // limit value range
  127. format: function (value) {
  128. value = this.filter(value);
  129. // format range
  130. value = value === '' ? 0 : +value;
  131. value = Math.max(Math.min(this.data.max, value), this.data.min);
  132. // format decimal
  133. if ((0, validator_1.isDef)(this.data.decimalLength)) {
  134. value = value.toFixed(this.data.decimalLength);
  135. }
  136. return value;
  137. },
  138. onInput: function (event) {
  139. var _a = (event.detail || {}).value, value = _a === void 0 ? '' : _a;
  140. // allow input to be empty
  141. if (value === '') {
  142. return;
  143. }
  144. var formatted = this.format(value);
  145. this.emitChange(formatted);
  146. },
  147. emitChange: function (value) {
  148. if (!this.data.asyncChange) {
  149. this.setData({ currentValue: value });
  150. }
  151. this.$emit('change', value);
  152. },
  153. onChange: function () {
  154. var type = this.type;
  155. if (this.isDisabled(type)) {
  156. this.$emit('overlimit', type);
  157. return;
  158. }
  159. var diff = type === 'minus' ? -this.data.step : +this.data.step;
  160. var value = this.format(add(+this.data.currentValue, diff));
  161. this.emitChange(value);
  162. this.$emit(type);
  163. },
  164. longPressStep: function () {
  165. var _this = this;
  166. this.longPressTimer = setTimeout(function () {
  167. _this.onChange();
  168. _this.longPressStep();
  169. }, LONG_PRESS_INTERVAL);
  170. },
  171. onTap: function (event) {
  172. var type = event.currentTarget.dataset.type;
  173. this.type = type;
  174. this.onChange();
  175. },
  176. onTouchStart: function (event) {
  177. var _this = this;
  178. if (!this.data.longPress) {
  179. return;
  180. }
  181. clearTimeout(this.longPressTimer);
  182. var type = event.currentTarget.dataset.type;
  183. this.type = type;
  184. this.isLongPress = false;
  185. this.longPressTimer = setTimeout(function () {
  186. _this.isLongPress = true;
  187. _this.onChange();
  188. _this.longPressStep();
  189. }, LONG_PRESS_START_TIME);
  190. },
  191. onTouchEnd: function () {
  192. if (!this.data.longPress) {
  193. return;
  194. }
  195. clearTimeout(this.longPressTimer);
  196. },
  197. },
  198. });