index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. },
  31. computed: {
  32. minusDisabled: function () {
  33. return this.data.disabled || this.data.value <= this.data.min;
  34. },
  35. plusDisabled: function () {
  36. return this.data.disabled || this.data.value >= this.data.max;
  37. }
  38. },
  39. watch: {
  40. value: function (value) {
  41. if (value === '') {
  42. return;
  43. }
  44. var newValue = this.range(value);
  45. if (typeof newValue === 'number' && +this.data.value !== newValue) {
  46. this.set({ value: newValue });
  47. }
  48. }
  49. },
  50. data: {
  51. focus: false
  52. },
  53. created: function () {
  54. this.set({
  55. value: this.range(this.data.value)
  56. });
  57. },
  58. methods: {
  59. onFocus: function (event) {
  60. this.$emit('focus', event.detail);
  61. },
  62. onBlur: function (event) {
  63. var value = this.range(this.data.value);
  64. this.triggerInput(value);
  65. this.$emit('blur', event.detail);
  66. },
  67. // limit value range
  68. range: function (value) {
  69. value = String(value).replace(/[^0-9.-]/g, '');
  70. return Math.max(Math.min(this.data.max, value), this.data.min);
  71. },
  72. onInput: function (event) {
  73. var _a = (event.detail || {}).value, value = _a === void 0 ? '' : _a;
  74. this.triggerInput(value);
  75. },
  76. onChange: function (type) {
  77. if (this.data[type + "Disabled"]) {
  78. this.$emit('overlimit', type);
  79. return;
  80. }
  81. var diff = type === 'minus' ? -this.data.step : +this.data.step;
  82. var value = Math.round((this.data.value + diff) * 100) / 100;
  83. this.triggerInput(this.range(value));
  84. this.$emit(type);
  85. },
  86. onMinus: function () {
  87. this.onChange('minus');
  88. },
  89. onPlus: function () {
  90. this.onChange('plus');
  91. },
  92. triggerInput: function (value) {
  93. this.set({
  94. value: this.data.asyncChange ? this.data.value : value
  95. });
  96. this.$emit('change', value);
  97. }
  98. }
  99. });