index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { VantComponent } from '../common/component';
  2. import { addUnit, isDef } from '../common/utils';
  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. VantComponent({
  11. field: true,
  12. classes: ['input-class', 'plus-class', 'minus-class'],
  13. props: {
  14. value: null,
  15. integer: Boolean,
  16. disabled: Boolean,
  17. inputWidth: null,
  18. buttonSize: null,
  19. asyncChange: Boolean,
  20. disableInput: Boolean,
  21. decimalLength: {
  22. type: Number,
  23. value: null
  24. },
  25. min: {
  26. type: null,
  27. value: 1
  28. },
  29. max: {
  30. type: null,
  31. value: Number.MAX_SAFE_INTEGER
  32. },
  33. step: {
  34. type: null,
  35. value: 1
  36. },
  37. showPlus: {
  38. type: Boolean,
  39. value: true
  40. },
  41. showMinus: {
  42. type: Boolean,
  43. value: true
  44. }
  45. },
  46. watch: {
  47. value(value) {
  48. if (value === '') {
  49. return;
  50. }
  51. const newValue = this.range(value);
  52. if (typeof newValue === 'number' && +this.data.value !== newValue) {
  53. this.setData({ value: newValue });
  54. }
  55. },
  56. inputWidth() {
  57. this.set({
  58. inputStyle: this.computeInputStyle()
  59. });
  60. },
  61. buttonSize() {
  62. this.set({
  63. inputStyle: this.computeInputStyle(),
  64. buttonStyle: this.computeButtonStyle()
  65. });
  66. }
  67. },
  68. data: {
  69. focus: false,
  70. inputStyle: '',
  71. buttonStyle: ''
  72. },
  73. created() {
  74. this.setData({
  75. value: this.range(this.data.value)
  76. });
  77. },
  78. methods: {
  79. isDisabled(type) {
  80. if (type === 'plus') {
  81. return this.data.disabled || this.data.value >= this.data.max;
  82. }
  83. return this.data.disabled || this.data.value <= this.data.min;
  84. },
  85. onFocus(event) {
  86. this.$emit('focus', event.detail);
  87. },
  88. onBlur(event) {
  89. const value = this.range(this.data.value);
  90. this.triggerInput(value);
  91. this.$emit('blur', event.detail);
  92. },
  93. // limit value range
  94. range(value) {
  95. value = String(value).replace(/[^0-9.-]/g, '');
  96. // format range
  97. value = value === '' ? 0 : +value;
  98. value = Math.max(Math.min(this.data.max, value), this.data.min);
  99. // format decimal
  100. if (isDef(this.data.decimalLength)) {
  101. value = value.toFixed(this.data.decimalLength);
  102. }
  103. return value;
  104. },
  105. onInput(event) {
  106. const { value = '' } = event.detail || {};
  107. this.triggerInput(value);
  108. },
  109. onChange() {
  110. const { type } = this;
  111. if (this.isDisabled(type)) {
  112. this.$emit('overlimit', type);
  113. return;
  114. }
  115. const diff = type === 'minus' ? -this.data.step : +this.data.step;
  116. const value = add(+this.data.value, diff);
  117. this.triggerInput(this.range(value));
  118. this.$emit(type);
  119. },
  120. longPressStep() {
  121. this.longPressTimer = setTimeout(() => {
  122. this.onChange();
  123. this.longPressStep();
  124. }, LONG_PRESS_INTERVAL);
  125. },
  126. onTap(event) {
  127. const { type } = event.currentTarget.dataset;
  128. this.type = type;
  129. this.onChange();
  130. },
  131. onTouchStart(event) {
  132. clearTimeout(this.longPressTimer);
  133. const { type } = event.currentTarget.dataset;
  134. this.type = type;
  135. this.isLongPress = false;
  136. this.longPressTimer = setTimeout(() => {
  137. this.isLongPress = true;
  138. this.onChange();
  139. this.longPressStep();
  140. }, LONG_PRESS_START_TIME);
  141. },
  142. onTouchEnd() {
  143. clearTimeout(this.longPressTimer);
  144. },
  145. triggerInput(value) {
  146. this.setData({
  147. value: this.data.asyncChange ? this.data.value : value
  148. });
  149. this.$emit('change', value);
  150. },
  151. computeInputStyle() {
  152. let style = '';
  153. if (this.data.inputWidth) {
  154. style = `width: ${addUnit(this.data.inputWidth)};`;
  155. }
  156. if (this.data.buttonSize) {
  157. style += `height: ${addUnit(this.data.buttonSize)};`;
  158. }
  159. return style;
  160. },
  161. computeButtonStyle() {
  162. let style = '';
  163. const size = addUnit(this.data.buttonSize);
  164. if (this.data.buttonSize) {
  165. style = `width: ${size};height: ${size};`;
  166. }
  167. return style;
  168. }
  169. }
  170. });