-
Notifications
You must be signed in to change notification settings - Fork 842
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
Conversation
- 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; |
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.
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
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.
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.
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.
LGTM! Confirmed absence of 0
locally
Preview documentation changes for this PR: https://eui.elastic.co/pr_5268/ |
This reverts commit 81ffe4a.
Preview documentation changes for this PR: https://eui.elastic.co/pr_5268/ |
…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
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 evaluates0 && 0 > 0
to0
instead of tofalse
(WTF, Javascript). When used within a JSX&&
, this turns into an unstyled0
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 ensurenumActiveFiltersDefined
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:
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