mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2025-12-16 15:01:15 +00:00
34 lines
881 B
TypeScript
34 lines
881 B
TypeScript
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 <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} />;
|
|
}
|