-
-
Notifications
You must be signed in to change notification settings - Fork 236
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
Add Precision parameter to BitNumberField (#10244) #10261
Add Precision parameter to BitNumberField (#10244) #10261
Conversation
WalkthroughThis change set introduces a new nullable Changes
Sequence Diagram(s)sequenceDiagram
participant UI as User/Input
participant NF as BitNumberField Component
participant NP as Normalization Process
participant EVT as OnValueChanged Event
UI->>NF: Set value and Precision parameter
NF->>NF: Invoke OnSetPrecision (store precision in _precision)
NF->>NP: Call Normalize(value, Precision)
NP-->>NF: Return rounded value
NF->>NF: Execute NormalizeValue (update value if changed)
NF->>EVT: Trigger OnValueChanged event
Poem
Tip ⚡🧪 Multi-step agentic review comment chat (experimental)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (4)
src/BlazorUI/Bit.BlazorUI/Components/Inputs/NumberField/BitNumberField.razor.cs (4)
12-12
: Consider adding XML documentation for private fieldWhile private fields typically don't need documentation, this field is used across multiple methods and could benefit from a brief comment explaining its purpose and default value.
- private int _precision; + /// <summary> + /// Stores the number of decimal places for rounding numeric values. + /// Default is 0 if not explicitly set. + /// </summary> + private int _precision;
648-669
: Consider simplifying precision calculation logicThe regex-based precision calculation is a bit complex and might be hard to maintain. Consider simplifying it for better readability.
private int CalculatePrecision() { - var step = Step ?? _step?.ToString() ?? "1"; - var regex = new Regex(@"[1-9]([0]+$)|\.([0-9]*)"); - if (regex.IsMatch(step) is false) return 0; - - var matches = regex.Matches(step); - if (matches.Count == 0) return 0; - - var groups = matches[0].Groups; - if (groups[1] != null && groups[1].Length != 0) - { - return -groups[1].Length; - } - - if (groups[2] != null && groups[2].Length != 0) - { - return groups[2].Length; - } - - return 0; + var step = Step ?? _step?.ToString() ?? "1"; + + // Check for trailing zeros in whole numbers (e.g., 100, 1000) + if (step.EndsWith("0") && !step.Contains('.')) + { + int zeroCount = 0; + for (int i = step.Length - 1; i >= 0; i--) + { + if (step[i] == '0') + zeroCount++; + else + break; + } + return -zeroCount; // Negative precision for trailing zeros + } + + // Check for decimal places + int decimalPointIndex = step.IndexOf('.'); + if (decimalPointIndex >= 0) + { + return step.Length - decimalPointIndex - 1; + } + + return 0; }
671-680
: Consider adding a check to avoid unnecessary state updatesThe NormalizeValue method could potentially trigger unnecessary state updates. A direct comparison before updating Value would be more efficient.
The implementation already checks for equality and only updates if needed, which is good practice. This prevents unnecessary re-renders.
682-685
: Consider using lambda for simple event handlersFor simple event handlers like this, consider using a lambda expression when subscribing to the event for better readability.
protected override async Task OnInitializedAsync() { - OnValueChanged += HandleOnValueChanged; + OnValueChanged += (sender, args) => NormalizeValue(); if (ValueHasBeenSet is false && DefaultValue is not null) { Value = DefaultValue; } NormalizeValue(); await base.OnInitializedAsync(); } -private void HandleOnValueChanged(object? sender, EventArgs args) -{ - NormalizeValue(); -}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
src/BlazorUI/Bit.BlazorUI.Tests/Components/Inputs/NumberField/BitNumberFieldTests.cs
(1 hunks)src/BlazorUI/Bit.BlazorUI/Components/Inputs/NumberField/BitNumberField.razor.cs
(5 hunks)src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/NumberField/BitNumberFieldDemo.razor
(5 hunks)src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/NumberField/BitNumberFieldDemo.razor.cs
(2 hunks)src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/NumberField/BitNumberFieldDemo.razor.samples.cs
(5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: build and test
🔇 Additional comments (9)
src/BlazorUI/Bit.BlazorUI.Tests/Components/Inputs/NumberField/BitNumberFieldTests.cs (1)
644-644
: Good addition of precision parameter in testThe addition of a precision parameter with a value of 2 aligns with the new functionality being added to the component. This ensures that the test properly validates the rounding behavior when the Enter key is pressed.
src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/NumberField/BitNumberFieldDemo.razor.cs (2)
232-237
: Good parameter documentationThe parameter is well-documented with clear type, default value, and description that explains the purpose of the precision parameter.
494-494
: Good test value for precision demonstrationUsing 3.1415 (π) as the test value is appropriate to demonstrate precision rounding functionality, as it contains enough decimal places to clearly show the effect of rounding.
src/BlazorUI/Bit.BlazorUI/Components/Inputs/NumberField/BitNumberField.razor.cs (2)
216-221
: Good parameter implementation with CallOnSet attributeThe parameter is correctly implemented with XML documentation and the CallOnSet attribute that triggers the OnSetPrecision method when the parameter is set.
693-693
: Correctly unsubscribing from eventGood practice to unsubscribe from the event in the DisposeAsync method to prevent memory leaks.
src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/NumberField/BitNumberFieldDemo.razor (2)
145-151
: Good demo section for the new precision featureThe demo section clearly demonstrates the precision functionality with a concise description and practical example. The label "Rounding to 2 Decimal Places" effectively communicates what the user should expect to see.
153-164
: Properly updated section IDsAll section IDs have been correctly updated to maintain sequential order after the addition of the new Precision section. This ensures proper navigation and linking within the demo page.
Also applies to: 166-166, 181-181, 203-203, 234-234
src/BlazorUI/Demo/Client/Bit.BlazorUI.Demo.Client.Core/Pages/Components/Inputs/NumberField/BitNumberFieldDemo.razor.samples.cs (2)
94-97
: Good addition of the precision parameter exampleThe new example clearly demonstrates the
Precision
parameter functionality with a value set to 2 decimal places, using π (3.1415) as a clear demonstration value that will show the rounding behavior.
99-99
: Consistent renumbering of examplesThe subsequent examples have been correctly renumbered to accommodate the new Example 8, maintaining consistency throughout the file.
Also applies to: 104-104, 107-107, 116-116, 121-121, 130-130, 140-140, 236-236
This closes #10244
Summary by CodeRabbit
New Features
Documentation