Skip to content
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
28 changes: 21 additions & 7 deletions app/javascript/packages/document-capture/components/file-input.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ function FileInput(props, ref) {
const inputId = `file-input-${instanceId}`;
const hintId = `${inputId}-hint`;
const innerHintId = `${hintId}-inner`;
const labelId = `${inputId}-label`;

/**
* In response to a file input change event, confirms that the file is valid before calling
Expand Down Expand Up @@ -194,17 +195,29 @@ function FileInput(props, ref) {
}

/**
* @param {string} fileLabel String velue of the label for input to display
* @param {string} fileLabel String value of the label for input to display
* @param {Blob|string|null|undefined} fileValue File or string for which to generate label.
* @return {{'aria-label': string} | {'aria-labelledby': string}}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This'd be a good file to port to TypeScript, though having just opened the file myself, I see it's a big one, and probably not a task we'd want to undertake here 😅

*/
function getLabelFromValue(fileLabel, fileValue) {
function getAriaLabelPropsFromValue(fileLabel, fileValue) {
if (fileValue instanceof window.File) {
return `${fileLabel} - ${fileValue.name}`;
return {
'aria-label': `${fileLabel} - ${fileValue.name}`,
};
}

if (fileValue) {
return `${fileLabel} - ${t('doc_auth.forms.captured_image')}`;
return {
'aria-label': `${fileLabel} - ${t('doc_auth.forms.captured_image')}`,
};
}
return '';

// When no file is selected, provide a slightly more verbose label
// including the actual <label> contents and the prompt to drag a file or
// choose from a folder.
return {
'aria-labelledby': `${labelId} ${innerHintId}`,
};
}

const shownErrorMessage = errorMessage ?? ownErrorMessage;
Expand Down Expand Up @@ -241,6 +254,7 @@ function FileInput(props, ref) {
*/}
{/* eslint-disable-next-line jsx-a11y/label-has-associated-control */}
<label
id={labelId}
htmlFor={inputId}
className={['usa-label', shownErrorMessage && 'usa-label--error'].filter(Boolean).join(' ')}
>
Expand Down Expand Up @@ -320,13 +334,13 @@ function FileInput(props, ref) {
id={inputId}
className="usa-file-input__input"
type="file"
aria-label={getLabelFromValue(label, value)}
{...getAriaLabelPropsFromValue(label, value)}
aria-busy={isValuePending}
onChange={onChangeIfValid}
onClick={onClick}
onDrop={onDrop}
accept={accept ? accept.join() : undefined}
aria-describedby={hint ? `${innerHintId} ${hintId}` : undefined}
aria-describedby={hint ? hintId : undefined}
/>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,8 @@ describe('document-capture/components/file-input', () => {
it('renders an optional hint', () => {
const { getByLabelText } = render(<FileInput label="File" hint="Must be small" />);

/**
* The second id in the `aria-descrbedby` value will specify the original
* hint.
*/
const input = getByLabelText('File');
const hintId = input.getAttribute('aria-describedby').split(' ')[1];
const hintId = input.getAttribute('aria-describedby');
const hint = document.getElementById(hintId).textContent;

expect(hint).to.equal('Must be small');
Expand Down Expand Up @@ -237,10 +233,10 @@ describe('document-capture/components/file-input', () => {
expect(onChange.getCall(0).args[0]).to.equal(file);
});

it('has a blank aria-label with no input added', () => {
it('has an appropriate 2-part aria-label with no input added', () => {
const { getByLabelText } = render(<FileInput label="File" />);

const queryByAriaLabel = getByLabelText('');
const queryByAriaLabel = getByLabelText('File doc_auth.forms.choose_file_html');

expect(queryByAriaLabel).to.exist();
});
Expand Down