Skip to content

Commit ca7f933

Browse files
committed
bug #323 [Live] Do not re-render <input file> if input holds unuploaded files (weaverryan)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Live] Do not re-render <input file> if input holds unuploaded files | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | Related to #299 | License | MIT On #299, we're working on proper file upload support for live components. BUT, I think one thing is for sure: if you simply select a file and then (later) the component re-renders for some reason, we do NOT want to "replace" the `<input file` that has the attached file with an empty `<input file`. I've tested locally - testing proved troublesome, due to some differences in how the `.value` attribute seems to work on browser vs the testing environment. Cheers! Commits ------- 28b29e9 dropping 7.2 from CI: it is no longer supported by CI tools 2ded2f2 do not re-render <input file> if input holds unuploaded files
2 parents e3c248e + 28b29e9 commit ca7f933

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

.github/workflows/test-turbo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
runs-on: ubuntu-latest
3434
strategy:
3535
matrix:
36-
php-versions: ['7.2', '7.3', '7.4', '8.0']
36+
php-versions: ['7.3', '7.4', '8.0']
3737
fail-fast: false
3838
name: PHP ${{ matrix.php-versions }} Test on ubuntu-latest
3939

src/LiveComponent/assets/src/normalize_attributes_for_comparison.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,18 @@
66
* if two nodes are identical.
77
*/
88
export function normalizeAttributesForComparison(element: HTMLElement): void {
9-
if (element.value) {
10-
element.setAttribute('value', element.value);
11-
} else if (element.hasAttribute('value')) {
12-
element.setAttribute('value', '');
9+
const isFileInput = element instanceof HTMLInputElement && element.type === 'file';
10+
11+
// don't add value attribute to file inputs
12+
// if a file is selected, but then a re-render happens before it is
13+
// uploaded, we do NOT want to add a value="" attribute because
14+
// this would cause the input to re-render (without the attached file)
15+
if (!isFileInput) {
16+
if (element.value) {
17+
element.setAttribute('value', element.value);
18+
} else if (element.hasAttribute('value')) {
19+
element.setAttribute('value', '');
20+
}
1321
}
1422

1523
Array.from(element.children).forEach((child: HTMLElement) => {

0 commit comments

Comments
 (0)