Improve accessibility and testability of `ComboBox` (#7658)
Changed it to use labeled_by to avoid kittest finding the label when searching for the ComboBox and also set the value so a screen reader will know what's selected.
This commit is contained in:
parent
0b9bb5f494
commit
d5320fe827
|
|
@ -239,7 +239,7 @@ impl ComboBox {
|
||||||
let mut ir = combo_box_dyn(
|
let mut ir = combo_box_dyn(
|
||||||
ui,
|
ui,
|
||||||
button_id,
|
button_id,
|
||||||
selected_text,
|
selected_text.clone(),
|
||||||
menu_contents,
|
menu_contents,
|
||||||
icon,
|
icon,
|
||||||
wrap_mode,
|
wrap_mode,
|
||||||
|
|
@ -247,14 +247,16 @@ impl ComboBox {
|
||||||
popup_style,
|
popup_style,
|
||||||
(width, height),
|
(width, height),
|
||||||
);
|
);
|
||||||
|
ir.response.widget_info(|| {
|
||||||
|
let mut info = WidgetInfo::new(WidgetType::ComboBox);
|
||||||
|
info.enabled = ui.is_enabled();
|
||||||
|
info.current_text_value = Some(selected_text.text().to_owned());
|
||||||
|
info
|
||||||
|
});
|
||||||
if let Some(label) = label {
|
if let Some(label) = label {
|
||||||
ir.response.widget_info(|| {
|
let label_response = ui.label(label);
|
||||||
WidgetInfo::labeled(WidgetType::ComboBox, ui.is_enabled(), label.text())
|
ir.response = ir.response.labelled_by(label_response.id);
|
||||||
});
|
ir.response |= label_response;
|
||||||
ir.response |= ui.label(label);
|
|
||||||
} else {
|
|
||||||
ir.response
|
|
||||||
.widget_info(|| WidgetInfo::labeled(WidgetType::ComboBox, ui.is_enabled(), ""));
|
|
||||||
}
|
}
|
||||||
ir
|
ir
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -61,3 +61,17 @@ fn text_edit_rtl() {
|
||||||
harness.snapshot(format!("text_edit_rtl_{i}"));
|
harness.snapshot(format!("text_edit_rtl_{i}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn combobox_should_have_value() {
|
||||||
|
let harness = Harness::new_ui(|ui| {
|
||||||
|
egui::ComboBox::from_label("Select an option")
|
||||||
|
.selected_text("Option 1")
|
||||||
|
.show_ui(ui, |_ui| {});
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
harness.get_by_label("Select an option").value().as_deref(),
|
||||||
|
Some("Option 1")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue