Skip to content

Commit

Permalink
few more changes for number input
Browse files Browse the repository at this point in the history
  • Loading branch information
genusistimelord committed Nov 27, 2024
1 parent 7d8cd8d commit 1bed9d4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,6 @@ required-features = ["number_input"]
[[example]]
name = "wrap"
required-features = ["wrap", "number_input"]

[lib]
crate-type = ["cdylib", "rlib"]
3 changes: 3 additions & 0 deletions examples/number_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ impl NumberInputDemo {
if let Message::NumInpChanged(Ok(val)) = message {
println!("Value changed to {:?}", val);
self.value = val;
} else if let Message::NumInpChanged(Err(_)) = message {
println!("Error Value reset to 0.0");
self.value = 0.0;
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/widget/typed_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,11 @@ where
match message {
InternalMessage::OnChange(value) => {
self.text = value;

if self.text.ends_with('.') {
self.text.push('0');
}

let value = match T::from_str(&self.text) {
Ok(val) if self.value != val => {
self.value = val.clone();
Expand All @@ -364,6 +369,10 @@ where
}
InternalMessage::OnSubmit => {
if let Some(on_submit) = &self.on_submit {
if self.text.ends_with('.') {
self.text.push('0');
}

let value = match T::from_str(&self.text) {
Ok(v) => Ok(v),
Err(_) => Err(self.text.clone()),
Expand All @@ -374,6 +383,10 @@ where
InternalMessage::OnPaste(value) => {
self.text = value;

if self.text.ends_with('.') {
self.text.push('0');
}

let value = match T::from_str(&self.text) {
Ok(val) if self.value != val => {
self.value = val.clone();
Expand Down

0 comments on commit 1bed9d4

Please sign in to comment.