index.js 4.8 KB

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