value.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { __spreadArray } from "tslib";
  2. function equal(a, b) {
  3. if (a === b) {
  4. return true;
  5. }
  6. if (a !== a && b !== b) {
  7. return true;
  8. }
  9. return false;
  10. }
  11. var component2 = my.canIUse('component2');
  12. export default (function (_a) {
  13. var _b;
  14. var _c = _a === void 0 ? {} : _a, _d = _c.valueKey, valueKey = _d === void 0 ? 'value' : _d, _e = _c.defaultValueKey, defaultValueKey = _e === void 0 ? 'defaultValue' : _e, _f = _c.scopeKey, scopeKey = _f === void 0 ? 'mixin' : _f, _g = _c.transformValue, transformValue = _g === void 0 ? function (value) { return ({
  15. needUpdate: true,
  16. value: value,
  17. }); } : _g;
  18. return {
  19. data: (_b = {},
  20. _b[scopeKey] = {
  21. value: undefined,
  22. updated: false,
  23. controlled: false,
  24. },
  25. _b),
  26. onInit: function () {
  27. var value = typeof this.props[valueKey] !== 'undefined' ? this.props[valueKey] : this.props[defaultValueKey];
  28. var needUpdate = this.update(value, {
  29. nextProps: this.props,
  30. }).needUpdate;
  31. if (!needUpdate) {
  32. this.updateControlled();
  33. }
  34. },
  35. deriveDataFromProps: function (nextProps) {
  36. if (!equal(nextProps[valueKey], this.props[valueKey])) {
  37. this.update(nextProps[valueKey], {
  38. nextProps: nextProps,
  39. });
  40. }
  41. },
  42. didUpdate: function (prevProps) {
  43. if (component2) {
  44. return;
  45. }
  46. if (!equal(prevProps[valueKey], this.props[valueKey])) {
  47. this.update(this.props[valueKey], {
  48. nextProps: this.props,
  49. });
  50. }
  51. },
  52. didMount: function () {
  53. if (component2) {
  54. return;
  55. }
  56. var value = typeof this.props[valueKey] !== 'undefined' ? this.props[valueKey] : this.props[defaultValueKey];
  57. var needUpdate = this.update(value, {
  58. nextProps: this.props,
  59. }).needUpdate;
  60. if (!needUpdate) {
  61. this.updateControlled();
  62. }
  63. },
  64. methods: {
  65. getValue: function (prevData) {
  66. return (prevData || this.data)[scopeKey].value;
  67. },
  68. isEqualValue: function (prevData) {
  69. if (!prevData[scopeKey].updated) {
  70. return true;
  71. }
  72. return equal(this.getValue(prevData), this.getValue());
  73. },
  74. isControlled: function () {
  75. if ('controlled' in this.props) {
  76. return this.props.controlled;
  77. }
  78. return valueKey in this.props;
  79. },
  80. updateControlled: function () {
  81. var _a;
  82. this.setData((_a = {},
  83. _a[scopeKey] = {
  84. controlled: this.isControlled(),
  85. },
  86. _a));
  87. },
  88. update: function (val, extra) {
  89. var _a;
  90. var args = [];
  91. for (var _i = 2; _i < arguments.length; _i++) {
  92. args[_i - 2] = arguments[_i];
  93. }
  94. var _b = transformValue.call.apply(transformValue, __spreadArray([this, val, extra], args, false)) || {}, needUpdate = _b.needUpdate, value = _b.value;
  95. if (needUpdate) {
  96. this.setData((_a = {},
  97. _a[scopeKey] = {
  98. value: value,
  99. updated: true,
  100. controlled: this.isControlled(),
  101. },
  102. _a));
  103. }
  104. return {
  105. needUpdate: needUpdate,
  106. value: value,
  107. };
  108. },
  109. },
  110. };
  111. });