Skip to content

Commit

Permalink
Export UploadButton \w props type, memoize handler
Browse files Browse the repository at this point in the history
  • Loading branch information
arnautov-anton committed Sep 11, 2023
1 parent 1c13e26 commit 8798b63
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/components/ReactFileUtilities/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import type { FileLike } from './types';
import { ChangeEvent, useCallback } from 'react';

export const useHandleFileChangeWrapper = (
resetOnChange = false,
handler?: (files: Array<File>) => void,
) => ({ currentTarget }: React.ChangeEvent<HTMLInputElement>) => {
const { files } = currentTarget;
) =>
useCallback(
({ currentTarget }: ChangeEvent<HTMLInputElement>) => {
const { files } = currentTarget;

if (!files) return;
if (!files) return;

try {
handler?.(Array.from(files));
} catch (error) {
console.error(error);
}
try {
handler?.(Array.from(files));
} catch (error) {
console.error(error);
}

if (resetOnChange) currentTarget.value = '';
};
if (resetOnChange) currentTarget.value = '';
},
[handler, resetOnChange],
);

export function dataTransferItemsHaveFiles(items?: DataTransferItem[]): boolean {
if (!items || !items.length) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ export * from './Tooltip';
export * from './TypingIndicator';
export * from './UserItem';
export * from './Window';

export { UploadButton, UploadButtonProps } from './ReactFileUtilities';

0 comments on commit 8798b63

Please sign in to comment.