Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MudTextField: Check if user is trying to enter negative number #11074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from

Conversation

Lewis-Pitman
Copy link
Contributor

@Lewis-Pitman Lewis-Pitman commented Mar 21, 2025

Description

Check if user is trying to enter negative number. If they are, modify the text so that it allows them to do so.
This seems like a hacky fix. May need guidance on an alternative fix
For issue #11055

How Has This Been Tested?

Visually

Type of Changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (fix or improvement to the website or code docs)

Before:
https://github.com/user-attachments/assets/be2f42db-a9c4-4f5d-93bd-d68fb7f18289

After:
https://github.com/user-attachments/assets/21621dd6-ba74-4e99-b4f8-c2b0056671c1

Checklist

  • The PR is submitted to the correct branch (dev).
  • My code follows the code style of this project.
  • I've added relevant tests.

Check if user is trying to enter negative number. If they are, modify the text so that it allows them to do so.
@github-actions github-actions bot added bug Something does not work as intended/expected PR: needs review labels Mar 21, 2025
@Lewis-Pitman
Copy link
Contributor Author

I am unsure whether this is the best way to go about fixing this. I posted some more information in a comment in #11055

@Lewis-Pitman Lewis-Pitman changed the title Check if user is trying to enter negative number MudTextField: Check if user is trying to enter negative number Mar 21, 2025
vernou

This comment was marked as resolved.

@Lewis-Pitman
Copy link
Contributor Author

This is a change in MudTextField.razor.cs that gives the desired effect. I have not yet implemented this for MudNumericField. Does this look better? @vernou
image

@Nickztar
Copy link
Contributor

Would probably have to check for more types of Value, or maybe it would be possible to check if Value is INumber

@vernou
Copy link
Contributor

vernou commented Mar 24, 2025

@Lewis-Pitman, don't hesitate to commit and push. It's more pleasant to review instead of image.


Consider to check T instead of Value.

Allow the user to type negative numbers correctly if bound to a numeric type
Copy link

codecov bot commented Mar 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.98%. Comparing base (68a2118) to head (325d7a4).
Report is 12 commits behind head on dev.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev   #11074      +/-   ##
==========================================
- Coverage   91.05%   90.98%   -0.08%     
==========================================
  Files         429      431       +2     
  Lines       13965    14062      +97     
  Branches     2698     2732      +34     
==========================================
+ Hits        12716    12794      +78     
- Misses        646      651       +5     
- Partials      603      617      +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Lewis-Pitman
Copy link
Contributor Author

This code makes MudTextField work as expected. Checking if the value was INumber did not work. Happy to implement tests if this method is accepted.

As for MudNumericField, I have been debugging the issue for hours with no luck. I think modifying MudBaseInput may be necessary to get it to work. Using SetTextAsync on MudNumericField doesn't change anything. I even tried intercepting the - key inside HandleKeyDownAsync but had no luck getting the desired outcome. I am unsure what to do here

@Lewis-Pitman
Copy link
Contributor Author

As for MudNumericField, I have been debugging the issue for hours with no luck.

Seems like there is a seperate issue #11035 for MudNumericField specifically. Since the solution seems to be a bit more complex, it might be better for me to leave it for now and someone else can try this?

@Lewis-Pitman
Copy link
Contributor Author

Lewis-Pitman commented Mar 25, 2025

Also just noticed that I'll need to add nullable types too like int? And float? For example

@Anu6is Anu6is requested a review from ScarletKuro March 26, 2025 09:16
@vernou
Copy link
Contributor

vernou commented Mar 26, 2025

MudNumericField use the tag HTML <input type="number">, so it's the browser that valid the input. You can ignore it. I try to fix it in #11035.

- MudTextField allows a negative number to be entered on the first attempt when bound to a numeric type
- Added test to check this works
- Reverted MudBaseInput changes
.Add(p => p.Immediate, true)
.Add(p => p.Value, 5));

comp.Find("input").Input("0-");

This comment was marked as resolved.

comp.Find("input").Input("0-");

comp.Instance.Text.Should().Be("-");
comp.Instance.Text.Should().NotBe("0");

This comment was marked as resolved.

Reverted MudBaseInput to what it was before. This was not properly reverted before, causing a test to run infinitely

Improved the test by checking if entering nothing changes the value of 5 to 0.
@Lewis-Pitman
Copy link
Contributor Author

I realised that I hadn't reverted MudBaseInput properly after the initial changes I made to it, which caused one of the checks to fail. I have now properly reverted the code to how it was before and this has been fixed.

As for the test, entering "" causes comp.Instance.Text.Should().Be("0"); to fail, although comp.Instance.Value.Should().Be(0); does pass meaning it changed the value from 5 to 0 when entering nothing. I removed the text check, but please let me know if I should add it back in.

@vernou
Copy link
Contributor

vernou commented Mar 28, 2025

It's important to check Text and Value for each step. Can you reput it? Just adapt the expected.

.Add(p => p.Value, 5));

comp.Find("input").Input("");
comp.Find("input").Blur();

This comment was marked as resolved.

Remove blur from test
comp.Find("input").Input("5");
comp.Find("input").Input("0-");
comp.Instance.Text.Should().Be("-");
comp.Instance.Value.Should().Be(5);

This comment was marked as resolved.

Added test to check entering a negative number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something does not work as intended/expected PR: needs review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants