Skip to content

refactor(DisplayBase): add IsNullable method#8093

Merged
ArgoZhang merged 3 commits into
mainfrom
refactor-nullable
Jun 7, 2026
Merged

refactor(DisplayBase): add IsNullable method#8093
ArgoZhang merged 3 commits into
mainfrom
refactor-nullable

Conversation

@ArgoZhang

@ArgoZhang ArgoZhang commented Jun 7, 2026

Copy link
Copy Markdown
Member

Link issues

fixes #8092

Summary By Copilot

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Introduce a shared nullable-type check in DisplayBase and update consumers to use it.

Enhancements:

  • Add a protected IsNullable helper in DisplayBase to centralize nullable generic type detection.
  • Refine clearable logic in select- and autofill-related components to rely on both value-type checks and the shared IsNullable helper.
  • Update validation, date/time picker, numeric input, radio lists, and select components to use the new IsNullable helper instead of directly inspecting NullableUnderlyingType.

Tests:

  • Remove the IsNullable behavior unit test for Select now that nullable detection is handled via DisplayBase.

Copilot AI review requested due to automatic review settings June 7, 2026 07:03
@bb-auto bb-auto Bot added the enhancement New feature or request label Jun 7, 2026
@bb-auto bb-auto Bot added this to the v10.7.0 milestone Jun 7, 2026
@sourcery-ai

sourcery-ai Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Refactors nullable-type handling for display-based components by centralizing an IsNullable() helper in DisplayBase and updating multiple components to rely on it, while simplifying clearability logic and improving consistency in how nullable generic value types are treated. The Select-specific reflection-based unit test for IsNullable is removed as part of this refactor.

File-Level Changes

Change Details Files
Introduce DisplayBase.IsNullable() helper and reuse it across components to standardize nullable generic handling.
  • Add protected bool IsNullable() that returns NullableUnderlyingType != null in DisplayBase.
  • Replace direct NullableUnderlyingType checks with IsNullable() in ValidateBase, DateTimePicker, InputNumber, RadioList, RadioListGeneric, Select, and SelectGeneric.
  • Align behavior where nullable TValue influences validation, null-item generation, placeholder items, AllowNull flags, and clearing logic.
src/BootstrapBlazor/Components/Display/DisplayBase.cs
src/BootstrapBlazor/Components/Validate/ValidateBase.cs
src/BootstrapBlazor/Components/DateTimePicker/DateTimePicker.razor.cs
src/BootstrapBlazor/Components/InputNumber/BootstrapInputNumber.razor.cs
src/BootstrapBlazor/Components/Radio/RadioList.razor.cs
src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor.cs
src/BootstrapBlazor/Components/Select/Select.razor.cs
src/BootstrapBlazor/Components/SelectGeneric/SelectGeneric.razor.cs
Unify clear-button availability logic in AutoFill and SelectBase to rely on shared IsNullable semantics instead of duplicating nullable checks.
  • Remove private IsNullable() implementations in AutoFill and SelectBase that duplicated value-type/nullable checks.
  • Update GetClearable() in AutoFill and SelectBase to combine the existing !ValueType.IsValueType check with the base IsNullable().
src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs
src/BootstrapBlazor/Components/Select/SelectBase.cs
Remove the Select IsNullable reflection-based unit test that relied on a component-specific private IsNullable implementation.
  • Delete IsNullable_Ok test verifying Select IsNullable behavior for various TValue shapes.
  • Delete the private test helper that used reflection to invoke the private IsNullable method on Select instances.
test/UnitTest/Components/SelectTest.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#8092 Add an IsNullable method on DisplayBase that determines whether TValue is a Nullable value type
#8092 Refactor components that need nullable-detection (e.g., Select, AutoFill, Validate, DateTimePicker, InputNumber, RadioList) to use DisplayBase.IsNullable instead of duplicating nullable checks

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs" line_range="244" />
<code_context>
-    private bool IsNullable() => !ValueType.IsValueType || NullableUnderlyingType != null;
-
-    private bool GetClearable() => IsClearable && !IsDisabled && IsNullable();
+    private bool GetClearable() => IsClearable && !IsDisabled && (!ValueType.IsValueType || IsNullable());

     private async Task OnClickItem(TValue val)
</code_context>
<issue_to_address>
**suggestion:** Avoid duplicating null-assignability logic across components

Both `AutoFill` and `SelectBase` now inline `(!ValueType.IsValueType || IsNullable())`. Since this is effectively "can `TValue` be cleared to null", consider a shared helper on the base class (e.g. `protected bool IsNullAssignable() => !ValueType.IsValueType || IsNullable();`) to remove duplication and keep future nullability changes centralized.

Suggested implementation:

```csharp
    private bool GetClearable() => IsClearable && !IsDisabled && IsNullAssignable();

```

To fully implement the suggestion and remove duplication:

1. Add a shared helper on the common base class for `AutoFill` and `SelectBase` (e.g. wherever `ValueType` and `IsNullable()` are currently defined):

```csharp
protected bool IsNullAssignable() => !ValueType.IsValueType || IsNullable();
```

2. Update `SelectBase` (or any other components that currently inline `(!ValueType.IsValueType || IsNullable())`) to call `IsNullAssignable()` instead of duplicating the logic.

3. Ensure `IsNullable()` remains implemented in the base class (or in a place accessible to both `AutoFill` and `SelectBase`) so that `IsNullAssignable()` compiles correctly.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

private bool IsNullable() => !ValueType.IsValueType || NullableUnderlyingType != null;

private bool GetClearable() => IsClearable && !IsDisabled && IsNullable();
private bool GetClearable() => IsClearable && !IsDisabled && (!ValueType.IsValueType || IsNullable());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Avoid duplicating null-assignability logic across components

Both AutoFill and SelectBase now inline (!ValueType.IsValueType || IsNullable()). Since this is effectively "can TValue be cleared to null", consider a shared helper on the base class (e.g. protected bool IsNullAssignable() => !ValueType.IsValueType || IsNullable();) to remove duplication and keep future nullability changes centralized.

Suggested implementation:

    private bool GetClearable() => IsClearable && !IsDisabled && IsNullAssignable();

To fully implement the suggestion and remove duplication:

  1. Add a shared helper on the common base class for AutoFill and SelectBase (e.g. wherever ValueType and IsNullable() are currently defined):
protected bool IsNullAssignable() => !ValueType.IsValueType || IsNullable();
  1. Update SelectBase (or any other components that currently inline (!ValueType.IsValueType || IsNullable())) to call IsNullAssignable() instead of duplicating the logic.

  2. Ensure IsNullable() remains implemented in the base class (or in a place accessible to both AutoFill and SelectBase) so that IsNullAssignable() compiles correctly.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR centralizes nullable value-type detection by introducing a shared IsNullable() helper on DisplayBase<TValue>, and updates multiple components to use it instead of directly checking NullableUnderlyingType.

Changes:

  • Add DisplayBase<TValue>.IsNullable() to encapsulate Nullable<T> detection.
  • Refactor select, radio, validation, datetime picker, and numeric input components to use IsNullable() in their nullable-specific logic.
  • Remove a select unit test that was asserting the old (broader) nullability behavior via reflection.

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.

Show a summary per file
File Description
test/UnitTest/Components/SelectTest.cs Removes reflection-based unit test for IsNullable behavior.
src/BootstrapBlazor/Components/Validate/ValidateBase.cs Uses IsNullable() for empty-string parsing and parsing-failure validation gating.
src/BootstrapBlazor/Components/SelectGeneric/SelectGeneric.razor.cs Uses IsNullable() to decide enum placeholder/null-item behavior.
src/BootstrapBlazor/Components/Select/SelectBase.cs Removes local IsNullable and updates clearable logic to use IsNullable() plus reference-type check.
src/BootstrapBlazor/Components/Select/Select.razor.cs Uses IsNullable() to decide enum placeholder/null-item behavior.
src/BootstrapBlazor/Components/Radio/RadioListGeneric.razor.cs Uses IsNullable() to decide auto null-item insertion for nullable enums.
src/BootstrapBlazor/Components/Radio/RadioList.razor.cs Uses IsNullable() to decide auto null-item insertion for nullable enums.
src/BootstrapBlazor/Components/InputNumber/BootstrapInputNumber.razor.cs Uses IsNullable() to decide when to clear the input on blur for nullable types.
src/BootstrapBlazor/Components/Display/DisplayBase.cs Adds the new protected IsNullable() helper (Nullable detection).
src/BootstrapBlazor/Components/DateTimePicker/DateTimePicker.razor.cs Sets AllowNull based on IsNullable().
src/BootstrapBlazor/Components/AutoFill/AutoFill.razor.cs Removes local IsNullable and updates clearable logic to use IsNullable() plus reference-type check.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov

codecov Bot commented Jun 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5c410ce) to head (c53f9ed).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #8093   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          766       766           
  Lines        34206     34204    -2     
  Branches      4697      4695    -2     
=========================================
- Hits         34206     34204    -2     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@ArgoZhang ArgoZhang merged commit a80e82b into main Jun 7, 2026
4 checks passed
@ArgoZhang ArgoZhang deleted the refactor-nullable branch June 7, 2026 07:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(DisplayBase): add IsNullable method

2 participants