stepper.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. const { prefix } = config;
  11. const name = `${prefix}-stepper`;
  12. let Stepper = class Stepper extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.externalClasses = [`${prefix}-class`, `${prefix}-class-input`, `${prefix}-class-minus`, `${prefix}-class-plus`];
  16. this.properties = Object.assign({}, props);
  17. this.controlledProps = [
  18. {
  19. key: 'value',
  20. event: 'change',
  21. },
  22. ];
  23. this.observers = {
  24. value(v) {
  25. this.preValue = Number(v);
  26. this.setData({
  27. currentValue: this.format(Number(v)),
  28. });
  29. },
  30. };
  31. this.data = {
  32. currentValue: 0,
  33. classPrefix: name,
  34. prefix,
  35. };
  36. this.lifetimes = {
  37. attached() {
  38. const { value, min } = this.properties;
  39. this.setData({
  40. currentValue: value ? Number(value) : min,
  41. });
  42. },
  43. };
  44. this.methods = {
  45. isDisabled(type) {
  46. const { min, max, disabled } = this.properties;
  47. const { currentValue } = this.data;
  48. if (disabled) {
  49. return true;
  50. }
  51. if (type === 'minus' && currentValue <= min) {
  52. return true;
  53. }
  54. if (type === 'plus' && currentValue >= max) {
  55. return true;
  56. }
  57. return false;
  58. },
  59. getLen(num) {
  60. const numStr = num.toString();
  61. return numStr.indexOf('.') === -1 ? 0 : numStr.split('.')[1].length;
  62. },
  63. add(a, b) {
  64. const maxLen = Math.max(this.getLen(a), this.getLen(b));
  65. const base = Math.pow(10, maxLen);
  66. return Math.round(a * base + b * base) / base;
  67. },
  68. format(value) {
  69. const { min, max, step } = this.properties;
  70. const len = Math.max(this.getLen(step), this.getLen(value));
  71. return Math.max(Math.min(max, value, Number.MAX_SAFE_INTEGER), min, Number.MIN_SAFE_INTEGER).toFixed(len);
  72. },
  73. setValue(value) {
  74. value = this.format(value);
  75. if (this.preValue === value)
  76. return;
  77. this.preValue = value;
  78. this._trigger('change', { value: Number(value) });
  79. },
  80. minusValue() {
  81. if (this.isDisabled('minus')) {
  82. this.triggerEvent('overlimit', { type: 'minus' });
  83. return false;
  84. }
  85. const { currentValue, step } = this.data;
  86. this.setValue(this.add(currentValue, -step));
  87. },
  88. plusValue() {
  89. if (this.isDisabled('plus')) {
  90. this.triggerEvent('overlimit', { type: 'plus' });
  91. return false;
  92. }
  93. const { currentValue, step } = this.data;
  94. this.setValue(this.add(currentValue, step));
  95. },
  96. filterIllegalChar(value) {
  97. const v = String(value).replace(/[^0-9.]/g, '');
  98. const indexOfDot = v.indexOf('.');
  99. if (this.properties.integer && indexOfDot !== -1) {
  100. return v.split('.')[0];
  101. }
  102. if (!this.properties.integer && indexOfDot !== -1 && indexOfDot !== v.lastIndexOf('.')) {
  103. return v.split('.', 2).join('.');
  104. }
  105. return v;
  106. },
  107. handleFocus(e) {
  108. const { value } = e.detail;
  109. this.triggerEvent('focus', { value });
  110. },
  111. handleInput(e) {
  112. const { value } = e.detail;
  113. if (value === '') {
  114. return;
  115. }
  116. const formatted = this.filterIllegalChar(value);
  117. this.setData({
  118. currentValue: formatted,
  119. });
  120. this.triggerEvent('input', { value: formatted });
  121. },
  122. handleBlur(e) {
  123. const { value: rawValue } = e.detail;
  124. const value = this.format(rawValue);
  125. this.setValue(value);
  126. this.triggerEvent('blur', { value });
  127. },
  128. };
  129. }
  130. };
  131. Stepper = __decorate([
  132. wxComponent()
  133. ], Stepper);
  134. export default Stepper;