index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var component_1 = require("../common/component");
  4. component_1.VantComponent({
  5. field: true,
  6. classes: [
  7. 'input-class',
  8. 'plus-class',
  9. 'minus-class'
  10. ],
  11. props: {
  12. value: null,
  13. integer: Boolean,
  14. disabled: Boolean,
  15. inputWidth: String,
  16. asyncChange: Boolean,
  17. disableInput: Boolean,
  18. min: {
  19. type: null,
  20. value: 1
  21. },
  22. max: {
  23. type: null,
  24. value: Number.MAX_SAFE_INTEGER
  25. },
  26. step: {
  27. type: null,
  28. value: 1
  29. },
  30. showPlus: {
  31. type: Boolean,
  32. value: true
  33. },
  34. showMinus: {
  35. type: Boolean,
  36. value: true
  37. }
  38. },
  39. computed: {
  40. minusDisabled: function () {
  41. return this.data.disabled || this.data.value <= this.data.min;
  42. },
  43. plusDisabled: function () {
  44. return this.data.disabled || this.data.value >= this.data.max;
  45. }
  46. },
  47. watch: {
  48. value: function (value) {
  49. if (value === '') {
  50. return;
  51. }
  52. var newValue = this.range(value);
  53. if (typeof newValue === 'number' && +this.data.value !== newValue) {
  54. this.set({ value: newValue });
  55. }
  56. }
  57. },
  58. data: {
  59. focus: false
  60. },
  61. created: function () {
  62. this.set({
  63. value: this.range(this.data.value)
  64. });
  65. },
  66. methods: {
  67. onFocus: function (event) {
  68. this.$emit('focus', event.detail);
  69. },
  70. onBlur: function (event) {
  71. var value = this.range(this.data.value);
  72. this.triggerInput(value);
  73. this.$emit('blur', event.detail);
  74. },
  75. // limit value range
  76. range: function (value) {
  77. value = String(value).replace(/[^0-9.-]/g, '');
  78. return Math.max(Math.min(this.data.max, value), this.data.min);
  79. },
  80. onInput: function (event) {
  81. var _a = (event.detail || {}).value, value = _a === void 0 ? '' : _a;
  82. this.triggerInput(value);
  83. },
  84. onChange: function (type) {
  85. if (this.data[type + "Disabled"]) {
  86. this.$emit('overlimit', type);
  87. return;
  88. }
  89. var diff = type === 'minus' ? -this.data.step : +this.data.step;
  90. var value = Math.round((+this.data.value + diff) * 100) / 100;
  91. this.triggerInput(this.range(value));
  92. this.$emit(type);
  93. },
  94. onMinus: function () {
  95. this.onChange('minus');
  96. },
  97. onPlus: function () {
  98. this.onChange('plus');
  99. },
  100. triggerInput: function (value) {
  101. this.set({
  102. value: this.data.asyncChange ? this.data.value : value
  103. });
  104. this.$emit('change', value);
  105. }
  106. }
  107. });