index.js 5.7 KB

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