index.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { Component, triggerEvent, getValueFromProps } from '../_util/simply';
  2. import { StepperDefaultProps } from './props';
  3. import { getPrecision, getValidNumber } from './utils';
  4. import mixinValue from '../mixins/value';
  5. Component(StepperDefaultProps, {
  6. onFocus: function (e) {
  7. var value = this.getValue();
  8. triggerEvent(this, 'focus', value === '' ? null : Number(value), e);
  9. },
  10. onChange: function (val, e) {
  11. var _a = this.update(val), needUpdate = _a.needUpdate, value = _a.value;
  12. if (getValueFromProps(this, 'onChange') && needUpdate) {
  13. triggerEvent(this, 'change', value === '' ? null : Number(value), e);
  14. }
  15. },
  16. onConfirm: function (val, e) {
  17. var value = this.getValue();
  18. triggerEvent(this, 'confirm', value === '' ? null : Number(value), e);
  19. },
  20. onBlur: function (e) {
  21. if (this.isControlled()) {
  22. this.update(getValueFromProps(this, 'value'));
  23. }
  24. var value = this.getValue();
  25. triggerEvent(this, 'blur', value === '' ? null : Number(value), e);
  26. },
  27. onTap: function (e) {
  28. var _a = getValueFromProps(this, [
  29. 'step',
  30. 'disabled',
  31. 'min',
  32. 'max',
  33. 'precision',
  34. ]), step = _a[0], disabled = _a[1], _b = _a[2], min = _b === void 0 ? -Infinity : _b, _c = _a[3], max = _c === void 0 ? Infinity : _c, precisionFormProps = _a[4];
  35. var value = Number(this.getValue() || 0);
  36. if (!disabled) {
  37. var mode = e.currentTarget.dataset.mode;
  38. var result = value;
  39. var precision = precisionFormProps >= 0
  40. ? precisionFormProps
  41. : Math.max(getPrecision(value), getPrecision(step));
  42. if (mode === 'minus') {
  43. // 【减】按钮的操作
  44. result = value - step;
  45. if (result < min) {
  46. result = min;
  47. }
  48. }
  49. else if (mode === 'add') {
  50. // 【加】按钮的操作
  51. result = value + step;
  52. if (result > max) {
  53. result = max;
  54. }
  55. }
  56. if (!this.isControlled()) {
  57. var needUpdate = this.update(result, {}, precision).needUpdate;
  58. if (!needUpdate) {
  59. return;
  60. }
  61. }
  62. {
  63. var value_1 = getValidNumber(result, min, max, step, precision).value;
  64. triggerEvent(this, 'change', Number(value_1), e);
  65. }
  66. }
  67. },
  68. }, undefined, [
  69. mixinValue({
  70. transformValue: function (num, extra, precision) {
  71. var _a = getValueFromProps(this, [
  72. 'min',
  73. 'max',
  74. 'step',
  75. 'precision',
  76. ]), min = _a[0], max = _a[1], step = _a[2], precisionFormProps = _a[3];
  77. var _b = getValidNumber(num, min, max, step, precision >= 0 ? precision : precisionFormProps), valid = _b.valid, value = _b.value;
  78. if (valid && this.getValue() !== value) {
  79. return {
  80. needUpdate: true,
  81. value: value,
  82. };
  83. }
  84. return {
  85. needUpdate: false,
  86. };
  87. },
  88. }),
  89. ]);