Conversation
mohamedhyouns
commented
Dec 12, 2024
- Fix Buggy behaviors in number input #32
- Refactored two-way binding logic to allow number inputs to remain empty. - Added `getRangePercent` function to compute slider percentage values. - Introduced `updateFieldValue` to handle empty or numeric inputs gracefully. - Implemented `sliderStateProxy` to synchronize range and number inputs. - Centralized input handling via `handleInputChange` to simplify event management. - Removed wired `addEventListener` logic to reduce bugs and improve maintainability.
- Added `name` attributes to slider inputs in `index.html` for proxy-based synchronization.
Removed test as the proxy now handles input and range updates automatically.
The current style of file not consistent with vscode reformating style
|
After merging this and updating the DXBR builder with the new slider files, this bug will be valid and need to be fixed |
jjroelofs
left a comment
There was a problem hiding this comment.
I will also comment on the issue but the bug you found is not a bug but a feature, here is why:
- The number input is not a real form element in this use case, it is just a tool that enhances the slider input with the possibility for more accuracy, that is why it also should not have a name attribute, we don't want to submit it separately to the server.
- A range element's value cannot be cleared, "empty" is not a valid state for it. That is why allowing the number input to be empty would be a bug.
- The default value of a range input is to be in the center. When clearing the number input, it slider goes to the center, and the number input assumes the value corresponding to the middle of range slider. This is expected behavior.
|
@jjroelofs Please have a look https://dxpr.monday.com/boards/935035417/pulses/8055708426 |
|
@jjroelofs, I got the idea of resetting both inputs to the middle when trying to clear the input of type number. This has been addressed here.
Since this is an atomic issue addressing multiple bugs, do you suggest releasing it as 3.x? I recorded a video to explain why the existing logic is problematic: https://www.youtube.com/watch?v=B8V_Bw9NK44. |
jjroelofs
left a comment
There was a problem hiding this comment.
This PR is too complicated for me to review without having information about the engineering decisions that were made and why they were made, so I can't accept it without a good commit message and PR description.
Also, I don't like the requirement for a new data attribute. The attribute values added in the examples are identical to the id values with already have so it seems they are redundant. Please see if you can remove this requirement because this change makes implementation more error prone and it is not backwards compatible, meaning we would have to bump the version to 3.x.
dxb-slider.js
Outdated
| return; | ||
| } | ||
|
|
||
| sliderStateProxy[e.target.dataset["dxbProxyKey"]] = e.target.value; |
There was a problem hiding this comment.
Please be consistent, always use dataset.dxbProxyKey for getting and setting values. Don't mix setAttribute("data-attr-name", value) with dataset.attributeName.
Also, you may put the attribute name in a const variable and use it whenever needed instead of hardcoding the same attribute name in all uses.
dxb-slider.js
Outdated
| [rangeInput, numberInput].forEach(input => | ||
| input.addEventListener('input', handleInputChange) | ||
| ); |
There was a problem hiding this comment.
Not sure if it's a good idea to have the same handler. For instance, we need to separate the numberInput handler and introduce a debounce to invoke the handler only after the user stops writing values—i.e., a 500ms debounce.
There was a problem hiding this comment.
a4a1d4c
Adding debounce won't be a good idea, it will affect the UX I tested it with 500 and without denouncing
slider-debouce.mp4
dxb-slider.js
Outdated
| // Reset the input value to the previously stored value if it exceeds the maximum allowed value | ||
| const newValue = Number(e.target.value); | ||
| const max = e.target.max; | ||
|
|
||
| if (max && newValue > e.target.max) { | ||
| e.target.value = sliderStateProxy[e.target.dataset["dxbProxyKey"]] | ||
| } | ||
|
|
There was a problem hiding this comment.
Please hand this over to the proxy not the event listener handler. This when Javascript Proxy are actually used for—i.e., validation.
There was a problem hiding this comment.
Please test your code thoroughly. This is the second instance where easily detectable bugs could have been caught with basic testing.
- For ranges like [-100, 100] with default step, I cannot add negative values (it resets to mid range).
- Value resets are confusing: Sometimes reset to max (i.e., when exceed maximum), reset to mid (i.e., on invalid input or on clear).
- In the 100 step example, putting a fraction of a step value do approximation on the range field while keep the number as is in the input field. Did we decide at some point to do this approximation part? Regardless, there is a mismatch in behavior between the input and range fields.
- We should provide feedback rather than do corrective actions on validation errors. This is the standard on how input fields behaves.
Although you introduce some code enhancements, I have the feeling that the new logic is a bit messy and results in more bugs. I will have to double check your final implementation after you fix the bugs.
- UPDATED: Enhanced min/max value validation to handle empty attributes correctly - UPDATED: Converted `validInput.min` and `validInput.max` to numbers for consistent comparisons - ADDED: Allow temporary entry of negative (`"-"`) and floating point (`"."`) characters before validation - FIXED: Prevent premature value correction while the user is still typing
The slider supports negative value: 7a41514
I agree, but not in all mentioned cases.
I asked ChatGPT about the proper Rounding Formula, and it gave me this 🙂 Seems to work fine for me: 1354a0c
The core implementation doesn't support this at the moment. Adding it would require a new issue, as it involves some complexity and is difficult to incorporate into the current atomic PR. |
- ADDED: `roundToPrecision` function to maintain correct floating-point precision - UPDATED: Ensured number input allows empty values without applying calculations - UPDATED: Added explicit parsing for `min`, `max`, and `step` attributes to prevent unintended behavior - UPDATED: Clamped input values within the allowed range before applying rounding - UPDATED: Improved rounding logic to correctly align with step increments - FIXED: Ensured precision consistency when updating number input values







