forked from alibaba/lowcode-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-icon.tsx
More file actions
40 lines (37 loc) · 995 Bytes
/
create-icon.tsx
File metadata and controls
40 lines (37 loc) · 995 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
28
29
30
31
32
33
34
35
36
37
38
39
40
import { isValidElement, ReactNode, createElement, cloneElement } from 'react';
import { Icon } from '@alifd/next';
import { IPublicTypeIconType } from '@alilc/lowcode-types';
import { isReactComponent } from './is-react';
import { isESModule } from './is-es-module';
const URL_RE = /^(https?:)\/\//i;
export function createIcon(
icon?: IPublicTypeIconType | null,
props?: Record<string, unknown>,
): ReactNode {
if (!icon) {
return null;
}
if (isESModule(icon)) {
icon = icon.default;
}
if (typeof icon === 'string') {
if (URL_RE.test(icon)) {
return createElement('img', {
src: icon,
class: props?.className,
...props,
});
}
return <Icon type={icon} {...props} />;
}
if (isValidElement(icon)) {
return cloneElement(icon, { ...props });
}
if (isReactComponent(icon)) {
return createElement(icon, {
class: props?.className,
...props,
});
}
return <Icon {...icon} {...props} />;
}