-
Notifications
You must be signed in to change notification settings - Fork 7
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
Introduce custom design of FileInputField
(#244)
#601
base: master
Are you sure you want to change the base?
Changes from all commits
ba65653
601179f
cfe01f0
c779f4f
c1e9d69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
import PropTypes from 'prop-types'; | ||
import React, { useContext } from 'react'; | ||
import React, { | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
import { withGlobalProps } from '../../providers/globalProps'; | ||
import { classNames } from '../../helpers/classNames/classNames'; | ||
import { classNames } from '../../helpers/classNames'; | ||
import { transferProps } from '../../helpers/transferProps'; | ||
import { TranslationsContext } from '../../providers/translations'; | ||
import { getRootSizeClassName } from '../_helpers/getRootSizeClassName'; | ||
import { getRootValidationStateClassName } from '../_helpers/getRootValidationStateClassName'; | ||
import { resolveContextOrProp } from '../_helpers/resolveContextOrProp'; | ||
import { InputGroupContext } from '../InputGroup'; | ||
import { Text } from '../Text'; | ||
import { FormLayoutContext } from '../FormLayout'; | ||
import styles from './FileInputField.module.scss'; | ||
|
||
|
@@ -17,34 +25,122 @@ export const FileInputField = React.forwardRef((props, ref) => { | |
isLabelVisible, | ||
label, | ||
layout, | ||
onChange, | ||
onDragEnter, | ||
onDragLeave, | ||
onDragOver, | ||
onDrop, | ||
required, | ||
size, | ||
validationState, | ||
validationText, | ||
...restProps | ||
} = props; | ||
|
||
const context = useContext(FormLayoutContext); | ||
const formLayoutContext = useContext(FormLayoutContext); | ||
const inputGroupContext = useContext(InputGroupContext); | ||
const translations = useContext(TranslationsContext); | ||
|
||
const [selectedFileNames, setSelectedFileNames] = useState([]); | ||
const [isDragAndDropSupported, setIsDragAndDropSupported] = useState(false); | ||
const [isDragging, setIsDragging] = useState(false); | ||
|
||
const handleFileChange = (files) => { | ||
if (files.length === 0) { | ||
setSelectedFileNames([]); | ||
return; | ||
} | ||
|
||
const fileNames = []; | ||
|
||
[...files].forEach((file) => { | ||
fileNames.push(file.name); | ||
}); | ||
|
||
setSelectedFileNames(fileNames); | ||
}; | ||
|
||
const handleInputChange = (event) => { | ||
handleFileChange(event.target.files); | ||
|
||
if (props?.onChange) { | ||
props.onChange(event); | ||
} | ||
}; | ||
|
||
const handleClick = (event) => { | ||
event.currentTarget.previousElementSibling.click(); | ||
}; | ||
|
||
const handleDrop = (event) => { | ||
event.preventDefault(); | ||
handleFileChange(event.dataTransfer.files); | ||
setIsDragging(false); | ||
|
||
if (props?.onDrop) { | ||
props.onDrop(event); | ||
} | ||
}; | ||
|
||
const handleDragOver = (event) => { | ||
event.preventDefault(); | ||
setIsDragging(true); | ||
|
||
if (props?.onDragOver) { | ||
props.onDragOver(event); | ||
} | ||
}; | ||
|
||
const handleDragEnter = (event) => { | ||
setIsDragging(true); | ||
|
||
if (props?.onDragEnter) { | ||
props.onDragEnter(event); | ||
} | ||
}; | ||
|
||
const handleDragLeave = (event) => { | ||
setIsDragging(false); | ||
|
||
if (props?.onDragLeave) { | ||
props.onDragLeave(event); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
setIsDragAndDropSupported('draggable' in document.createElement('span')); | ||
}, []); | ||
|
||
return ( | ||
<label | ||
className={classNames( | ||
styles.root, | ||
fullWidth && styles.isRootFullWidth, | ||
context && styles.isRootInFormLayout, | ||
resolveContextOrProp(context && context.layout, layout) === 'horizontal' | ||
formLayoutContext && styles.isRootInFormLayout, | ||
resolveContextOrProp(formLayoutContext && formLayoutContext.layout, layout) === 'horizontal' | ||
? styles.isRootLayoutHorizontal | ||
: styles.isRootLayoutVertical, | ||
disabled && styles.isRootDisabled, | ||
resolveContextOrProp(inputGroupContext && inputGroupContext.disabled, disabled) && styles.isRootDisabled, | ||
inputGroupContext && styles.isRootGrouped, | ||
isDragging && styles.isRootDragging, | ||
required && styles.isRootRequired, | ||
getRootSizeClassName( | ||
resolveContextOrProp(inputGroupContext && inputGroupContext.size, size), | ||
styles, | ||
), | ||
getRootValidationStateClassName(validationState, styles), | ||
)} | ||
htmlFor={id} | ||
id={id && `${id}__label`} | ||
onDragEnter={!disabled && isDragAndDropSupported ? handleDragEnter : undefined} | ||
onDragLeave={!disabled && isDragAndDropSupported ? handleDragLeave : undefined} | ||
onDragOver={!disabled && isDragAndDropSupported ? handleDragOver : undefined} | ||
onDrop={!disabled && isDragAndDropSupported ? handleDrop : undefined} | ||
> | ||
<div | ||
className={classNames( | ||
styles.label, | ||
!isLabelVisible && styles.isLabelHidden, | ||
(!isLabelVisible || inputGroupContext) && styles.isLabelHidden, | ||
)} | ||
id={id && `${id}__labelText`} | ||
> | ||
|
@@ -54,12 +150,37 @@ export const FileInputField = React.forwardRef((props, ref) => { | |
<div className={styles.inputContainer}> | ||
<input | ||
{...transferProps(restProps)} | ||
disabled={disabled} | ||
className={styles.input} | ||
disabled={resolveContextOrProp(inputGroupContext && inputGroupContext.disabled, disabled)} | ||
id={id} | ||
onChange={handleInputChange} | ||
ref={ref} | ||
required={required} | ||
tabIndex={-1} | ||
type="file" | ||
/> | ||
<button | ||
className={styles.dropZone} | ||
onClick={handleClick} | ||
type="button" | ||
> | ||
<Text lines={1}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know why but There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is due to pointer events, see my comment: https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop#prevent_the_browsers_default_drag_behavior |
||
{!selectedFileNames.length && ( | ||
<> | ||
<span className={styles.dropZoneLink}>{translations.FileInputField.browse}</span> | ||
{isDragAndDropSupported && ` ${translations.FileInputField.drop}`} | ||
</> | ||
)} | ||
{selectedFileNames.length === 1 && selectedFileNames[0]} | ||
{selectedFileNames.length > 1 && ( | ||
<> | ||
{selectedFileNames.length} | ||
{' '} | ||
{translations.FileInputField.filesSelected} | ||
</> | ||
)} | ||
</Text> | ||
</button> | ||
</div> | ||
{helpText && ( | ||
<div | ||
|
@@ -89,7 +210,13 @@ FileInputField.defaultProps = { | |
id: undefined, | ||
isLabelVisible: true, | ||
layout: 'vertical', | ||
onChange: () => {}, | ||
onDragEnter: () => {}, | ||
onDragLeave: () => {}, | ||
onDragOver: () => {}, | ||
onDrop: () => {}, | ||
required: false, | ||
size: 'medium', | ||
validationState: null, | ||
validationText: null, | ||
}; | ||
|
@@ -134,10 +261,36 @@ FileInputField.propTypes = { | |
* | ||
*/ | ||
layout: PropTypes.oneOf(['horizontal', 'vertical']), | ||
/** | ||
* Callback fired when the value of the input changes. | ||
*/ | ||
onChange: PropTypes.func, | ||
/** | ||
* Callback fired when a drag event enters the field. | ||
*/ | ||
onDragEnter: PropTypes.func, | ||
/** | ||
* Callback fired when a drag event leaves the field. | ||
*/ | ||
onDragLeave: PropTypes.func, | ||
/** | ||
* Callback fired when a drag event is over the field. | ||
*/ | ||
onDragOver: PropTypes.func, | ||
/** | ||
* Callback fired when a file is dropped onto the field. | ||
*/ | ||
onDrop: PropTypes.func, | ||
/** | ||
* If `true`, the input will be required. | ||
*/ | ||
required: PropTypes.bool, | ||
/** | ||
* Size of the field. | ||
* | ||
* Ignored if the component is rendered within `InputGroup` component as the value is inherited in such case. | ||
*/ | ||
size: PropTypes.oneOf(['small', 'medium', 'large']), | ||
/** | ||
* Alter the field to provide feedback based on validation result. | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My suspicion is this function is evaluated many times per second (which corresponds to how often the
dragover
event is being fired). Is it OK?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how
onDragOver
is supposed to behave.I think that
setIsDragging
ishandleDragOver
is not necessary. We should be able to solve it without it just usingonDragEnter
andonDragLeave
. We only have to solve problem thatonDragLeave
is called when dragging over child element.This can be solved with change in
FileInputField.module.scss
:Then
setIsDragging(true);
can be removed fromhandleDragOver
.CAUTION:
handleDragOver
must be present here to prevent default behaviour. See https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API/File_drag_and_drop#prevent_the_browsers_default_drag_behavior