Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(module:select): Null option shows in SelectContent #2023

Merged
merged 10 commits into from
Oct 29, 2021

Conversation

anddrzejb
Copy link
Member

馃 This is a ...

  • New feature
  • Bug fix
  • Site / documentation update
  • Demo update
  • Component style update
  • Bundle size optimization
  • Performance optimization
  • Refactoring
  • Code style optimization
  • Test Case
  • Branch merge
  • Other (about what?)

馃敆 Related issue link

Fixes #1587
Addresses issues mentioned in this comment.
Addresses proposal in this comment.

馃挕 Background and solution

To fix the primary issue, in this PR I added a boolean that identifies if there is a SelectOptionItem that has its Value equal to the default value of TItemValue. I use that flag to figure out what to do when the value is selected and when the Select is cleared. On top of that, it also adds the parameter ValueOnClear which will be used as a primary value when Select is cleared. ValueOnClear may be set to a value that does not exist in SelectOptionItem. In that case, the SelectContent is cleared. Otherwise it is set to the existing SelectOptionItem. I included test cases to cover for these scenarios (I think I cover all possibilities, at least until proven otherwise 馃槒).

During my tests I found out we have some weird issues with Select. It seems it relies a lot on things happening during re-rendering. I am not a fan of this, as it splits responsibilities and is really hard to follow (and thus debug). In my opinion this makes Select more complex & less performant.

馃摑 Changelog

Language Changelog
馃嚭馃嚫 English new [Parameter] ValueOnChange - stored Value that will be used when clear button is pressed
馃嚚馃嚦 Chinese

鈽戯笍 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
  • Changelog is provided or not needed

@github-actions
Copy link

github-actions bot commented Oct 13, 2021

@anranruye
Copy link
Member

Well, I didn't have a look at the codes yet. From your description, I feel this solution makes things more complicated. As we discussed before, the basic logic for Select should be simpler. With no fallback strategy, the Select component accepts all values of TItemValue, if the value is in select-options, then we display that item, otherwise we display nothing or a placeholder identities nothing(null) or a placeholder identities a not-null value which is not in the options. When clear, set the value to default(TItemValue) or ValueOnClear.

With a fallback strategy, when the value is:

  1. value in select-options, display that item, no fallback
  2. null, display a placeholder, no fallback
  3. fallback value defined by the strategy(null, default(TItemValue) or a user specified value), display nothing or a placeholder
  4. other values, fallback

That's all. And for most cases, we don't need a fallback strategy, which makes things as simple as I mentioned in the first paragraph.

@anddrzejb
Copy link
Member Author

I am confused about what do you understand when you say "fallback strategy". Do you mean usage of ValueOnClear? If not, could you elaborate and possibly give an example?

I do not think my solution is that complex. What it does handle is possible later inclusion of an item with its value set to default(TItemValue). And also avoids searching every time for an item that would match default(TItemValue). Without it, there would be an unnecessary performance penalty on every clear button pressed.

I do understand you are probably addressing the conversation we had in PR #1906. I read the whole thing again before I posted this PR. To apply all what we established in that discussion would mean a serious refactoring of the Select component. I don't believe we should attempt that before we have a fairly good test coverage for the Select component. And I am not referring to codecov % but to real scenarios. This PR was meant to fix the current issues we have. I think I did apply to clearing what we established in PR #1906. This should be reflected in the test scenarios I created.

@codecov
Copy link

codecov bot commented Oct 18, 2021

Codecov Report

Merging #2023 (5d8dccc) into master (e698e40) will increase coverage by 2.37%.
The diff coverage is 89.87%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2023      +/-   ##
==========================================
+ Coverage   27.57%   29.94%   +2.37%     
==========================================
  Files         489      512      +23     
  Lines       32243    24635    -7608     
  Branches        0      238     +238     
==========================================
- Hits         8890     7377    -1513     
+ Misses      23353    17222    -6131     
- Partials        0       36      +36     
Impacted Files Coverage 螖
components/select/LabelTemplateItem.razor.cs 0.00% <酶> (酶)
components/select/Properties.cs 0.00% <酶> (酶)
components/select/SelectLocale.cs 0.00% <酶> (酶)
...ents/select/internal/DataSourceEqualityComparer.cs 57.14% <酶> (+7.14%) 猬嗭笍
components/select/internal/SelectContent.razor.cs 57.97% <0.00%> (+4.49%) 猬嗭笍
components/select/internal/SelectModeExtensions.cs 100.00% <酶> (酶)
...ponents/select/internal/SelectOptionGroup.razor.cs 0.00% <酶> (酶)
components/select/internal/SelectOptionItem.cs 82.35% <酶> (+2.55%) 猬嗭笍
components/select/internal/ValueLabel.cs 0.00% <酶> (酶)
components/select/SelectBase.cs 71.11% <90.32%> (+14.32%) 猬嗭笍
... and 434 more

Continue to review full report at Codecov.

Legend - Click here to learn more
螖 = absolute <relative> (impact), 酶 = not affected, ? = missing data
Powered by Codecov. Last update e698e40...5d8dccc. Read the comment docs.

components/select/SelectBase.cs Outdated Show resolved Hide resolved
Copy link
Member

@anranruye anranruye left a comment

Choose a reason for hiding this comment

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

        internal void RemoveEqualityToNoValue(SelectOptionItem<TItemValue, TItem> option)
        {
            if (TypeDefaultExistsAsSelectOption)
            {
                TypeDefaultExistsAsSelectOption = IsOptionEqualToNoValue(option);
            }
        }

I still think here has a mistake.

components/select/SelectBase.cs Outdated Show resolved Hide resolved
@ElderJames ElderJames merged commit acbdc9d into ant-design-blazor:master Oct 29, 2021
@anddrzejb anddrzejb deleted the selectWithNullOption branch October 30, 2021 08:47
@jeffraska
Copy link
Contributor

Hi, I think this PR breaks Two-way binding in Default mode while changing binded value to NULL.

  • When I dont have any special NULL value option in DataSource:
  1. Select.OnValueChange() with NULL value is called.
  2. TItemValue is long? so condition is true and OnInputClearClickAsync() is called.
  3. ClearDefaultMode() is called.
  4. Value Equals to default(long?) so this method just returns and do nothing, SelectedOptionItems is not changed and user still see previous value.
  • When I have special NULL value option in DataSource:
  1. Select.OnValueChange() with NULL value is called.
  2. EvaluateValueChangedOutsideComponent() is called.
  3. ActiveOption is not NULL and Value Equals to default(long?) so this method just returns and do nothing, SelectedOptionItems is not changed and user still see previous value.

Or am I doing something wrong and missing something?

@anranruye
Copy link
Member

@jeffraska Yes, I can reproduce this.

ElderJames pushed a commit that referenced this pull request Apr 23, 2022
* fix(module:select): loads null in SelectOption into SelectContent

fix: adds ValueOnClear
tests

* docs(module:select): add ValueOnChange

* Update components/select/SelectBase.cs

Co-authored-by: Hao Sun <54608128+anranruye@users.noreply.github.com>

* other: clean-up warnings & add .Net foundation header

Co-authored-by: Hao Sun <54608128+anranruye@users.noreply.github.com>
ElderJames pushed a commit that referenced this pull request Apr 30, 2022
* fix(module:select): loads null in SelectOption into SelectContent

fix: adds ValueOnClear
tests

* docs(module:select): add ValueOnChange

* Update components/select/SelectBase.cs

Co-authored-by: Hao Sun <54608128+anranruye@users.noreply.github.com>

* other: clean-up warnings & add .Net foundation header

Co-authored-by: Hao Sun <54608128+anranruye@users.noreply.github.com>
ElderJames pushed a commit that referenced this pull request Sep 6, 2022
* fix(module:select): loads null in SelectOption into SelectContent

fix: adds ValueOnClear
tests

* docs(module:select): add ValueOnChange

* Update components/select/SelectBase.cs

Co-authored-by: Hao Sun <54608128+anranruye@users.noreply.github.com>

* other: clean-up warnings & add .Net foundation header

Co-authored-by: Hao Sun <54608128+anranruye@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SelectOption bug when Value = 0
4 participants