index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { VantComponent } from '../common/component'; // Note that the bitwise operators and shift operators operate on 32-bit ints
  2. // so in that case, the max safe integer is 2^31-1, or 2147483647
  3. var MAX = 2147483647;
  4. VantComponent({
  5. field: true,
  6. classes: ['input-class', 'plus-class', 'minus-class'],
  7. props: {
  8. value: null,
  9. integer: Boolean,
  10. disabled: Boolean,
  11. asyncChange: Boolean,
  12. disableInput: Boolean,
  13. min: {
  14. type: null,
  15. value: 1
  16. },
  17. max: {
  18. type: null,
  19. value: MAX
  20. },
  21. step: {
  22. type: null,
  23. value: 1
  24. }
  25. },
  26. computed: {
  27. minusDisabled: function minusDisabled() {
  28. return this.data.disabled || this.data.value <= this.data.min;
  29. },
  30. plusDisabled: function plusDisabled() {
  31. return this.data.disabled || this.data.value >= this.data.max;
  32. }
  33. },
  34. watch: {
  35. value: function value(_value) {
  36. if (_value !== '') {
  37. this.set({
  38. value: this.range(_value)
  39. });
  40. }
  41. }
  42. },
  43. data: {
  44. focus: false
  45. },
  46. created: function created() {
  47. this.set({
  48. value: this.range(this.data.value)
  49. });
  50. },
  51. methods: {
  52. onFocus: function onFocus() {
  53. this.setData({
  54. focus: true
  55. });
  56. },
  57. // limit value range
  58. range: function range(value) {
  59. return Math.max(Math.min(this.data.max, value), this.data.min);
  60. },
  61. onInput: function onInput(event) {
  62. var _ref = event.detail || {},
  63. _ref$value = _ref.value,
  64. value = _ref$value === void 0 ? '' : _ref$value;
  65. this.triggerInput(value);
  66. },
  67. onChange: function onChange(type) {
  68. if (this.data[type + "Disabled"]) {
  69. this.$emit('overlimit', type);
  70. return;
  71. }
  72. var diff = type === 'minus' ? -this.data.step : +this.data.step;
  73. var value = Math.round((this.data.value + diff) * 100) / 100;
  74. this.triggerInput(this.range(value));
  75. this.$emit(type);
  76. },
  77. onBlur: function onBlur(event) {
  78. var value = this.range(this.data.value);
  79. this.triggerInput(value);
  80. this.$emit('blur', event);
  81. },
  82. onMinus: function onMinus() {
  83. this.onChange('minus');
  84. },
  85. onPlus: function onPlus() {
  86. this.onChange('plus');
  87. },
  88. triggerInput: function triggerInput(value) {
  89. this.set({
  90. value: this.data.asyncChange ? this.data.value : value
  91. });
  92. this.$emit('change', value);
  93. }
  94. }
  95. });