1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
use dioxus::prelude::*;
use freya_elements::{
elements as dioxus_elements,
events::KeyboardEvent,
};
use freya_hooks::{
use_applied_theme,
use_focus,
RadioTheme,
RadioThemeWith,
};
/// Controlled `Radio` component.
///
/// # Styling
/// Inherits the [`RadioTheme`](freya_hooks::RadioTheme) theme.
///
/// # Example
///
/// ```no_run
/// # use freya::prelude::*;
/// #[derive(PartialEq)]
/// enum Choice {
/// First,
/// Second,
/// }
///
/// fn app() -> Element {
/// let mut selected = use_signal(|| Choice::First);
/// rsx!(
/// Tile {
/// onselect: move |_| selected.set(Choice::First),
/// leading: rsx!(
/// Radio {
/// selected: *selected.read() == Choice::First,
/// },
/// ),
/// label { "First choice" }
/// }
/// Tile {
/// onselect: move |_| selected.set(Choice::Second),
/// leading: rsx!(
/// Radio {
/// selected: *selected.read() == Choice::Second,
/// },
/// ),
/// label { "Second choice" }
/// }
/// )
/// }
/// ```
#[allow(non_snake_case)]
#[component]
pub fn Radio(
/// Indicate whether this radio is selected or not.
selected: bool,
/// Theme override.
theme: Option<RadioThemeWith>,
) -> Element {
let focus = use_focus();
let RadioTheme {
unselected_fill,
selected_fill,
border_fill,
} = use_applied_theme!(&theme, radio);
let fill = if selected {
selected_fill
} else {
unselected_fill
};
let border = if focus.is_selected() {
format!("2 inner {fill}, 4 outer {border_fill}")
} else {
format!("2 inner {fill}")
};
let onkeydown = move |_: KeyboardEvent| {};
rsx!(
rect {
a11y_id: focus.attribute(),
width: "18",
height: "18",
border,
padding: "4",
main_align: "center",
cross_align: "center",
corner_radius: "99",
onkeydown,
if selected {
rect {
width: "10",
height: "10",
background: "{fill}",
corner_radius: "99",
}
}
}
)
}
#[cfg(test)]
mod test {
use dioxus::prelude::use_signal;
use freya::prelude::*;
use freya_testing::prelude::*;
#[tokio::test]
pub async fn radio() {
#[derive(PartialEq)]
enum Choice {
First,
Second,
Third,
}
fn radio_app() -> Element {
let mut selected = use_signal(|| Choice::First);
rsx!(
Tile {
onselect: move |_| selected.set(Choice::First),
leading: rsx!(
Radio {
selected: *selected.read() == Choice::First,
},
),
label { "First choice" }
}
Tile {
onselect: move |_| selected.set(Choice::Second),
leading: rsx!(
Radio {
selected: *selected.read() == Choice::Second,
},
),
label { "Second choice" }
}
Tile {
onselect: move |_| selected.set(Choice::Third),
leading: rsx!(
Radio {
selected: *selected.read() == Choice::Third,
},
),
label { "Third choice" }
}
)
}
let mut utils = launch_test(radio_app);
let root = utils.root();
utils.wait_for_update().await;
// If the inner circle exists it means that the Radio is activated, otherwise it isn't
assert!(root.get(0).get(0).get(0).get(0).is_element());
assert!(root.get(1).get(0).get(0).get(0).is_placeholder());
assert!(root.get(2).get(0).get(0).get(0).is_placeholder());
utils.click_cursor((20., 50.)).await;
assert!(root.get(0).get(0).get(0).get(0).is_placeholder());
assert!(root.get(1).get(0).get(0).get(0).is_element());
assert!(root.get(2).get(0).get(0).get(0).is_placeholder());
utils.click_cursor((10., 90.)).await;
assert!(root.get(0).get(0).get(0).get(0).is_placeholder());
assert!(root.get(1).get(0).get(0).get(0).is_placeholder());
assert!(root.get(2).get(0).get(0).get(0).is_element());
utils.click_cursor((10., 10.)).await;
assert!(root.get(0).get(0).get(0).get(0).is_element());
assert!(root.get(1).get(0).get(0).get(0).is_placeholder());
assert!(root.get(2).get(0).get(0).get(0).is_placeholder());
}
}