You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bug #3031 [LiveComponent] Return empty string for data-value="" instead of falling back to null (mercuryseries)
This PR was squashed before being merged into the 2.x branch.
Discussion
----------
[LiveComponent] Return empty string for `data-value=""` instead of falling back to null
| Q | A
| ------------- | ---
| Bug fix? | yes
| New feature? | no <!-- please update src/**/CHANGELOG.md files -->
| Docs? | no <!-- required for new features -->
| Issues | # <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License | MIT
## Problem
<details>
<summary><b>Demo video</b></summary>
https://github.com/user-attachments/assets/31bba6af-8690-4cd9-a5c4-20a561342626
</details>
```html
<input type="text" data-model="post.title">
<button
type="button"
data-model="post.title"
data-value="" <!-- HERE -->
data-action="live#update"
>
Clear Title
</button>
```
When using `data-value=""` on an element, the current implementation incorrectly returns `null` instead of the intended empty string value. This happens because the condition `if (element.dataset.value)` evaluates to `false` for empty strings (since empty strings are falsy in JavaScript), causing the function to fall through to the final `return null;` statement.
## Impact
This behavior prevents developers from explicitly setting empty string values via the `data-value` attribute, which is a common use case for clearing form fields or resetting values to empty states rather than `null`.
### Current behavior:
```html
<button data-model="title" data-value="" data-action="live#update">Clear</button>
<!-- Results in: null -->
```
### Expected behavior:
```html
<button data-model="title" data-value="" data-action="live#update">Clear</button>
<!-- Should result in: "" -->
```
<details>
<summary><b>After (demo video)</b></summary>
https://github.com/user-attachments/assets/bad5f5f3-1104-4102-a2bc-30264bb3c5bf
</details>
## Changes
Replace the truthiness check `if (element.dataset.value)` with an explicit attribute existence check `if (element.hasAttribute("data-value"))`. This ensures that any `data-value` attribute, including those with empty string values, are properly processed and returned.
## Manual testing done
- ✅ `data-value=""` now returns `""`
- ✅ `data-value="test"` still returns `"test"`
- ✅ Missing `data-value` attribute still falls back to other value sources
- ✅ All existing functionality remains intact
Commits
-------
d0c0670 [LiveComponent] Return empty string for `data-value=""` instead of falling back to null
0 commit comments