value.js 4.2 KB

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