craft-ui: a number field whose value is not finite has no numeric value in its node

It shows the dash, as a mixed field does. A NaN numeric value also never equals itself in a node comparison.
This commit is contained in:
aiongg
2026-11-20 07:31:57 +00:00
parent d5ea6a0e53
commit afbb2d66b0
2 changed files with 21 additions and 1 deletions
+2 -1
View File
@@ -603,7 +603,8 @@ impl<V: NumberValue + 'static> Component for NumberField<'_, V> {
/// text value, the selection and the text actions to the same node.
fn access(&self, access: &mut Access, cx: &Cx<'_, Self>) {
let rule = self.rule();
let numeric = match self.shown_number(cx.value()) {
// A value that is not finite shows the dash, as a mixed one does, and has no number to read.
let numeric = match self.shown_number(cx.value()).filter(|value| value.is_finite()) {
Some(value) => Numeric::new(value, rule.bounds()),
None => Numeric::empty(rule.bounds()),
};
+19
View File
@@ -51,3 +51,22 @@ fn a_number_field_that_keeps_its_name_is_described_by_its_caption() {
}
}
}
/// A number field whose value is not a number shows the dash and its node carries no numeric value, only the range;
/// with a number it carries that number.
#[trial]
fn a_number_field_with_no_number_has_no_numeric_value() {
use craft_ui::text::NumberField;
for (value, read) in [(f64::NAN, None), (12.0, Some(12.0))] {
let field = Rc::new(Cell::new(egui::Id::NULL));
let seen = field.clone();
let mut fixture = Fixture::new(move |ui| {
let mut shown = Some(value);
seen.set(NumberField::new("Offset", Bind::bound(&mut shown)).id_salt("offset").show(ui).response.id);
});
fixture.settle();
let numeric = fixture.tree().numeric(field.get()).expect("the field's node is numeric");
assert_eq!(numeric.value, read, "the numeric value of a field holding {value}");
}
}