Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
5cce2e59
Commit
5cce2e59
authored
Aug 24, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Aug 24, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make FileInput a single Button to improve accessibility
parent
cef29a96
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
35 additions
and
49 deletions
+35
-49
FileInput.tsx
src/sidebar/components/ShareDialog/FileInput.tsx
+27
-30
ImportAnnotations.tsx
src/sidebar/components/ShareDialog/ImportAnnotations.tsx
+1
-0
FileInput-test.js
src/sidebar/components/ShareDialog/test/FileInput-test.js
+7
-19
No files found.
src/sidebar/components/ShareDialog/FileInput.tsx
View file @
5cce2e59
import
{
FileGenericIcon
,
IconButton
,
Input
,
InputGroup
,
}
from
'@hypothesis/frontend-shared'
;
import
{
useId
,
useRef
}
from
'preact/hooks'
;
import
{
Button
,
FileGenericIcon
}
from
'@hypothesis/frontend-shared'
;
import
classnames
from
'classnames'
;
import
{
useRef
,
useState
}
from
'preact/hooks'
;
export
type
FileInputProps
=
{
onFileSelected
:
(
file
:
File
)
=>
void
;
...
...
@@ -16,10 +12,20 @@ export default function FileInput({
disabled
,
}:
FileInputProps
)
{
const
fileInputRef
=
useRef
<
HTMLInputElement
>
(
null
);
const
inputId
=
useId
(
);
const
[
filename
,
setFilename
]
=
useState
<
string
|
null
>
(
null
);
return
(
<>
<
Button
variant=
"custom"
classes=
{
classnames
(
'w-full relative overflow-hidden border rounded'
,
'bg-grey-0 hover:bg-grey-0'
,
)
}
title=
{
filename
?
`${filename}, Select a file`
:
'Select a file'
}
onClick=
{
()
=>
fileInputRef
.
current
?.
click
()
}
disabled=
{
disabled
}
data
-
testid=
"file-input-proxy-button"
>
<
input
ref=
{
fileInputRef
}
accept=
".json"
...
...
@@ -30,31 +36,22 @@ export default function FileInput({
data
-
testid=
"file-input"
onChange=
{
e
=>
{
const
files
=
(
e
.
target
as
HTMLInputElement
)
!
.
files
;
setFilename
(
files
?.[
0
]?.
name
??
null
);
if
(
files
!==
null
&&
files
.
length
>
0
)
{
onFileSelected
(
files
[
0
]);
}
}
}
/>
<
label
htmlFor=
{
inputId
}
>
Select Hypothesis export file:
</
label
>
<
InputGroup
>
<
Input
id=
{
inputId
}
onClick=
{
()
=>
fileInputRef
.
current
?.
click
()
}
readonly
disabled=
{
disabled
}
value=
{
fileInputRef
.
current
?.
files
!
[
0
]?.
name
??
'Select a file'
}
data
-
testid=
"file-input-proxy-input"
classes=
"cursor-pointer"
/>
<
IconButton
title=
"Select a file"
onClick=
{
()
=>
fileInputRef
.
current
?.
click
()
}
icon=
{
FileGenericIcon
}
variant=
"dark"
disabled=
{
disabled
}
data
-
testid=
"file-input-proxy-button"
/>
</
InputGroup
>
</>
<
div
className=
"max-w-full pr-10 truncate"
data
-
testid=
"filename-container"
>
{
filename
??
'Select a file'
}
</
div
>
<
div
className=
"absolute right-0 h-full p-2 border-l bg-grey-2"
>
<
FileGenericIcon
/>
</
div
>
</
Button
>
);
}
src/sidebar/components/ShareDialog/ImportAnnotations.tsx
View file @
5cce2e59
...
...
@@ -143,6 +143,7 @@ function ImportAnnotations({
return
(
<>
<
p
>
Select Hypothesis export file:
</
p
>
<
FileInput
onFileSelected=
{
setFile
}
disabled=
{
busy
}
/>
{
userList
&&
(
<>
...
...
src/sidebar/components/ShareDialog/test/FileInput-test.js
View file @
5cce2e59
...
...
@@ -29,10 +29,10 @@ describe('FileInput', () => {
const
getActualFileInput
=
wrapper
=>
wrapper
.
find
(
'[data-testid="file-input"]'
);
const
getProxyInput
=
wrapper
=>
wrapper
.
find
(
'input[data-testid="file-input-proxy-input"]'
);
const
getProxyButton
=
wrapper
=>
wrapper
.
find
(
'button[data-testid="file-input-proxy-button"]'
);
const
getFilenameContainer
=
wrapper
=>
wrapper
.
find
(
'[data-testid="filename-container"]'
);
const
createInput
=
(
disabled
=
undefined
)
=>
{
const
wrapper
=
mount
(
...
...
@@ -52,10 +52,14 @@ describe('FileInput', () => {
const
firstFile
=
createFile
(
'foo'
);
const
fileInput
=
getActualFileInput
(
wrapper
);
// We display a placeholder/CTA before any file has been selected
assert
.
equal
(
getFilenameContainer
(
wrapper
).
text
(),
'Select a file'
);
fillInputWithFiles
(
fileInput
,
[
firstFile
,
createFile
(
'bar'
)]);
fileInput
.
simulate
(
'change'
);
assert
.
calledWith
(
fakeOnFileSelected
,
firstFile
);
assert
.
equal
(
getFilenameContainer
(
wrapper
).
text
(),
'foo.json'
);
});
it
(
'does not call onFileSelected when input changes with no files'
,
()
=>
{
...
...
@@ -67,15 +71,6 @@ describe('FileInput', () => {
assert
.
notCalled
(
fakeOnFileSelected
);
});
it
(
'forwards click on proxy input to actual file input'
,
()
=>
{
const
wrapper
=
createInput
();
const
proxyInput
=
getProxyInput
(
wrapper
);
proxyInput
.
simulate
(
'click'
);
assert
.
called
(
getActualFileInput
(
wrapper
).
getDOMNode
().
click
);
});
it
(
'forwards click on proxy button to actual file input'
,
()
=>
{
const
wrapper
=
createInput
();
const
proxyButton
=
getProxyButton
(
wrapper
);
...
...
@@ -89,11 +84,9 @@ describe('FileInput', () => {
it
(
'disables all inner components when FileInput is disabled'
,
()
=>
{
const
wrapper
=
createInput
(
disabled
);
const
fileInput
=
getActualFileInput
(
wrapper
);
const
proxyInput
=
getProxyInput
(
wrapper
);
const
proxyButton
=
getProxyButton
(
wrapper
);
assert
.
equal
(
fileInput
.
prop
(
'disabled'
),
disabled
);
assert
.
equal
(
proxyInput
.
prop
(
'disabled'
),
disabled
);
assert
.
equal
(
proxyButton
.
prop
(
'disabled'
),
disabled
);
});
});
...
...
@@ -102,12 +95,7 @@ describe('FileInput', () => {
'should pass a11y checks'
,
checkAccessibility
([
{
content
:
()
=>
mount
(
<
div
>
<
FileInput
onFileSelected
=
{
fakeOnFileSelected
}
/
>
<
/div>
,
),
content
:
()
=>
mount
(
<
FileInput
onFileSelected
=
{
fakeOnFileSelected
}
/>
)
,
},
]),
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment