You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

101 lines
2.4 KiB

  1. import React from 'react';
  2. export function toArrayChildren(children) {
  3. var ret = [];
  4. React.Children.forEach(children, function (child) {
  5. ret.push(child);
  6. });
  7. return ret;
  8. }
  9. export function findChildInChildrenByKey(children, key) {
  10. var ret = null;
  11. if (children) {
  12. children.forEach(function (child) {
  13. if (ret) {
  14. return;
  15. }
  16. if (child && child.key === key) {
  17. ret = child;
  18. }
  19. });
  20. }
  21. return ret;
  22. }
  23. export function findShownChildInChildrenByKey(children, key, showProp) {
  24. var ret = null;
  25. if (children) {
  26. children.forEach(function (child) {
  27. if (child && child.key === key && child.props[showProp]) {
  28. if (ret) {
  29. throw new Error('two child with same key for <rc-animate> children');
  30. }
  31. ret = child;
  32. }
  33. });
  34. }
  35. return ret;
  36. }
  37. export function findHiddenChildInChildrenByKey(children, key, showProp) {
  38. var found = 0;
  39. if (children) {
  40. children.forEach(function (child) {
  41. if (found) {
  42. return;
  43. }
  44. found = child && child.key === key && !child.props[showProp];
  45. });
  46. }
  47. return found;
  48. }
  49. export function isSameChildren(c1, c2, showProp) {
  50. var same = c1.length === c2.length;
  51. if (same) {
  52. c1.forEach(function (child, index) {
  53. var child2 = c2[index];
  54. if (child && child2) {
  55. if (child && !child2 || !child && child2) {
  56. same = false;
  57. } else if (child.key !== child2.key) {
  58. same = false;
  59. } else if (showProp && child.props[showProp] !== child2.props[showProp]) {
  60. same = false;
  61. }
  62. }
  63. });
  64. }
  65. return same;
  66. }
  67. export function mergeChildren(prev, next) {
  68. var ret = [];
  69. // For each key of `next`, the list of keys to insert before that key in
  70. // the combined list
  71. var nextChildrenPending = {};
  72. var pendingChildren = [];
  73. prev.forEach(function (child) {
  74. if (child && findChildInChildrenByKey(next, child.key)) {
  75. if (pendingChildren.length) {
  76. nextChildrenPending[child.key] = pendingChildren;
  77. pendingChildren = [];
  78. }
  79. } else {
  80. pendingChildren.push(child);
  81. }
  82. });
  83. next.forEach(function (child) {
  84. if (child && Object.prototype.hasOwnProperty.call(nextChildrenPending, child.key)) {
  85. ret = ret.concat(nextChildrenPending[child.key]);
  86. }
  87. ret.push(child);
  88. });
  89. ret = ret.concat(pendingChildren);
  90. return ret;
  91. }