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

Fix the bug that accept attribute in the input field didn't work properly #2360

Merged
merged 4 commits into from
Sep 5, 2024
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
5 changes: 5 additions & 0 deletions .changeset/fair-tables-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@lion/ui": patch
---

[input-file] fix the bug that accept attribute in the input field didn't work properly
2 changes: 1 addition & 1 deletion packages/ui/components/input-file/src/FileHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class FileHandle {
*/
// eslint-disable-next-line class-methods-use-this
_getFileNameExtension(fileName) {
return fileName.slice(fileName.lastIndexOf('.') + 1);
return fileName.slice(fileName.lastIndexOf('.'));
}

// TODO: seems to suggest upload is going on...
Expand Down
4 changes: 1 addition & 3 deletions packages/ui/components/input-file/src/LionInputFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export class LionInputFile extends ScopedElementsMixin(LocalizeMixin(LionField))
/** @type {string[]} */
let allowedFileExtensions = [];
if (this.accept) {
const acceptedFiles = this.accept.replace(/\s+/g, '').replace(/\.+/g, '').split(',');
const acceptedFiles = this.accept.replace(/\s+/g, '').split(',');
allowedFileTypes = acceptedFiles.filter(acceptedFile => acceptedFile.includes('/'));
allowedFileExtensions = acceptedFiles.filter(acceptedFile => !acceptedFile.includes('/'));
}
Expand Down Expand Up @@ -692,8 +692,6 @@ export class LionInputFile extends ScopedElementsMixin(LocalizeMixin(LionField))

if (allowedFileExtensions.length) {
array = allowedFileExtensions;
// eslint-disable-next-line no-return-assign
array = array.map(item => (item = `.${item}`));
lastItem = array.pop();
arrayLength = array.length;
} else if (allowedFileTypes.length) {
Expand Down
25 changes: 11 additions & 14 deletions packages/ui/components/input-file/test/lion-input-file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ describe('lion-input-file', () => {
})
);

it('error should not be there when the file extensions are accepted', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was failing before the fix

const el = await fixture(html`
<lion-input-file label="Select" accept=".txt"></lion-input-file>
`);

mimicSelectFile(el, [fileWrongType]);
await el.updateComplete;

expect(el.hasFeedbackFor.length).to.equal(0);
});

it('should not be added to the selected list', async () => {
const el = await fixture(html`
<lion-input-file label="Select" accept="text/plain"></lion-input-file>
Expand Down Expand Up @@ -343,20 +354,6 @@ describe('lion-input-file', () => {
expect(error.message).to.equal('Please select a .jpg, .png or .pdf file with max 500MB.');
});
});

it('error message should add all file extensions to the validator message also works without dots "."', async () => {
const el = await fixture(html`
<lion-input-file label="Select" accept="jpg, png, pdf"></lion-input-file>
`);

mimicSelectFile(el, [fileWrongType]);
await el.updateComplete;

// @ts-expect-error [allow-protected-in-test]
el._selectedFilesMetaData[0].validationFeedback?.forEach(error => {
expect(error.message).to.equal('Please select a .jpg, .png or .pdf file with max 500MB.');
});
});
});

describe('invalid file sizes', async () => {
Expand Down