From 852d59752e209d4f3cdc38e164f5eeb31e164700 Mon Sep 17 00:00:00 2001 From: Tom Pridham Date: Sun, 12 Apr 2020 00:51:17 -0600 Subject: add some accessibility features to web widgets --- web/src/widget/radio.rs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'web/src/widget/radio.rs') diff --git a/web/src/widget/radio.rs b/web/src/widget/radio.rs index 520b24cd..acbac3b6 100644 --- a/web/src/widget/radio.rs +++ b/web/src/widget/radio.rs @@ -35,6 +35,8 @@ pub struct Radio { is_selected: bool, on_click: Message, label: String, + id: String, + name: String, style: Box, } @@ -63,6 +65,8 @@ impl Radio { is_selected: Some(value) == selected, on_click: f(value), label: label.into(), + id: Default::default(), + name: Default::default(), style: Default::default(), } } @@ -74,6 +78,22 @@ impl Radio { self.style = style.into(); self } + + /// Sets the name attribute of the [`Radio`] button. + /// + /// [`Radio`]: struct.Radio.html + pub fn name(mut self, name: impl Into) -> Self { + self.name = name.into(); + self + } + + /// Sets the id of the [`Radio`] button. + /// + /// [`Radio`]: struct.Radio.html + pub fn id(mut self, id: impl Into) -> Self { + self.id = id.into(); + self + } } impl Widget for Radio @@ -88,7 +108,11 @@ where ) -> dodrio::Node<'b> { use dodrio::builder::*; - let radio_label = bumpalo::format!(in bump, "{}", self.label); + let radio_label = + bumpalo::format!(in bump, "{}", self.label).into_bump_str(); + let radio_name = + bumpalo::format!(in bump, "{}", self.name).into_bump_str(); + let radio_id = bumpalo::format!(in bump, "{}", self.id).into_bump_str(); let event_bus = bus.clone(); let on_click = self.on_click.clone(); @@ -96,16 +120,19 @@ where // TODO: Complete styling label(bump) .attr("style", "display: block; font-size: 20px") + .attr("for", radio_id) .children(vec![ input(bump) .attr("type", "radio") + .attr("id", radio_id) + .attr("name", radio_name) .attr("style", "margin-right: 10px") .bool_attr("checked", self.is_selected) .on("click", move |_root, _vdom, _event| { event_bus.publish(on_click.clone()); }) .finish(), - text(radio_label.into_bump_str()), + text(radio_label), ]) .finish() } -- cgit From ffd195cdb543dfbff42327c516d6082cb2df51ef Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 1 Jul 2020 06:52:13 +0200 Subject: Fix empty `id` and `name` attributes in `iced_web` --- web/src/widget/radio.rs | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'web/src/widget/radio.rs') diff --git a/web/src/widget/radio.rs b/web/src/widget/radio.rs index acbac3b6..fae67cd8 100644 --- a/web/src/widget/radio.rs +++ b/web/src/widget/radio.rs @@ -35,8 +35,8 @@ pub struct Radio { is_selected: bool, on_click: Message, label: String, - id: String, - name: String, + id: Option, + name: Option, style: Box, } @@ -65,8 +65,8 @@ impl Radio { is_selected: Some(value) == selected, on_click: f(value), label: label.into(), - id: Default::default(), - name: Default::default(), + id: None, + name: None, style: Default::default(), } } @@ -83,7 +83,7 @@ impl Radio { /// /// [`Radio`]: struct.Radio.html pub fn name(mut self, name: impl Into) -> Self { - self.name = name.into(); + self.name = Some(name.into()); self } @@ -91,7 +91,7 @@ impl Radio { /// /// [`Radio`]: struct.Radio.html pub fn id(mut self, id: impl Into) -> Self { - self.id = id.into(); + self.id = Some(id.into()); self } } @@ -110,22 +110,32 @@ where let radio_label = bumpalo::format!(in bump, "{}", self.label).into_bump_str(); - let radio_name = - bumpalo::format!(in bump, "{}", self.name).into_bump_str(); - let radio_id = bumpalo::format!(in bump, "{}", self.id).into_bump_str(); let event_bus = bus.clone(); let on_click = self.on_click.clone(); + let (label, input) = if let Some(id) = &self.id { + let id = bumpalo::format!(in bump, "{}", id).into_bump_str(); + + (label(bump).attr("for", id), input(bump).attr("id", id)) + } else { + (label(bump), input(bump)) + }; + + let input = if let Some(name) = &self.name { + let name = bumpalo::format!(in bump, "{}", name).into_bump_str(); + + dodrio::builder::input(bump).attr("name", name) + } else { + input + }; + // TODO: Complete styling - label(bump) + label .attr("style", "display: block; font-size: 20px") - .attr("for", radio_id) .children(vec![ - input(bump) + input .attr("type", "radio") - .attr("id", radio_id) - .attr("name", radio_name) .attr("style", "margin-right: 10px") .bool_attr("checked", self.is_selected) .on("click", move |_root, _vdom, _event| { -- cgit From 75464ad89422884e0718eb0429586a9d77f61c71 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 1 Jul 2020 07:36:42 +0200 Subject: Use `String::from_str_in` in `iced_web` --- web/src/widget/radio.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'web/src/widget/radio.rs') diff --git a/web/src/widget/radio.rs b/web/src/widget/radio.rs index fae67cd8..c9d0a00e 100644 --- a/web/src/widget/radio.rs +++ b/web/src/widget/radio.rs @@ -107,15 +107,16 @@ where _style_sheet: &mut Css<'b>, ) -> dodrio::Node<'b> { use dodrio::builder::*; + use dodrio::bumpalo::collections::String; let radio_label = - bumpalo::format!(in bump, "{}", self.label).into_bump_str(); + String::from_str_in(&self.label, bump).into_bump_str(); let event_bus = bus.clone(); let on_click = self.on_click.clone(); let (label, input) = if let Some(id) = &self.id { - let id = bumpalo::format!(in bump, "{}", id).into_bump_str(); + let id = String::from_str_in(id, bump).into_bump_str(); (label(bump).attr("for", id), input(bump).attr("id", id)) } else { @@ -123,7 +124,7 @@ where }; let input = if let Some(name) = &self.name { - let name = bumpalo::format!(in bump, "{}", name).into_bump_str(); + let name = String::from_str_in(name, bump).into_bump_str(); dodrio::builder::input(bump).attr("name", name) } else { -- cgit