-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic import from file support #450
- Loading branch information
Showing
5 changed files
with
109 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import PropTypes from 'prop-types'; | ||
import { memo, useCallback, useId, useRef } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { Button, Icon } from 'web-common/components'; | ||
|
||
import { importFromFile } from '../../../actions'; | ||
import { getFileData } from '../../../common/event'; | ||
|
||
|
||
const ImportAction = ({ disabled, onFocusNext, onFocusPrev, tabIndex }) => { | ||
const dispatch = useDispatch(); | ||
const uploadFileId = useId(); | ||
const fileInputRef = useRef(null); | ||
|
||
const handleKeyDown = useCallback(ev => { | ||
if (ev.target !== ev.currentTarget) { | ||
return; | ||
} | ||
|
||
if (ev.key === 'ArrowRight') { | ||
onFocusNext(ev); | ||
} else if (ev.key === 'ArrowLeft') { | ||
onFocusPrev(ev); | ||
} | ||
}, [onFocusNext, onFocusPrev]); | ||
|
||
const handleImportClick = useCallback(() => { | ||
fileInputRef.current.click(); | ||
}, []); | ||
|
||
const handleFileInputChange = useCallback(async ev => { | ||
const fileData = await getFileData(ev.currentTarget.files[0]); | ||
if (fileData) { | ||
dispatch(importFromFile(fileData)); | ||
} | ||
}, [dispatch]); | ||
|
||
return ( | ||
<div className="btn-file"> | ||
<input | ||
aria-labelledby={uploadFileId} | ||
multiple={false} | ||
onChange={handleFileInputChange} | ||
ref={fileInputRef} | ||
tabIndex={-1} | ||
type="file" | ||
/> | ||
<Button | ||
disabled={disabled} | ||
icon | ||
onClick={handleImportClick} | ||
onKeyDown={handleKeyDown} | ||
tabIndex={tabIndex} | ||
title="Import" | ||
> | ||
<Icon type="16/caret-16" width="16" height="16" /> | ||
</Button> | ||
|
||
</div> | ||
); | ||
} | ||
|
||
ImportAction.propTypes = { | ||
disabled: PropTypes.bool, | ||
onFocusNext: PropTypes.func, | ||
onFocusPrev: PropTypes.func, | ||
tabIndex: PropTypes.number | ||
}; | ||
|
||
export default memo(ImportAction); |