-
-
Notifications
You must be signed in to change notification settings - Fork 394
[LiveComponent] Return empty string for data-value="" instead of falling back to null
#3031
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
Conversation
data-value="" instead of falling back to nulldata-value="" instead of falling back to null
📊 Packages dist files size differenceℹ️ No difference in dist packagesFiles. |
|
👍 It would indeed be consistent with Stimulus values https://stimulus.hotwired.dev/reference/values#getters Could you add some tests to prevent any future regression ? |
data-value="" instead of falling back to nulldata-value="" instead of falling back to null
data-value="" instead of falling back to nulldata-value="" instead of falling back to null
|
@smnandre I added some tests. I see that some tests failed, but seems not related with the changes. Can you confirm please? |
|
It does not feel related to me.. @Kocal wdyt ? |
|
No that's not related, it should be green after a rebase |
0a47fc7 to
208ee9a
Compare
…lling back to null
208ee9a to
d0c0670
Compare
|
Thank you @mercuryseries. |
Problem
Demo video
before.mov
When using
data-value=""on an element, the current implementation incorrectly returnsnullinstead of the intended empty string value. This happens because the conditionif (element.dataset.value)evaluates tofalsefor empty strings (since empty strings are falsy in JavaScript), causing the function to fall through to the finalreturn null;statement.Impact
This behavior prevents developers from explicitly setting empty string values via the
data-valueattribute, which is a common use case for clearing form fields or resetting values to empty states rather thannull.Current behavior:
Expected behavior:
After (demo video)
after.mov
Changes
Replace the truthiness check
if (element.dataset.value)with an explicit attribute existence checkif (element.hasAttribute("data-value")). This ensures that anydata-valueattribute, including those with empty string values, are properly processed and returned.Manual testing done
data-value=""now returns""data-value="test"still returns"test"data-valueattribute still falls back to other value sources