Fix slider zero falsy#21864
Conversation
When the slider is initialized with value 0 and a negative min, the knob is placed at the leftmost position instead of the correct position. This happens because if (value) treats 0 as falsy, falling back to _cachedRate instead of computing the correct rate. Fixes carbon-design-system#21611
|
All contributors have signed the DCO. |
|
I have read the DCO document and I hereby sign the DCO. |
✅ Deploy Preview for v11-carbon-web-components ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for v11-carbon-react ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
| const { max, min, value } = this; | ||
| // Copes with out-of-range value coming programmatically or from `<cds-slider-input>` | ||
| if (value) { | ||
| if (value != null) { |
There was a problem hiding this comment.
Is '' supposed to pass this check?
There was a problem hiding this comment.
Is it better then to do if (value || value === 0) or to do if (value !== null || value !== undefined || value !== '')? Because it looks as if the functionality inside the if statement expects value to be a number but currently it accepts any non falsy value.
There was a problem hiding this comment.
value || value === 0 is the safer change here.
value !== null || value !== undefined || value !== '' would always evaluate to true, so it would not guard anything. Even if that were changed to &&, it would still allow NaN through.
Longer term, after this bug is fixed, this code could probably be improved with Number.isFinite(value) so it only accepts numeric values. That's a separate change though.
Closes #21611
When the slider is initialized with value 0 and a negative min, the knob is placed at the leftmost position instead of the correct position. This happens because if (value) treats 0 as falsy, falling back to _cachedRate instead of computing the correct rate.
Changelog
Changed
Testing / Reviewing
PR Checklist
As the author of this PR, before marking ready for review, confirm you:
Updated documentation and storybook examplesAddressed any impact on accessibility (a11y)More details can be found in the pull request guide