1
+ #![ allow( non_snake_case) ]
2
+
1
3
//! A tour of the rsx! macro
2
4
//! ------------------------
3
5
//!
@@ -42,10 +44,6 @@ fn main() {
42
44
dioxus_desktop:: launch ( app) ;
43
45
}
44
46
45
- /// When trying to return "nothing" to Dioxus, you'll need to specify the type parameter or Rust will be sad.
46
- /// This type alias specifies the type for you so you don't need to write "None as Option<()>"
47
- const NONE_ELEMENT : Option < ( ) > = None ;
48
-
49
47
use core:: { fmt, str:: FromStr } ;
50
48
use std:: fmt:: Display ;
51
49
@@ -63,7 +61,7 @@ fn app(cx: Scope) -> Element {
63
61
h1 { "Some text" }
64
62
h1 { "Some text with {formatting}" }
65
63
h1 { "Formatting basic expressions {formatting_tuple.0} and {formatting_tuple.1}" }
66
- h1 { "Formatting without interpolation " [ formatting_tuple. 0 ] "and" [ formatting_tuple. 1 ] }
64
+ h1 { "Formatting without interpolation " formatting_tuple. 0 "and" formatting_tuple. 1 }
67
65
h2 {
68
66
"Multiple"
69
67
"Text"
@@ -131,13 +129,9 @@ fn app(cx: Scope) -> Element {
131
129
None
132
130
}
133
131
134
-
135
132
// returning "None" without a diverging branch is a bit noisy... but rare in practice
136
133
None as Option <( ) >,
137
134
138
- // Use the Dioxus type-alias for less noise
139
- NONE_ELEMENT ,
140
-
141
135
// can also just use empty fragments
142
136
Fragment { }
143
137
@@ -160,7 +154,7 @@ fn app(cx: Scope) -> Element {
160
154
// Can accept any paths
161
155
// Notice how you still get syntax highlighting and IDE support :)
162
156
Baller { }
163
- baller:: Baller { }
157
+ baller:: Baller { }
164
158
crate :: baller:: Baller { }
165
159
166
160
// Can take properties
@@ -187,11 +181,13 @@ fn app(cx: Scope) -> Element {
187
181
// This component's props are defined *inline* with the `inline_props` macro
188
182
WithInline { text: "using functionc all syntax" }
189
183
190
- // Components can be geneirc too
184
+ // Components can be generic too
191
185
// This component takes i32 type to give you typed input
192
186
TypedInput :: <TypedInputProps <i32 >> { }
187
+
193
188
// Type inference can be used too
194
189
TypedInput { initial: 10.0 }
190
+
195
191
// geneircs with the `inline_props` macro
196
192
Label { text: "hello geneirc world!" }
197
193
Label { text: 99.9 }
@@ -205,10 +201,23 @@ fn app(cx: Scope) -> Element {
205
201
// helper functions
206
202
// Anything that implements IntoVnode can be dropped directly into Rsx
207
203
helper( & cx, "hello world!" )
204
+
205
+ // Strings can be supplied directly
206
+ String :: from( "Hello world!" )
207
+
208
+ // So can format_args
209
+ format_args!( "Hello {}!" , "world" )
210
+
211
+ // Or we can shell out to a helper function
212
+ format_dollars( 10 , 50 )
208
213
}
209
214
} )
210
215
}
211
216
217
+ fn format_dollars ( dollars : u32 , cents : u32 ) -> String {
218
+ format ! ( "${}.{:02}" , dollars, cents)
219
+ }
220
+
212
221
fn helper < ' a > ( cx : & ' a ScopeState , text : & str ) -> Element < ' a > {
213
222
cx. render ( rsx ! {
214
223
p { "{text}" }
@@ -223,7 +232,7 @@ fn lowercase_helper(cx: Scope) -> Element {
223
232
224
233
mod baller {
225
234
use super :: * ;
226
- #[ derive( Props , PartialEq ) ]
235
+ #[ derive( Props , PartialEq , Eq ) ]
227
236
pub struct BallerProps { }
228
237
229
238
#[ allow( non_snake_case) ]
@@ -252,7 +261,7 @@ pub fn Taller<'a>(cx: Scope<'a, TallerProps<'a>>) -> Element {
252
261
} )
253
262
}
254
263
255
- #[ derive( Props , PartialEq ) ]
264
+ #[ derive( Props , PartialEq , Eq ) ]
256
265
pub struct TypedInputProps < T > {
257
266
#[ props( optional, default ) ]
258
267
initial : Option < T > ,
0 commit comments