-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: FilesDropTarget -> TS (#30254)
Co-authored-by: Aleksander Nicacio da Silva <[email protected]>
- Loading branch information
1 parent
bec5a89
commit 7d1524f
Showing
4 changed files
with
130 additions
and
113 deletions.
There are no files selected for viewing
107 changes: 0 additions & 107 deletions
107
packages/livechat/src/components/FilesDropTarget/index.js
This file was deleted.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
packages/livechat/src/components/FilesDropTarget/index.tsx
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,122 @@ | ||
import type { ComponentChildren, Ref } from 'preact'; | ||
import type { TargetedEvent } from 'preact/compat'; | ||
import { useState } from 'preact/hooks'; | ||
import type { JSXInternal } from 'preact/src/jsx'; | ||
|
||
import { createClassName } from '../../helpers/createClassName'; | ||
import styles from './styles.scss'; | ||
|
||
const escapeForRegExp = (string: string) => string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
|
||
type FilesDropTargetProps = { | ||
overlayed?: boolean; | ||
overlayText?: string; | ||
accept?: string; | ||
multiple?: boolean; | ||
className?: string; | ||
style?: JSXInternal.CSSProperties; | ||
children?: ComponentChildren; | ||
inputRef?: Ref<HTMLInputElement>; | ||
onUpload?: (files: File[]) => void; | ||
}; | ||
|
||
export const FilesDropTarget = ({ | ||
overlayed, | ||
overlayText, | ||
accept, | ||
multiple, | ||
className, | ||
style = {}, | ||
children, | ||
inputRef, | ||
onUpload, | ||
}: FilesDropTargetProps) => { | ||
const [dragLevel, setDragLevel] = useState(0); | ||
|
||
const handleDragOver = (event: DragEvent) => { | ||
event.preventDefault(); | ||
}; | ||
|
||
const handleDragEnter = (event: DragEvent) => { | ||
event.preventDefault(); | ||
setDragLevel(dragLevel + 1); | ||
}; | ||
|
||
const handleDragLeave = (event: DragEvent) => { | ||
event.preventDefault(); | ||
setDragLevel(dragLevel - 1); | ||
}; | ||
|
||
const handleDrop = (event: DragEvent) => { | ||
event.preventDefault(); | ||
|
||
if (dragLevel === 0 || !event?.dataTransfer?.files?.length) { | ||
return; | ||
} | ||
|
||
setDragLevel(0); | ||
|
||
handleUpload(event?.dataTransfer?.files); | ||
}; | ||
|
||
const handleInputChange = (event: TargetedEvent<HTMLInputElement>) => { | ||
if (!event?.currentTarget?.files?.length) { | ||
return; | ||
} | ||
|
||
handleUpload(event.currentTarget.files); | ||
}; | ||
|
||
const handleUpload = (files: FileList) => { | ||
if (!onUpload) { | ||
return; | ||
} | ||
|
||
let filteredFiles = Array.from(files); | ||
|
||
if (accept) { | ||
const acceptMatchers = accept.split(',').map((acceptString) => { | ||
if (acceptString.charAt(0) === '.') { | ||
return ({ name }: { name: string }) => new RegExp(`${escapeForRegExp(acceptString)}$`, 'i').test(name); | ||
} | ||
|
||
const matchTypeOnly = /^(.+)\/\*$/i.exec(acceptString); | ||
if (matchTypeOnly) { | ||
return ({ type }: { type: string }) => new RegExp(`^${escapeForRegExp(matchTypeOnly[1])}/.*$`, 'i').test(type); | ||
} | ||
|
||
return ({ type }: { type: string }) => new RegExp(`^s${escapeForRegExp(acceptString)}$`, 'i').test(type); | ||
}); | ||
|
||
filteredFiles = filteredFiles.filter((file) => acceptMatchers.some((acceptMatcher) => acceptMatcher(file))); | ||
} | ||
|
||
if (!multiple) { | ||
filteredFiles = filteredFiles.slice(0, 1); | ||
} | ||
|
||
filteredFiles.length && onUpload(filteredFiles); | ||
}; | ||
|
||
return ( | ||
<div | ||
data-overlay-text={overlayText} | ||
onDragOver={handleDragOver} | ||
onDragEnter={handleDragEnter} | ||
onDragLeave={handleDragLeave} | ||
onDrop={handleDrop} | ||
className={createClassName(styles, 'drop', { overlayed, dragover: dragLevel > 0 }, [className])} | ||
style={style} | ||
> | ||
<input | ||
ref={inputRef} | ||
type='file' | ||
accept={accept} | ||
multiple={multiple} | ||
onChange={handleInputChange} | ||
className={createClassName(styles, 'drop__input')} | ||
/> | ||
{children} | ||
</div> | ||
); | ||
}; |
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