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/checkbox.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) (limited to 'web/src/widget/checkbox.rs') diff --git a/web/src/widget/checkbox.rs b/web/src/widget/checkbox.rs index 5ebc26c8..10a46661 100644 --- a/web/src/widget/checkbox.rs +++ b/web/src/widget/checkbox.rs @@ -28,6 +28,7 @@ pub struct Checkbox { is_checked: bool, on_toggle: Rc Message>, label: String, + id: String, width: Length, style: Box, } @@ -51,6 +52,7 @@ impl Checkbox { is_checked, on_toggle: Rc::new(f), label: label.into(), + id: Default::default(), width: Length::Shrink, style: Default::default(), } @@ -71,6 +73,14 @@ impl Checkbox { self.style = style.into(); self } + + /// Sets the id of the [`Checkbox`]. + /// + /// [`Checkbox`]: struct.Checkbox.html + pub fn id(mut self, id: impl Into) -> Self { + self.id = id.into(); + self + } } impl Widget for Checkbox @@ -85,7 +95,10 @@ where ) -> dodrio::Node<'b> { use dodrio::builder::*; - let checkbox_label = bumpalo::format!(in bump, "{}", self.label); + let checkbox_label = + bumpalo::format!(in bump, "{}", self.label).into_bump_str(); + let checkbox_id = + bumpalo::format!(in bump, "{}", self.id).into_bump_str(); let event_bus = bus.clone(); let on_toggle = self.on_toggle.clone(); @@ -96,6 +109,7 @@ where let spacing_class = style_sheet.insert(bump, css::Rule::Spacing(5)); label(bump) + .attr("for", checkbox_id) .attr( "class", bumpalo::format!(in bump, "{} {}", row_class, spacing_class) @@ -110,6 +124,7 @@ where // TODO: Checkbox styling input(bump) .attr("type", "checkbox") + .attr("id", checkbox_id) .bool_attr("checked", self.is_checked) .on("click", move |_root, vdom, _event| { let msg = on_toggle(!is_checked); @@ -118,8 +133,7 @@ where vdom.schedule_render(); }) .finish(), - span(bump).children(vec![ - text(checkbox_label.into_bump_str())]).finish(), + text(checkbox_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/checkbox.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'web/src/widget/checkbox.rs') diff --git a/web/src/widget/checkbox.rs b/web/src/widget/checkbox.rs index 10a46661..4d0c7c17 100644 --- a/web/src/widget/checkbox.rs +++ b/web/src/widget/checkbox.rs @@ -28,7 +28,7 @@ pub struct Checkbox { is_checked: bool, on_toggle: Rc Message>, label: String, - id: String, + id: Option, width: Length, style: Box, } @@ -52,7 +52,7 @@ impl Checkbox { is_checked, on_toggle: Rc::new(f), label: label.into(), - id: Default::default(), + id: None, width: Length::Shrink, style: Default::default(), } @@ -78,7 +78,7 @@ impl Checkbox { /// /// [`Checkbox`]: struct.Checkbox.html pub fn id(mut self, id: impl Into) -> Self { - self.id = id.into(); + self.id = Some(id.into()); self } } @@ -97,8 +97,6 @@ where let checkbox_label = bumpalo::format!(in bump, "{}", self.label).into_bump_str(); - let checkbox_id = - bumpalo::format!(in bump, "{}", self.id).into_bump_str(); let event_bus = bus.clone(); let on_toggle = self.on_toggle.clone(); @@ -108,8 +106,15 @@ where let spacing_class = style_sheet.insert(bump, css::Rule::Spacing(5)); - label(bump) - .attr("for", checkbox_id) + 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)) + }; + + label .attr( "class", bumpalo::format!(in bump, "{} {}", row_class, spacing_class) @@ -122,9 +127,8 @@ where ) .children(vec![ // TODO: Checkbox styling - input(bump) + input .attr("type", "checkbox") - .attr("id", checkbox_id) .bool_attr("checked", self.is_checked) .on("click", move |_root, vdom, _event| { let msg = on_toggle(!is_checked); -- cgit