Skip to content
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

[Live] Preserve file input values after render requests #2628

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/LiveComponent/assets/dist/live_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2120,9 +2120,6 @@ class Component {
this.backendRequest.promise.then(async (response) => {
const backendResponse = new BackendResponse(response);
const html = await backendResponse.getBody();
for (const input of Object.values(this.pendingFiles)) {
input.value = '';
}
const headers = backendResponse.response.headers;
if (!headers.get('Content-Type')?.includes('application/vnd.live-component+html') &&
!headers.get('X-Live-Redirect')) {
Expand Down
5 changes: 0 additions & 5 deletions src/LiveComponent/assets/src/Component/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,6 @@ export default class Component {
const backendResponse = new BackendResponse(response);
const html = await backendResponse.getBody();

// clear sent files inputs
for (const input of Object.values(this.pendingFiles)) {
input.value = '';
}

// if the response does not contain a component, render as an error
const headers = backendResponse.response.headers;
if (
Expand Down
39 changes: 39 additions & 0 deletions src/LiveComponent/assets/test/controller/render.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,45 @@ describe('LiveController rendering Tests', () => {
expect((test.queryByDataModel('comment') as HTMLTextAreaElement).value).toEqual('I HAD A GREAT TIME');
});

it('keeps file input value after a render request', async () => {
const test = await createTest(
{},
() => `
<div ${initComponent({}, { debounce: 1 })}>
<input type="file" data-model="file">
<button data-action="live#$render">Reload</button>
</div>
`
);

const fileInput = test.element.querySelector('input[type="file"]') as HTMLInputElement;
const file = new File(['content'], 'test.txt', { type: 'text/plain' });

Object.defineProperty(fileInput, 'files', {
value: [file],
writable: false,
});

// Checks that the file is correctly assigned before rendering
expect(fileInput.files).not.toBeNull();
expect(fileInput.files?.length).toBe(1);
expect(fileInput.files?.[0].name).toBe('test.txt');

// Simulates an AJAX request triggered by Live rendering
test.expectsAjaxCall()
.expectUpdatedData({})
.delayResponse(100);

getByText(test.element, 'Reload').click();

// Checks that the file is still present after rendering
await waitFor(() => {
expect(fileInput.files).not.toBeNull();
expect(fileInput.files?.length).toBe(1);
expect(fileInput.files?.[0].name).toBe('test.txt');
});
});

it('conserves the value of an unmapped field that was modified after a render request', async () => {
const test = await createTest(
{ title: 'greetings' },
Expand Down
Loading