Skip to content
Snippets Groups Projects
Unverified Commit 09b8a057 authored by John's avatar John Committed by GitHub
Browse files

Merge pull request #35 from StarArawn/optional_props_with_default

Require default for every widget prop.
parents d34d7b8e 323d5a5a
No related branches found
No related tags found
No related merge requests found
......@@ -38,8 +38,8 @@ fn TextBoxExample(context: &mut KayakContext) {
rsx! {
<>
<Window position={(50.0, 50.0)} size={(300.0, 300.0)} title={"TextBox Example".to_string()}>
<TextBox styles={Some(input_styles)} value={current_value} on_change={Some(on_change)} placeholder={None as Option<String>} />
<TextBox styles={Some(input_styles)} value={current_value2} on_change={Some(on_change2)} placeholder={None as Option<String>} />
<TextBox styles={Some(input_styles)} value={current_value} on_change={Some(on_change)} />
<TextBox styles={Some(input_styles)} value={current_value2} on_change={Some(on_change2)} />
</Window>
</>
}
......
......@@ -79,11 +79,15 @@ fn TodoApp() {
cloned_set_todos(todos_cloned.clone());
});
let placeholder = Some("Type here to add a new todo!".to_string());
rsx! {
<Window position={(415.0, 50.0)} size={(450.0, 600.0)} title={"Todo!".to_string()}>
<Element styles={Some(top_area_styles)}>
<TextBox styles={Some(text_box_styles)} value={new_todo_value} placeholder={placeholder} on_change={Some(on_change)} />
<TextBox
styles={Some(text_box_styles)}
value={new_todo_value}
placeholder={Some("Type here to add a new todo!".to_string())}
on_change={Some(on_change)}
/>
<AddButton on_event={Some(add_events)} />
</Element>
<Cards cards={todos} on_delete={handle_delete} />
......
......@@ -3,13 +3,14 @@ use derivative::*;
use crate::{context::KayakContext, styles::Style, Index, Widget};
#[derive(Derivative)]
#[derivative(Debug, PartialEq)]
#[derivative(Default, Debug, PartialEq)]
pub struct Fragment {
pub id: Index,
#[derivative(Default(value = "None"))]
pub styles: Option<Style>,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
#[derivative(Default(value = "None"), Debug = "ignore", PartialEq = "ignore")]
pub children: crate::Children,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
#[derivative(Default(value = "None"), Debug = "ignore", PartialEq = "ignore")]
pub on_event: Option<crate::OnEvent>,
}
......
......@@ -58,6 +58,12 @@ impl OnEvent {
#[derive(Clone)]
pub struct Handler<T>(pub Arc<RwLock<dyn FnMut(T) + Send + Sync + 'static>>);
impl<T> Default for Handler<T> {
fn default() -> Self {
Self(Arc::new(RwLock::new(|_| {})))
}
}
impl<T> Handler<T> {
pub fn new<F: FnMut(T) + Send + Sync + 'static>(f: F) -> Handler<T> {
Handler(Arc::new(RwLock::new(f)))
......
use kayak_core::{context::KayakContext, styles::Style, Children, Index};
use kayak_core::{derivative::*, Fragment, Widget};
use kayak_core::{
derivative::{self, *},
Fragment, Widget,
};
use kayak_render_macros::rsx;
#[derive(Derivative)]
#[derivative(Debug, PartialEq)]
#[derivative(Default, Debug, PartialEq)]
#[allow(dead_code)]
struct Test {
id: Index,
#[derivative(Default(value = "None"))]
styles: Option<Style>,
foo: u32,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
#[derivative(Debug = "ignore", PartialEq = "ignore", Default(value = "None"))]
children: Children,
#[derivative(Debug = "ignore", PartialEq = "ignore")]
#[derivative(Debug = "ignore", PartialEq = "ignore", Default(value = "None"))]
pub on_event: Option<kayak_core::OnEvent>,
}
......
......@@ -87,13 +87,14 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments)
"styles : Option< kayak_ui :: core :: styles :: Style >",
],
quote! {
#[derivative(Default(value="None"))]
pub styles: Option<#kayak_core::styles::Style>
},
),
(
vec!["children : Children"],
quote! {
#[derivative(Debug = "ignore", PartialEq = "ignore")]
#[derivative(Default(value="None"), Debug = "ignore", PartialEq = "ignore")]
pub children: #kayak_core::Children
},
),
......@@ -104,7 +105,7 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments)
"on_event : Option <\nkayak_ui :: core :: OnEvent >",
],
quote! {
#[derivative(Debug = "ignore", PartialEq = "ignore")]
#[derivative(Default(value="None"), Debug = "ignore", PartialEq = "ignore")]
pub on_event: Option<#kayak_core::OnEvent>
},
),
......@@ -149,7 +150,7 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments)
use #kayak_core::derivative::*;
#[derive(Derivative)]
#[derivative(Debug, PartialEq, Clone)]
#[derivative(Default, Debug, PartialEq, Clone)]
#vis struct #struct_name #impl_generics {
pub id: #kayak_core::Index,
#inputs_block
......
......@@ -107,22 +107,22 @@ impl<'a, 'c> ToTokens for CustomWidgetAttributes<'a, 'c> {
}
}
#[cfg(feature = "internal")]
let kayak_core = quote! { kayak_core };
#[cfg(not(feature = "internal"))]
let kayak_core = quote! { kayak_ui::core };
// #[cfg(feature = "internal")]
// let kayak_core = quote! { kayak_core };
// #[cfg(not(feature = "internal"))]
// let kayak_core = quote! { kayak_ui::core };
let quoted = if attrs.len() == 0 {
quote!({ id: #kayak_core::Index::default(), styles: None, children: None, on_event: None, })
quote!({ ..Default::default() })
} else {
if !self
.attributes
.iter()
.any(|attribute| attribute.ident().to_token_stream().to_string() == "styles")
{
quote!({ #(#attrs),*, id: #kayak_core::Index::default() })
quote!({ #(#attrs),*, ..Default::default() })
} else {
quote!({ #(#attrs),*, id: #kayak_core::Index::default() })
quote!({ #(#attrs),*, ..Default::default() })
}
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment