Skip to content

Commit

Permalink
fix: 修复icon组件缺少事件问题 (baidu#9496)
Browse files Browse the repository at this point in the history
Co-authored-by: qinhaoyan <30946345+qinhaoyan@users.noreply.github.com>
  • Loading branch information
qkiroc and qkiroc committed Jan 23, 2024
1 parent 3a6b138 commit 55a0618
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/amis-ui/src/components/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ export function Icon({
onClick = () => {},
onMouseEnter = () => {},
onMouseLeave = () => {},
style
style,
...rest
}: {
icon: string;
iconContent?: string;
Expand All @@ -280,6 +281,14 @@ export function Icon({
return null;
}

// 获取全部事件
let events: any = {};
Object.keys(rest).forEach(key => {
if (key.startsWith('on') && key.length > 2) {
events[key] = rest[key];
}
});

// 直接的icon dom
if (React.isValidElement(icon)) {
return React.cloneElement(icon, {
Expand Down Expand Up @@ -317,6 +326,7 @@ export function Icon({

return (
<div
{...events}
onClick={onClick}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
Expand All @@ -332,6 +342,7 @@ export function Icon({
if (Component) {
return (
<Component
{...events}
onClick={onClick}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
Expand Down Expand Up @@ -366,6 +377,7 @@ export function Icon({
} else {
return (
<svg
{...events}
onClick={onClick}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
Expand All @@ -382,6 +394,7 @@ export function Icon({
if (typeof icon === 'string' && icon.startsWith('<svg')) {
const svgStr = /<svg .*?>(.*?)<\/svg>/.exec(icon);
const svgHTML = createElement('svg', {
...events,
onClick,
onMouseEnter,
onMouseLeave,
Expand All @@ -398,6 +411,7 @@ export function Icon({
if (isURLIcon) {
return (
<img
{...events}
onClick={onClick}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
Expand Down Expand Up @@ -425,6 +439,7 @@ export function Icon({
if (isIconfont) {
return (
<i
{...events}
onClick={onClick}
onMouseEnter={onMouseEnter}
onMouseLeave={onMouseLeave}
Expand Down

0 comments on commit 55a0618

Please sign in to comment.