-
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathcommonUtil.ts
More file actions
27 lines (20 loc) · 783 Bytes
/
commonUtil.ts
File metadata and controls
27 lines (20 loc) · 783 Bytes
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
import toArray from '@rc-component/util/lib/Children/toArray';
import * as React from 'react';
export function parseChildren(children: React.ReactNode | undefined, keyPath: string[]) {
return toArray(children).map((child, index) => {
if (React.isValidElement(child)) {
const { key } = child;
let eventKey = (child.props as any)?.eventKey ?? key;
const emptyKey = eventKey === null || eventKey === undefined;
if (emptyKey) {
eventKey = `tmp_key-${[...keyPath, index].join('-')}`;
}
const cloneProps = { key: eventKey, eventKey } as any;
if (process.env.NODE_ENV !== 'production' && emptyKey) {
cloneProps.warnKey = true;
}
return React.cloneElement(child, cloneProps);
}
return child;
});
}