value.js 4.0 KB

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