Skip to content

Commit

Permalink
fix(Util): copy react node directly, close #1132
Browse files Browse the repository at this point in the history
  • Loading branch information
youluna committed Sep 17, 2019
1 parent 7ac35b0 commit 04618b0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/util/object.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react';

/**
* 获取对象的类型
* @param {*} obj
Expand Down Expand Up @@ -249,13 +251,18 @@ export function deepMerge(target, ...sources) {

if (isPlainObject(target) && isPlainObject(source)) {
for (const key in source) {
if (isPlainObject(source[key])) {
// 如果是object 进行深拷贝
if (
isPlainObject(source[key]) &&
!React.isValidElement(source[key])
) {
if (!target[key]) Object.assign(target, { [key]: {} });
// fix {a: 'te'}, {a:{b:3}}
if (!isPlainObject(target[key])) {
target[key] = source[key];
}
deepMerge(target[key], source[key]);
// string/number/function/react node 等直接复制
} else {
Object.assign(target, { [key]: source[key] });
}
Expand Down
6 changes: 6 additions & 0 deletions test/util/object-spec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import React from 'react';
import assert from 'power-assert';
import * as object from '../../src/util/object';

Expand Down Expand Up @@ -249,6 +250,11 @@ describe('src/object.js', function() {
assert(res.a === 4);
});

it('deepMerge support node', function() {
const res = object.deepMerge({}, {a: <span>ddd</span>}, {b: 3});
console.log(res)
assert(Object.keys(res).length === 2);
});

it('deepMerge support deep', function() {

Expand Down

0 comments on commit 04618b0

Please sign in to comment.