index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. disablePlus: Boolean,
  46. disableMinus: Boolean
  47. },
  48. watch: {
  49. value(value) {
  50. if (value === '') {
  51. return;
  52. }
  53. const newValue = this.range(value);
  54. if (typeof newValue === 'number' && +this.data.value !== newValue) {
  55. this.setData({ value: newValue });
  56. }
  57. },
  58. inputWidth() {
  59. this.set({
  60. inputStyle: this.computeInputStyle()
  61. });
  62. },
  63. buttonSize() {
  64. this.set({
  65. inputStyle: this.computeInputStyle(),
  66. buttonStyle: this.computeButtonStyle()
  67. });
  68. }
  69. },
  70. data: {
  71. focus: false,
  72. inputStyle: '',
  73. buttonStyle: ''
  74. },
  75. created() {
  76. this.setData({
  77. value: this.range(this.data.value)
  78. });
  79. },
  80. methods: {
  81. isDisabled(type) {
  82. if (type === 'plus') {
  83. return this.data.disabled || this.data.disablePlus || this.data.value >= this.data.max;
  84. }
  85. return this.data.disabled || this.data.disableMinus || this.data.value <= this.data.min;
  86. },
  87. onFocus(event) {
  88. this.$emit('focus', event.detail);
  89. },
  90. onBlur(event) {
  91. const value = this.range(this.data.value);
  92. this.triggerInput(value);
  93. this.$emit('blur', event.detail);
  94. },
  95. // limit value range
  96. range(value) {
  97. value = String(value).replace(/[^0-9.-]/g, '');
  98. // format range
  99. value = value === '' ? 0 : +value;
  100. value = Math.max(Math.min(this.data.max, value), this.data.min);
  101. // format decimal
  102. if (isDef(this.data.decimalLength)) {
  103. value = value.toFixed(this.data.decimalLength);
  104. }
  105. return value;
  106. },
  107. onInput(event) {
  108. const { value = '' } = event.detail || {};
  109. this.triggerInput(value);
  110. },
  111. onChange() {
  112. const { type } = this;
  113. if (this.isDisabled(type)) {
  114. this.$emit('overlimit', type);
  115. return;
  116. }
  117. const diff = type === 'minus' ? -this.data.step : +this.data.step;
  118. const value = add(+this.data.value, diff);
  119. this.triggerInput(this.range(value));
  120. this.$emit(type);
  121. },
  122. longPressStep() {
  123. this.longPressTimer = setTimeout(() => {
  124. this.onChange();
  125. this.longPressStep();
  126. }, LONG_PRESS_INTERVAL);
  127. },
  128. onTap(event) {
  129. const { type } = event.currentTarget.dataset;
  130. this.type = type;
  131. this.onChange();
  132. },
  133. onTouchStart(event) {
  134. clearTimeout(this.longPressTimer);
  135. const { type } = event.currentTarget.dataset;
  136. this.type = type;
  137. this.isLongPress = false;
  138. this.longPressTimer = setTimeout(() => {
  139. this.isLongPress = true;
  140. this.onChange();
  141. this.longPressStep();
  142. }, LONG_PRESS_START_TIME);
  143. },
  144. onTouchEnd() {
  145. clearTimeout(this.longPressTimer);
  146. },
  147. triggerInput(value) {
  148. this.setData({
  149. value: this.data.asyncChange ? this.data.value : value
  150. });
  151. this.$emit('change', value);
  152. },
  153. computeInputStyle() {
  154. let style = '';
  155. if (this.data.inputWidth) {
  156. style = `width: ${addUnit(this.data.inputWidth)};`;
  157. }
  158. if (this.data.buttonSize) {
  159. style += `height: ${addUnit(this.data.buttonSize)};`;
  160. }
  161. return style;
  162. },
  163. computeButtonStyle() {
  164. let style = '';
  165. const size = addUnit(this.data.buttonSize);
  166. if (this.data.buttonSize) {
  167. style = `width: ${size};height: ${size};`;
  168. }
  169. return style;
  170. }
  171. }
  172. });