Skip to content

fix(file-input): remove value state in file input #501

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

Merged
merged 2 commits into from
May 31, 2025
Merged
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
6 changes: 6 additions & 0 deletions .changeset/chilly-signs-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@tapsioss/web-components": patch
---

Resolve issues related to setting files and value in file-input.

15 changes: 12 additions & 3 deletions packages/web-components/src/file-input/Validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ class FileInputValidator extends Validator<FileInputState> {
if (!this._control) {
// Lazily create the platform input
this._control = document.createElement("input");
this._control.type = "text";
this._control.type = "file";
}

this._control.required = state.required;
this._control.value = state.value;
if (!state.value && state.required) {
const shadowControl = document.createElement("input");

shadowControl.type = "text";
shadowControl.required = true;

return {
validity: this._control.validity,
validationMessage: this._control.validationMessage,
};
}

return {
validity: this._control.validity,
Expand Down
11 changes: 8 additions & 3 deletions packages/web-components/src/file-input/file-input.style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,14 @@ const styles: CSSResult = css`
cursor: pointer;

position: absolute;

inset: 0;
z-index: 1;
width: 1px;
height: 1px;
padding: 0;
margin: -1;
border: 0;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
}

.input:focus-visible + .file-input {
Expand Down
15 changes: 12 additions & 3 deletions packages/web-components/src/file-input/file-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ import {
setupMocks,
test,
} from "@internals/test-helpers";
import type { Locator } from "@playwright/test";
import * as path from "path";
import { ErrorMessages, scope } from "./constants.ts";

describe("🧩 file-input", () => {
const getSelectedFiles = async (fileInput: Locator): Promise<number> => {
return await fileInput
.evaluateHandle((el: HTMLInputElement) => {
return el.files?.length ?? 0;
})
.then(handle => handle.jsonValue());
};

afterEach(async ({ page }) => {
await disposeMocks(page);
});
Expand Down Expand Up @@ -186,7 +195,7 @@ describe("🧩 file-input", () => {
const fileInput = page.getByTestId("test-file-input");

// At first, no files are selected.
await expect(fileInput).toHaveJSProperty("files", null);
expect(await getSelectedFiles(fileInput)).toBe(0);

const fileChooserPromise = page.waitForEvent("filechooser");

Expand All @@ -195,7 +204,7 @@ describe("🧩 file-input", () => {

// if we reach here, it means the file input has been opened!
await fileChooser.setFiles(path.resolve("src", "file-input", "index.ts"));
await expect(fileInput).not.toHaveJSProperty("files", null);
expect(await getSelectedFiles(fileInput)).toBe(1);

// We should show to user the file name when it's not an image.
const previewSection = page.locator("tapsi-file-input .text");
Expand All @@ -214,7 +223,7 @@ describe("🧩 file-input", () => {
const fileInput = page.getByTestId("test-file-input");

// At first, no files are selected.
await expect(fileInput).toHaveJSProperty("files", null);
expect(await getSelectedFiles(fileInput)).toBe(0);

const fileChooserPromise = page.waitForEvent("filechooser");

Expand Down
Loading