From a2b91a7a2b9d5cda519103e8cae5a9239a56d372 Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Wed, 23 Jul 2025 14:30:16 +0800 Subject: [PATCH] input(state): Provide context to the `validate` method (#1082) --- crates/story/src/input_story.rs | 2 +- crates/story/src/table_story.rs | 2 +- crates/ui/src/input/state.rs | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/story/src/input_story.rs b/crates/story/src/input_story.rs index fd17b6c9..1494a807 100644 --- a/crates/story/src/input_story.rs +++ b/crates/story/src/input_story.rs @@ -110,7 +110,7 @@ impl InputStory { large_input: cx.new(|cx| InputState::new(window, cx).placeholder("Large input")), small_input: cx.new(|cx| { InputState::new(window, cx) - .validate(|s| s.parse::().is_ok()) + .validate(|s, _| s.parse::().is_ok()) .placeholder("validate to limit float number.") }), prefix_input1, diff --git a/crates/story/src/table_story.rs b/crates/story/src/table_story.rs index 34e68bc2..d076d994 100644 --- a/crates/story/src/table_story.rs +++ b/crates/story/src/table_story.rs @@ -626,7 +626,7 @@ impl TableStory { let num_stocks_input = cx.new(|cx| { let mut input = InputState::new(window, cx) .placeholder("Enter number of Stocks to display") - .validate(|s| s.parse::().is_ok()); + .validate(|s, _| s.parse::().is_ok()); input.set_value("5000", window, cx); input }); diff --git a/crates/ui/src/input/state.rs b/crates/ui/src/input/state.rs index 39c5df13..411bf1e5 100644 --- a/crates/ui/src/input/state.rs +++ b/crates/ui/src/input/state.rs @@ -263,7 +263,7 @@ pub struct InputState { pub(super) masked: bool, pub(super) clean_on_escape: bool, pub(super) pattern: Option, - pub(super) validate: Option bool + 'static>>, + pub(super) validate: Option) -> bool + 'static>>, pub(crate) scroll_handle: ScrollHandle, pub(super) scroll_state: ScrollbarState, /// The size of the scrollable content. @@ -776,7 +776,7 @@ impl InputState { } /// Set the validation function of the input field. - pub fn validate(mut self, f: impl Fn(&str) -> bool + 'static) -> Self { + pub fn validate(mut self, f: impl Fn(&str, &mut Context) -> bool + 'static) -> Self { self.validate = Some(Box::new(f)); self } @@ -2049,13 +2049,13 @@ impl InputState { self.select_to(Cursor::new(offset), window, cx); } - fn is_valid_input(&self, new_text: &str) -> bool { + fn is_valid_input(&self, new_text: &str, cx: &mut Context) -> bool { if new_text.is_empty() { return true; } if let Some(validate) = &self.validate { - if !validate(new_text) { + if !validate(new_text, cx) { return false; } } @@ -2182,7 +2182,7 @@ impl EntityInputHandler for InputState { + self.text_for_range_utf8(range.end..self.text.len())) .into(); // Check if the new text is valid - if !self.is_valid_input(&pending_text) { + if !self.is_valid_input(&pending_text, cx) { return; } @@ -2227,7 +2227,7 @@ impl EntityInputHandler for InputState { + new_text + self.text_for_range_utf8(range.end..self.text.len())) .into(); - if !self.is_valid_input(&pending_text) { + if !self.is_valid_input(&pending_text, cx) { return; }