Skip to content

Commit

Permalink
Merge pull request #2134 from SpareBank1/develop_ffe-file-upload
Browse files Browse the repository at this point in the history
fix(ffe-file-upload-react): accessibility issue
  • Loading branch information
pethel authored Jun 28, 2024
2 parents a71091f + f18635d commit d7b238f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
10 changes: 1 addition & 9 deletions packages/ffe-file-upload-react/src/FileUpload.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { FileUpload, FileUploadProps } from './FileUpload';
import { render, screen, fireEvent } from '@testing-library/react';
import { render, fireEvent } from '@testing-library/react';

const renderFileUpload = (props?: Partial<FileUploadProps<unknown>>) =>
render(
Expand All @@ -23,17 +23,9 @@ const renderFileUpload = (props?: Partial<FileUploadProps<unknown>>) =>
describe('<FileUpload/>', () => {
it('should have a button and input', () => {
const { container } = renderFileUpload();
expect(container.querySelector('#file-upload-button')).toBeTruthy();
expect(container.querySelector('#file-upload')).toBeTruthy();
});

it('should render input label', () => {
renderFileUpload();
expect(
screen.getByRole('button', { name: 'label' }),
).toBeInTheDocument();
});

it('should extract and return files when user finishes selecting files', () => {
const onFilesSelected = jest.fn();
const { container } = renderFileUpload({ onFilesSelected });
Expand Down
42 changes: 26 additions & 16 deletions packages/ffe-file-upload-react/src/FileUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ export function FileUpload<Document>({
uploadTitle,
uploadMicroText,
uploadSubText,
accept,
accept = '*',
onFilesDropped,
onFileDeleted,
onFilesSelected,
}: FileUploadProps<Document>) {
const [isHover, setIsHover] = useState(false);
const fileInputElement = useRef<HTMLInputElement>(null);
const downloadIcon =
'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIGhlaWdodD0iMjAiIHZpZXdCb3g9IjAgLTk2MCA5NjAgOTYwIiB3aWR0aD0iMjAiPjxwYXRoIGQ9Ik00NzkuOC0zNTcuNTM5cS02Lj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YxNiAzLjg0NiA4LjQ2MyAzLjg0NyAzLjg0NiA4LjQ2MyAzLjg0Nmg0NTUuMzgycTQuNjE2IDAgOC40NjMtMy44NDYgMy44NDYtMy44NDcgMy44NDYtOC40NjN2LTc4LjQ2MXEwLTEyLjc2OSA4LjYxNS0yMS4zODR0MjEuMzg0LTguNjE1cTEyLjc2OSAwIDIxLjM4NCA4LjYxNSA4LjYxNiA4LjYxNSA4LjYxNiAyMS4zODR2NzguNDYxcTAgMzAuMzA4LTIxIDUxLjMwOHQtNTEuMzA4IDIxSDI1Mi4zMDlaIi8+PC9zdmc+';

const handleFilesDropped = (event: React.DragEvent<HTMLDivElement>) => {
event.preventDefault();
Expand Down Expand Up @@ -124,9 +124,9 @@ export function FileUpload<Document>({
</div>
</div>
)}
{/* eslint-disable-next-line jsx-a11y/no-static-element-interactions */}
<div
className="ffe-file-upload__upload-section"
role="presentation"
onDrop={handleFilesDropped}
onDragOver={event => {
event.preventDefault();
Expand All @@ -149,27 +149,37 @@ export function FileUpload<Document>({
<div className="ffe-file-upload__upload-section-microtext">
{uploadMicroText}
</div>

<SecondaryButton
leftIcon={<Icon fileUrl={downloadIcon} size="sm" />}
onClick={triggerUploadFileNativeHandler}
id={`${id}-button`}
tabIndex={0}
as="label"
htmlFor={id}
leftIcon={<Icon fileUrl={downloadIcon} size="md" />}
onClick={e => {
e.preventDefault();
triggerUploadFileNativeHandler();
}}
onKeyUp={e => {
if (e.key === 'Enter' || e.key === ' ') {
triggerUploadFileNativeHandler();
}
}}
>
<span id={`${id}-label`}>{label}</span>
{label}
</SecondaryButton>
<input
id={id}
type="file"
multiple={multiple}
ref={fileInputElement}
onChange={handleFileSelected}
accept={accept}
/>
<div className="ffe-file-upload__upload-section-subtext">
{uploadSubText}
</div>
</div>
</div>
<input
id={id}
type="file"
multiple={multiple}
ref={fileInputElement}
onChange={handleFileSelected}
aria-labelledby={`${id}-label`}
accept={accept || '*'}
/>
</div>
);
}

0 comments on commit d7b238f

Please sign in to comment.