2
2
3
3
#[ cfg( not( feature = "generator" ) ) ]
4
4
mod generated;
5
- #[ cfg( not( feature = "generator" ) ) ]
6
- pub use generated:: { get_svg, IconId } ;
5
+
6
+ use std:: cmp:: Ordering ;
7
+ use std:: fmt:: Debug ;
7
8
8
9
#[ cfg( not( feature = "generator" ) ) ]
9
10
use yew:: prelude:: * ;
10
11
11
12
#[ cfg( not( feature = "generator" ) ) ]
12
13
use yew:: virtual_dom:: AttrValue ;
13
14
15
+ #[ derive( Copy , Clone ) ]
16
+ pub struct IconData {
17
+ name : & ' static str ,
18
+ html : fn ( props : & IconProps ) -> Html ,
19
+ }
20
+
21
+ impl IconData {
22
+ const HELLO_WORLD : IconData = IconData {
23
+ name : "HelloWorld" ,
24
+ html : |props : & IconProps | {
25
+ html ! {
26
+ <p>{ "Hello world" } </p>
27
+ }
28
+ } ,
29
+ } ;
30
+ }
31
+
32
+ impl PartialEq for IconData {
33
+ fn eq ( & self , other : & Self ) -> bool {
34
+ self . name . eq ( other. name )
35
+ }
36
+ }
37
+ impl Eq for IconData { }
38
+
39
+ impl Ord for IconData {
40
+ fn cmp ( & self , other : & Self ) -> Ordering {
41
+ self . name . cmp ( other. name )
42
+ }
43
+ }
44
+
45
+ impl PartialOrd for IconData {
46
+ fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
47
+ Some ( self . cmp ( other) )
48
+ }
49
+ }
50
+
51
+ impl Debug for IconData {
52
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
53
+ f. write_str ( self . name )
54
+ }
55
+ }
56
+
14
57
/// For customizing icon rendering. Only `icon_id` is required.
15
58
#[ cfg( not( feature = "generator" ) ) ]
16
59
#[ derive( Properties , PartialEq ) ]
17
60
pub struct IconProps {
18
- /// Which icon to render. Enable icons with feature flags.
19
- pub icon_id : IconId ,
61
+ /// Which icon to render. Enable icon collections with feature flags.
62
+ pub data : IconData ,
20
63
/// Tooltip text.
21
64
#[ prop_or( None ) ]
22
65
pub title : Option < AttrValue > ,
@@ -37,7 +80,7 @@ pub struct IconProps {
37
80
pub class : Classes ,
38
81
/// For inline CSS.
39
82
#[ prop_or( None ) ]
40
- pub style : Option < AttrValue > ,
83
+ pub style : Option < AttrValue > ,
41
84
#[ prop_or( None ) ]
42
85
pub role : Option < AttrValue > ,
43
86
}
@@ -61,38 +104,34 @@ pub struct IconProps {
61
104
#[ cfg( not( feature = "generator" ) ) ]
62
105
#[ function_component( Icon ) ]
63
106
pub fn icon ( props : & IconProps ) -> Html {
64
- get_svg ( props)
107
+ ( props . data . html ) ( props)
65
108
}
66
109
67
110
#[ cfg( test) ]
68
111
mod test {
69
- use crate :: { Icon , IconId , IconProps } ;
70
- use enum_iterator:: IntoEnumIterator ;
112
+ use crate :: { Icon , IconData , IconProps } ;
71
113
use yew:: prelude:: * ;
72
114
73
115
#[ tokio:: test]
74
116
async fn test ( ) {
75
- for icon_id in IconId :: into_enum_iter ( ) {
76
- println ! ( "rendering icon {:?}" , icon_id) ;
77
- let icon_id = icon_id. clone ( ) ;
78
- let renderer = yew:: ServerRenderer :: < Icon > :: with_props ( move || IconProps {
79
- icon_id,
80
- width : "2em" . into ( ) ,
81
- height : "3em" . into ( ) ,
82
- onclick : Some ( Callback :: from ( |_e : MouseEvent | { } ) ) ,
83
- class : Classes :: new ( ) ,
84
- oncontextmenu : None ,
85
- style : None ,
86
- title : None ,
87
- role : Some ( "presentation" . into ( ) ) ,
88
- } ) ;
89
-
90
- let rendered = renderer. render ( ) . await ;
91
-
92
- assert ! ( rendered. contains( "2em" ) , "{:?} {}" , icon_id, rendered) ;
93
- assert ! ( rendered. contains( "3em" ) , "{:?} {}" , icon_id, rendered) ;
94
-
95
- //println!("{:?} => {}", icon_id, rendered);
96
- }
117
+ let data = IconData :: HELLO_WORLD ;
118
+ let renderer = yew:: ServerRenderer :: < Icon > :: with_props ( move || IconProps {
119
+ data,
120
+ width : "2em" . into ( ) ,
121
+ height : "3em" . into ( ) ,
122
+ onclick : Some ( Callback :: from ( |_e : MouseEvent | { } ) ) ,
123
+ class : Classes :: new ( ) ,
124
+ oncontextmenu : None ,
125
+ style : None ,
126
+ title : None ,
127
+ role : Some ( "presentation" . into ( ) ) ,
128
+ } ) ;
129
+
130
+ let rendered = renderer. render ( ) . await ;
131
+
132
+ assert ! ( rendered. contains( "2em" ) , "{data:?} {}" , rendered) ;
133
+ assert ! ( rendered. contains( "3em" ) , "{data:?} {}" , rendered) ;
134
+
135
+ //println!("{:?} => {}", icon_id, rendered);
97
136
}
98
137
}
0 commit comments