index.js 5.5 KB

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