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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Select onChange is being fired without explicit selection #24698

Merged
merged 5 commits into from
Jul 24, 2023

Conversation

michael-s-molina
Copy link
Member

@michael-s-molina michael-s-molina commented Jul 14, 2023

SUMMARY

This PR fixes a bug in the Select component related to searched values that were automatically being selected without an explicit selection from the user. This happens because the onChange event is called when a value selection is made or when the input changes. This was only perceived with the introduction of autoClearSearchValue={false} in #23869. This PR changes the logic to only fire the onChange event when there's an actual selection by the user.

TESTING INSTRUCTIONS

I added a test to the component but we need to check the general use of selects in the application because there are wrappers that override the component behavior such as SelectControl and SelectFilterPlugin.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

const [onChangeCount, setOnChangeCount] = useState(0);
const previousChangeCount = usePrevious(onChangeCount, 0);

const fireOnChange = useCallback(
Copy link
Member Author

Choose a reason for hiding this comment

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

Simple counter used to fire the onChange event.

fullSelectOptions.filter(opt => set.has(opt.value)),
);
if (isSingleMode) {
// @ts-ignore
Copy link
Member Author

Choose a reason for hiding this comment

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

The original typescript definition does not allow undefined but I checked and the component allows it when using allowClear.

: option.value,
),
);
if (isSingleMode) {
Copy link
Member Author

@michael-s-molina michael-s-molina Jul 18, 2023

Choose a reason for hiding this comment

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

Single mode was previously using an empty array but it should be undefined.

newValues = mapValues(array, labelInValue);
newOptions = mapOptions(array);
}
onChange?.(newValues, newOptions);
Copy link
Member Author

Choose a reason for hiding this comment

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

No relevant changes to handleOnChange apart from adding useCallback.

onDeselect={handleOnDeselect}
onDropdownVisibleChange={handleOnDropdownVisibleChange}
onPopupScroll={undefined}
onSearch={shouldShowSearch ? handleOnSearch : undefined}
onSelect={handleOnSelect}
onClear={handleClear}
onChange={handleOnChange}
Copy link
Member Author

Choose a reason for hiding this comment

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

onChange is now manually fired when there's an actual change by the user.

label: opt.label,
disabled: opt.disabled,
...opt,
Copy link
Member Author

Choose a reason for hiding this comment

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

Options may contain custom fields and these should be present when mapping.

test('does not fire onChange when searching but no selection', async () => {
const onChange = jest.fn();
render(
<div role="main">
Copy link
Member Author

Choose a reason for hiding this comment

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

This div is to simulate a click outside of the select.

@codecov
Copy link

codecov bot commented Jul 18, 2023

Codecov Report

Merging #24698 (14f2c7b) into master (cb9b865) will increase coverage by 8.88%.
The diff coverage is 74.31%.

❗ Current head 14f2c7b differs from pull request most recent head 6b06ea0. Consider uploading reports for the commit 6b06ea0 to get more accurate results

@@            Coverage Diff             @@
##           master   #24698      +/-   ##
==========================================
+ Coverage   58.35%   67.24%   +8.88%     
==========================================
  Files        1901     1902       +1     
  Lines       73933    73996      +63     
  Branches     8183     8195      +12     
==========================================
+ Hits        43146    49760    +6614     
+ Misses      28666    22117    -6549     
+ Partials     2121     2119       -2     
Flag Coverage Δ
javascript 55.79% <74.12%> (+0.08%) ⬆️
unit ?

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

Impacted Files Coverage Δ
...src/components/ColumnTypeLabel/ColumnTypeLabel.tsx 100.00% <ø> (ø)
...d/packages/superset-ui-chart-controls/src/index.ts 100.00% <ø> (ø)
...-chart-controls/src/sections/advancedAnalytics.tsx 14.28% <ø> (ø)
...rset-ui-chart-controls/src/sections/chartTitle.tsx 100.00% <ø> (ø)
...legacy-plugin-chart-partition/src/controlPanel.tsx 25.00% <ø> (ø)
...gins/legacy-plugin-chart-rose/src/controlPanel.tsx 50.00% <ø> (ø)
...gins/legacy-preset-chart-nvd3/src/NVD3Controls.tsx 100.00% <ø> (ø)
.../BigNumber/BigNumberWithTrendline/controlPanel.tsx 16.66% <ø> (ø)
...s/plugin-chart-echarts/src/Funnel/controlPanel.tsx 66.66% <ø> (ø)
...ns/plugin-chart-echarts/src/Gauge/controlPanel.tsx 66.66% <ø> (ø)
... and 45 more

... and 342 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@michael-s-molina michael-s-molina marked this pull request as ready for review July 19, 2023 13:15
@michael-s-molina michael-s-molina added the v3.0 Label added by the release manager to track PRs to be included in the 3.0 branch label Jul 19, 2023
@michael-s-molina
Copy link
Member Author

@jinghua-qa @sadpandajoe Could you help us test the PR?

Copy link
Member

@geido geido left a comment

Choose a reason for hiding this comment

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

Hey @michael-s-molina thanks for this. I see we are adding a bunch of logic to control how the onChange event is being triggered. Just trying to understand what's the benefit of doing this VS enforcing that the wrapper components use the onSelect / onDeselect handlers instead?

@michael-s-molina
Copy link
Member Author

Hey @michael-s-molina thanks for this. I see we are adding a bunch of logic to control how the onChange event is being triggered. Just trying to understand what's the benefit of doing this VS enforcing that the wrapper components use the onSelect / onDeselect handlers instead?

That's a great question. The difference is that the onChange event works with all selected values at once which is ideal for bulk updates such as Select All.

Co-authored-by: JUST.in DO IT <justin.park@airbnb.com>
@apache apache deleted a comment from github-actions bot Jul 20, 2023
@geido
Copy link
Member

geido commented Jul 21, 2023

/testenv up

@github-actions
Copy link
Contributor

@geido Ephemeral environment spinning up at http://35.92.133.95:8080. Credentials are admin/admin. Please allow several minutes for bootstrapping and startup.

@geido
Copy link
Member

geido commented Jul 21, 2023

@michael-s-molina I think it makes sense to make the autoClearSearchValue an option that can be controlled from the wrappers. I am looking at the Dashboard properties to set owners. I don't think this functionality is very useful in that sense and it might be confusing as well. What do you think? Maybe we can consider that for a future iteration.

I found a small issue. When clicking on the input when there is a search value, the search value disappears. I think we need to control the target of the click.

FCC.New.Coder.Survey.2018.mp4

Another weird aspect of the Select, that has nothing to do with this PR, but that I'd like to point out. When hitting Enter on a new search value, I am expecting that the value will be added to the options, like it happens when there is no other match and the value is new. However, when the search value matches with an already selected value that is focused, hitting Enter not only does not select the search value, but deselects the focused value, which is super confusing.

FCC.New.Coder.Survey.2018.1.mp4

@michael-s-molina
Copy link
Member Author

michael-s-molina commented Jul 21, 2023

@michael-s-molina I think it makes sense to make the autoClearSearchValue an option that can be controlled from the wrappers. I am looking at the Dashboard properties to set owners. I don't think this functionality is very useful in that sense and it might be confusing as well. What do you think? Maybe we can consider that for a future iteration.

I exposed autoClearSearchValue as a property and now it can be overridden as you suggested.

I found a small issue. When clicking on the input when there is a search value, the search value disappears. I think we need to control the target of the click.

This is a bug with Ant Design that was fixed on version 4.24.5 which is some leap from our current 4.10.3 version. Unfortunately, we may delay this fix until Ant Design is upgraded.

@michael-s-molina
Copy link
Member Author

Another weird aspect of the Select, that has nothing to do with this PR, but that I'd like to point out. When hitting Enter on a new search value, I am expecting that the value will be added to the options, like it happens when there is no other match and the value is new. However, when the search value matches with an already selected value that is focused, hitting Enter not only does not select the search value, but deselects the focused value, which is super confusing.

What's happening is that when you select an option, it gets focused and that's why when you press Enter, it gets unselected. If you use the arrow keys after selecting an item, you can see that if you focus a different option and press Enter it will get selected. So it's not related to the search value matching a selected option. One way we could improve this would be to always auto focus the next option after the selected ones but I didn't find a way to programmatically change the focus.

Screen.Recording.2023-07-21.at.11.27.51.mov

Copy link
Member

@geido geido left a comment

Choose a reason for hiding this comment

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

Thanks for all the work on this!

@michael-s-molina michael-s-molina merged commit 6089b5f into apache:master Jul 24, 2023
26 checks passed
@github-actions
Copy link
Contributor

Ephemeral environment shutdown and build artifacts deleted.

michael-s-molina added a commit that referenced this pull request Jul 26, 2023
Co-authored-by: JUST.in DO IT <justin.park@airbnb.com>
(cherry picked from commit 6089b5f)
@mistercrunch mistercrunch added 🍒 3.0.0 🍒 3.0.1 🍒 3.0.2 🍒 3.0.3 🍒 3.0.4 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 3.1.0 labels Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/L v3.0 Label added by the release manager to track PRs to be included in the 3.0 branch 🍒 3.0.0 🍒 3.0.1 🍒 3.0.2 🍒 3.0.3 🍒 3.0.4 🚢 3.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants