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

[EuiFilterButton] Fix unstyled '0' content when numFilters is undefined #5268

Merged
merged 4 commits into from
Oct 13, 2021

Conversation

cee-chen
Copy link
Contributor

@cee-chen cee-chen commented Oct 13, 2021

Summary

@andreadelrio fixed this in #5012 but I undid her good work in #5061 🤦‍♀️ I thought I was refactoring the original numActiveFilters && numActiveFilters > 0 check to be more succinct, but it turns out JS evaluates 0 && 0 > 0 to 0 instead of to false (WTF, Javascript). When used within a JSX &&, this turns into an unstyled 0 getting printed instead of the desired conditional JSX.

I could also have solved this by coercing the showBadge flag on line 95 to a !! bool, but I thought it would be better to ensure numActiveFiltersDefined always evaluates to a boolean.

I also added a Jest test to ensure regressions don't occur again in the future, and tested the snapshot against the un-fixed code to ensure it failed.

QA

Before:

After:

  • Go to http://localhost:8030/#/forms/filter-group
  • Confirm the 2nd example with composers does not have a badge next to it, and does not have the number 0
  • Confirm clicking and adding a composer still adds the active filter badge in pink

Checklist

- [ ] Check against all themes for compatibility in both light and dark modes
- [ ] Checked in mobile
- [ ] Checked in Chrome, Safari, Edge, and Firefox
- [ ] Props have proper autodocs and playground toggles
- [ ] Added documentation
- [ ] Checked Code Sandbox works for any docs examples

- [ ] Checked for breaking changes and labeled appropriately
- [ ] Checked for accessibility including keyboard-only and screenreader modes

  • A changelog entry exists and is marked appropriately

- showBadge was evaluating as '0' because numActiveFilters was evaluating as 0
- if numActiveFilters was 0 (0 && 0 > 0) validated as 0 :/ just JS things
@@ -68,7 +68,8 @@ export const EuiFilterButton: FunctionComponent<EuiFilterButtonProps> = ({
...rest
}) => {
const numFiltersDefined = numFilters != null; // != instead of !== to allow for null and undefined
const numActiveFiltersDefined = numActiveFilters && numActiveFilters > 0;
const numActiveFiltersDefined =
numActiveFilters != null && numActiveFilters > 0;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

You know what's doubly hilarious in retrospect, undefined > 0 and null > 0 both evaluate to false so there's actually no need for a numActiveFilters && in the first place 💀 I can simplify that if folks prefer here.

I still can't get over 0 && 0 > 0 evaluating to 0 tbh

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't mind keeping it as is. We don't have to think about what undefined > 0 and null > 0 evaluate to with the current logic.

Copy link
Contributor

@thompsongl thompsongl left a comment

Choose a reason for hiding this comment

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

LGTM! Confirmed absence of 0 locally

@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_5268/

@cee-chen cee-chen enabled auto-merge (squash) October 13, 2021 20:28
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_5268/

@cee-chen cee-chen merged commit 2c29fc1 into elastic:master Oct 13, 2021
@cee-chen cee-chen deleted the filter-button-0-fix branch October 13, 2021 21:03
ym pushed a commit to ym/eui that referenced this pull request Oct 29, 2021
…ed (elastic#5268)

* [EuiFilterButton] Fix unstyled '0' content when numFilters is undefined

- showBadge was evaluating as '0' because numActiveFilters was evaluating as 0
- if numActiveFilters was 0 (0 && 0 > 0) validated as 0 :/ just JS things

* Add changelog entry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants