-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
51 lines (43 loc) · 1.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React from 'react';
export default {
/*
* Find and return all matched children by type. `type` can be a React element class or
* string
*/
findAllByType(children, type) {
let result = [];
if (type && type.displayName) {
type = type.displayName;
}
React.Children.forEach(children, child => {
if (child && child.type && child.type.displayName === type) {
result.push(child);
}
});
return result;
},
/*
* Return the first matched child by type, return null otherwise.
* `type` can be a React element class or string.
*/
findChildByType(children, type) {
this.findAllByType(children, type)[0];
},
/*
* Create a new array of children excluding the ones matched the type
*/
withoutType(children) {
let newChildren = [];
let types = [].map.call(arguments, type => {
if (type && type.displayName) { return type.displayName; }
return type;
});
React.Children.forEach(children, child => {
if (child && child.type && child.type.displayName && types.indexOf(child.type.displayName) !== -1) {
return;
}
newChildren.push(child);
});
return newChildren;
}
};