input(state): Provide context to the validate method (#1082)

This commit is contained in:
Floyd Wang 2025-07-23 14:30:16 +08:00 committed by GitHub
parent 271116926c
commit a2b91a7a2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 8 additions and 8 deletions

View file

@ -110,7 +110,7 @@ impl InputStory {
large_input: cx.new(|cx| InputState::new(window, cx).placeholder("Large input")), large_input: cx.new(|cx| InputState::new(window, cx).placeholder("Large input")),
small_input: cx.new(|cx| { small_input: cx.new(|cx| {
InputState::new(window, cx) InputState::new(window, cx)
.validate(|s| s.parse::<f32>().is_ok()) .validate(|s, _| s.parse::<f32>().is_ok())
.placeholder("validate to limit float number.") .placeholder("validate to limit float number.")
}), }),
prefix_input1, prefix_input1,

View file

@ -626,7 +626,7 @@ impl TableStory {
let num_stocks_input = cx.new(|cx| { let num_stocks_input = cx.new(|cx| {
let mut input = InputState::new(window, cx) let mut input = InputState::new(window, cx)
.placeholder("Enter number of Stocks to display") .placeholder("Enter number of Stocks to display")
.validate(|s| s.parse::<usize>().is_ok()); .validate(|s, _| s.parse::<usize>().is_ok());
input.set_value("5000", window, cx); input.set_value("5000", window, cx);
input input
}); });

View file

@ -263,7 +263,7 @@ pub struct InputState {
pub(super) masked: bool, pub(super) masked: bool,
pub(super) clean_on_escape: bool, pub(super) clean_on_escape: bool,
pub(super) pattern: Option<regex::Regex>, pub(super) pattern: Option<regex::Regex>,
pub(super) validate: Option<Box<dyn Fn(&str) -> bool + 'static>>, pub(super) validate: Option<Box<dyn Fn(&str, &mut Context<Self>) -> bool + 'static>>,
pub(crate) scroll_handle: ScrollHandle, pub(crate) scroll_handle: ScrollHandle,
pub(super) scroll_state: ScrollbarState, pub(super) scroll_state: ScrollbarState,
/// The size of the scrollable content. /// The size of the scrollable content.
@ -776,7 +776,7 @@ impl InputState {
} }
/// Set the validation function of the input field. /// 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<Self>) -> bool + 'static) -> Self {
self.validate = Some(Box::new(f)); self.validate = Some(Box::new(f));
self self
} }
@ -2049,13 +2049,13 @@ impl InputState {
self.select_to(Cursor::new(offset), window, cx); 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<Self>) -> bool {
if new_text.is_empty() { if new_text.is_empty() {
return true; return true;
} }
if let Some(validate) = &self.validate { if let Some(validate) = &self.validate {
if !validate(new_text) { if !validate(new_text, cx) {
return false; return false;
} }
} }
@ -2182,7 +2182,7 @@ impl EntityInputHandler for InputState {
+ self.text_for_range_utf8(range.end..self.text.len())) + self.text_for_range_utf8(range.end..self.text.len()))
.into(); .into();
// Check if the new text is valid // Check if the new text is valid
if !self.is_valid_input(&pending_text) { if !self.is_valid_input(&pending_text, cx) {
return; return;
} }
@ -2227,7 +2227,7 @@ impl EntityInputHandler for InputState {
+ new_text + new_text
+ self.text_for_range_utf8(range.end..self.text.len())) + self.text_for_range_utf8(range.end..self.text.len()))
.into(); .into();
if !self.is_valid_input(&pending_text) { if !self.is_valid_input(&pending_text, cx) {
return; return;
} }