合伙人运营小程序
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

stepper.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. handleFocus(e) {
  46. const { value } = e.detail;
  47. this.triggerEvent('focus', { value });
  48. },
  49. handleInput(e) {
  50. const { value } = e.detail;
  51. if (value === '') {
  52. return;
  53. }
  54. this.triggerEvent('input', { value });
  55. },
  56. handleBlur(e) {
  57. const { value: rawValue } = e.detail;
  58. const value = this.format(rawValue);
  59. this.setValue(value);
  60. this.triggerEvent('blur', { value });
  61. },
  62. };
  63. }
  64. isDisabled(type) {
  65. const { min, max, disabled } = this.properties;
  66. const { currentValue } = this.data;
  67. if (disabled) {
  68. return true;
  69. }
  70. if (type === 'minus' && currentValue <= min) {
  71. return true;
  72. }
  73. if (type === 'plus' && currentValue >= max) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. getLen(num) {
  79. const numStr = num.toString();
  80. return numStr.indexOf('.') === -1 ? 0 : numStr.split('.')[1].length;
  81. }
  82. add(a, b) {
  83. const maxLen = Math.max(this.getLen(a), this.getLen(b));
  84. const base = Math.pow(10, maxLen);
  85. return Math.round(a * base + b * base) / base;
  86. }
  87. format(value) {
  88. const { min, max, step } = this.properties;
  89. const len = Math.max(this.getLen(step), this.getLen(value));
  90. return Math.max(Math.min(max, value, Number.MAX_SAFE_INTEGER), min, Number.MIN_SAFE_INTEGER).toFixed(len);
  91. }
  92. setValue(value) {
  93. value = this.format(value);
  94. if (this.preValue === value)
  95. return;
  96. this.preValue = value;
  97. this._trigger('change', { value: Number(value) });
  98. }
  99. minusValue() {
  100. if (this.isDisabled('minus')) {
  101. this.triggerEvent('overlimit', { type: 'minus' });
  102. return false;
  103. }
  104. const { currentValue, step } = this.data;
  105. this.setValue(this.add(currentValue, -step));
  106. }
  107. plusValue() {
  108. if (this.isDisabled('plus')) {
  109. this.triggerEvent('overlimit', { type: 'plus' });
  110. return false;
  111. }
  112. const { currentValue, step } = this.data;
  113. this.setValue(this.add(currentValue, step));
  114. }
  115. };
  116. Stepper = __decorate([
  117. wxComponent()
  118. ], Stepper);
  119. export default Stepper;