Support Optional Read Signals (#2761)

* progress: add test

* feat: optional read signals

* revision: doc

* fix: fmt

* revision: imports

* fix: return type only if option

* add a few more compile tests for optional props

---------

Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
This commit is contained in:
Miles MurgawandEvan Almloff authored and GitHub committed 2024-08-01 19:22:35 +00:00
1 parent c5fb69f9f1
commit e0bb67b476
2 files changed
+94 -21

No files matched your search

+53
View File
@@ -51,3 +51,56 @@ mod test_default_into {
pub some_other_data: bool,
}
}
/// This test ensures that read-only signals that contain an option (`Signal<Option<u16>>`)
/// are correctly created as default when not provided.
///
/// These are compile-time tests.
/// See https://github.com/DioxusLabs/dioxus/issues/2648
#[cfg(test)]
#[allow(unused)]
mod test_optional_signals {
use dioxus::prelude::*;
// Test if test components fail to compile.
#[component]
fn UsesComponents() -> Element {
rsx! {
PropsStruct {
regular_read_signal: ReadOnlySignal::new(Signal::new(1234)),
}
PropsStruct {
optional_read_signal: 1234,
regular_read_signal: 123u16,
}
PropParams {}
PropParams {
opt_read_sig: 1234
}
DoubleOption {}
DoubleOption { optional: Some(1234) }
}
}
// Test props as struct param.
#[derive(Props, Clone, PartialEq)]
struct MyTestProps {
pub optional_read_signal: ReadOnlySignal<Option<u16>>,
pub regular_read_signal: ReadOnlySignal<u16>,
}
#[component]
fn PropsStruct(props: MyTestProps) -> Element {
rsx! { "hi" }
}
// Test props as params.
#[component]
fn PropParams(opt_read_sig: ReadOnlySignal<Option<u16>>) -> Element {
rsx! { "hi!" }
}
#[component]
fn DoubleOption(optional: Option<Option<u16>>) -> Element {
rsx! { "hi!" }
}
}