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
30 lines (27 loc) · 846 Bytes
/
create-icon.tsx
File metadata and controls
30 lines (27 loc) · 846 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
import { isValidElement, ReactNode, createElement, cloneElement } from 'react';
import { Icon } from '@alifd/next';
import { IconType } from '@alilc/lowcode-types';
import { isReactComponent } from './is-react';
import { isESModule } from './is-es-module';
const URL_RE = /^(https?:)\/\//i;
export function createIcon(icon?: IconType | 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 <img src={icon} {...props} />;
}
return <Icon type={icon} {...props} />;
}
if (isValidElement(icon)) {
return cloneElement(icon, { ...props });
}
if (isReactComponent(icon)) {
return createElement(icon, { ...props });
}
return <Icon {...icon} {...props} />;
}