-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
33 lines (31 loc) · 895 Bytes
/
index.ts
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
/**
* @file index
* @author Cuttle Cong
* @date 2018/9/6
* @description
*/
import * as React from 'react'
import { IFunction } from '@rcp/_types'
import isComponentClass from '@rcp/util.iscompclass'
/**
* The utility for getting display name
* @public
* @param {React.ComponentType | React.ReactElement<any> | IFunction | string} component
* @return {string}
*/
export default function displayName(
component: React.ComponentType | React.ReactElement<any> | IFunction | string
): string {
if (typeof component === 'string') {
return component
}
if (!component) {
return String(component)
}
const result = (<React.StatelessComponent>component).displayName || (<React.StatelessComponent>component).name
if (!result) {
component = <React.ReactElement<any>>component
return (component.type && displayName(component.type)) || 'Unknown'
}
return result
}