Skip to content

Commit 6184b9c

Browse files
authored
fix: iconfont provider & add support for custom user icons (#79)
1 parent 95d728c commit 6184b9c

File tree

9 files changed

+64
-20
lines changed

9 files changed

+64
-20
lines changed

.changeset/little-ideas-greet.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modelscope-studio/antd': patch
3+
'@modelscope-studio/frontend': patch
4+
'modelscope_studio': patch
5+
---
6+
7+
fix: iconfont provider not displaying after build

.changeset/purple-melons-dance.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@modelscope-studio/antd': patch
3+
'@modelscope-studio/frontend': patch
4+
'modelscope_studio': patch
5+
---
6+
7+
feat: add support for custom user icons

backend/modelscope_studio/components/antd/icon/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
from gradio.events import EventListener
44

5-
from ....utils.dev import ModelScopeComponent, resolve_frontend_dir
5+
from ....utils.dev import ModelScopeDataLayoutComponent, resolve_frontend_dir
66
from .iconfont_provider import AntdIconIconfontProvider
77

88

9-
class AntdIcon(ModelScopeComponent):
9+
class AntdIcon(ModelScopeDataLayoutComponent):
1010
"""
1111
Ant Design: https://ant.design/components/icon
1212
@@ -22,7 +22,7 @@ class AntdIcon(ModelScopeComponent):
2222
]
2323

2424
# supported slots
25-
SLOTS = []
25+
SLOTS = ["component"]
2626

2727
def __init__(
2828
self,

docs/components/antd/icon/demos/basic.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@
1515
antd.Icon("SmileTwoTone")
1616
icon = antd.Icon("HeartTwoTone", two_tone_color="#eb2f96")
1717
icon.click(lambda: print("clicked"))
18+
antd.Divider("Custom Icon")
19+
with antd.Icon():
20+
with ms.Slot("component"):
21+
ms.Text("🔥")
1822
if __name__ == "__main__":
1923
demo.queue().launch()

frontend/antd/icon/Index.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
{...bindEvents($mergedProps)}
6969
slots={$slots}
7070
value={$mergedProps.value}
71-
/>
71+
>
72+
<slot />
73+
</Icon>
7274
{/await}
7375
{/if}

frontend/antd/icon/icon.tsx

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,45 @@
11
import { sveltify } from '@svelte-preprocess-react';
2-
import React from 'react';
2+
import { useIconFontContext } from '@svelte-preprocess-react/context';
3+
import { ReactSlot } from '@svelte-preprocess-react/react-slot';
4+
import React, { useMemo } from 'react';
35
import * as icons from '@ant-design/icons';
46
import type { GetProps } from 'antd';
57

6-
import { useIconFontContext } from './iconfont-provider/context';
8+
() => {
9+
return;
10+
};
711

812
export const Icon = sveltify<
913
GetProps<typeof icons.default> & {
1014
value: string;
11-
}
12-
>(({ value, ...props }) => {
15+
},
16+
['component']
17+
>(({ value, slots, children, ...props }) => {
1318
const IconFont = useIconFontContext();
1419
const icon = icons[value as keyof typeof icons] as React.ComponentType<
1520
GetProps<typeof icons.default>
1621
>;
22+
23+
const CustomComponentIcon = useMemo(() => {
24+
return (() =>
25+
slots.component ? (
26+
<ReactSlot slot={slots.component} />
27+
) : null) as unknown as React.ComponentType<
28+
React.SVGProps<SVGSVGElement>
29+
>;
30+
}, [slots.component]);
31+
32+
if (slots.component) {
33+
return (
34+
<>
35+
<div style={{ display: 'none' }}>{children}</div>
36+
{React.createElement(icons.default, {
37+
...props,
38+
component: CustomComponentIcon,
39+
})}
40+
</>
41+
);
42+
}
1743
return (
1844
<>
1945
{icon ? (

frontend/antd/icon/iconfont-provider/context.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

frontend/antd/icon/iconfont-provider/iconfont-provider.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { sveltify } from '@svelte-preprocess-react';
2+
import { IconFontContext } from '@svelte-preprocess-react/context';
23
import React, { useMemo, useRef } from 'react';
34
import { createFromIconfontCN } from '@ant-design/icons';
45
import type { CustomIconOptions } from '@ant-design/icons/es/components/IconFont';
56
import { isEqual } from 'lodash-es';
67

7-
import { IconFontContext } from './context';
8-
98
export const Icon = sveltify<
109
CustomIconOptions & {
1110
children?: React.ReactNode;

frontend/svelte-preprocess-react/context.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import React, { createContext, useContext, useMemo, useRef } from 'react';
2+
import type { createFromIconfontCN } from '@ant-design/icons';
23
import { isEqual } from 'lodash-es';
34

5+
export const IconFontContext = createContext<ReturnType<
6+
typeof createFromIconfontCN
7+
> | null>(null);
8+
9+
export const useIconFontContext = () => {
10+
return useContext(IconFontContext);
11+
};
12+
413
export const FormItemContext = createContext<{
514
value?: any;
615
onChange?: (...args: any[]) => void;

0 commit comments

Comments
 (0)