Skip to content

Commit b2a1e59

Browse files
committed
add generic element type for div or input element type
1 parent 7f34851 commit b2a1e59

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/useCSVReader.tsx

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ import Remove, { Props as RemoveComponentProps } from './Remove';
3131
// 'application/vnd.ms-excel' for Window 10
3232
const DEFAULT_ACCEPT = 'text/csv, .csv, application/vnd.ms-excel';
3333

34-
export interface Props<T> {
34+
export interface Props<T, E> {
3535
children: (fn: {
36-
getRootProps: () => HTMLAttributes<HTMLDivElement>;
36+
getRootProps: () => HTMLAttributes<E>;
3737
acceptedFile: File;
3838
ProgressBar: React.ComponentType<ProgressBarComponentProps>;
39-
getRemoveFileProps: () => HTMLAttributes<HTMLDivElement>;
39+
getRemoveFileProps: () => HTMLAttributes<E>;
4040
Remove: React.ComponentType<RemoveComponentProps>;
4141
}) => void | React.ReactNode;
4242
accept?: string;
@@ -69,7 +69,10 @@ export interface ProgressBarComponentProps {
6969
className?: string;
7070
}
7171

72-
function useCSVReaderComponent<T = void>() {
72+
function useCSVReaderComponent<
73+
T = void,
74+
E extends HTMLDivElement | HTMLButtonElement = HTMLDivElement
75+
>() {
7376
const CSVReaderComponent = ({
7477
children,
7578
accept = DEFAULT_ACCEPT,
@@ -91,9 +94,9 @@ function useCSVReaderComponent<T = void>() {
9194
onDragEnter,
9295
onDragOver,
9396
onDragLeave,
94-
}: Props<T>) => {
97+
}: Props<T, E>) => {
9598
const inputRef = useRef<HTMLInputElement>(null);
96-
const rootRef = useRef<HTMLDivElement>(null);
99+
const rootRef = useRef<E>(null);
97100
const dragTargetsRef = useRef([]);
98101

99102
const [state, dispatch] = useReducer(reducer, initialState);
@@ -518,14 +521,14 @@ function useCSVReaderComponent<T = void>() {
518521
// refKey = rootRef,
519522
...rest
520523
}: {
521-
onClick?: MouseEventHandler<HTMLDivElement>;
522-
onDrop?: DragEventHandler<HTMLDivElement>;
523-
onDragEnter?: DragEventHandler<HTMLDivElement>;
524-
onDragLeave?: DragEventHandler<HTMLDivElement>;
525-
onDragOver?: DragEventHandler<HTMLDivElement>;
526-
onKeyDown?: KeyboardEventHandler<HTMLDivElement>;
527-
onFocus?: FocusEventHandler<HTMLDivElement>;
528-
onBlur?: FocusEventHandler<HTMLDivElement>;
524+
onClick?: MouseEventHandler<E>;
525+
onDrop?: DragEventHandler<E>;
526+
onDragEnter?: DragEventHandler<E>;
527+
onDragLeave?: DragEventHandler<E>;
528+
onDragOver?: DragEventHandler<E>;
529+
onKeyDown?: KeyboardEventHandler<E>;
530+
onFocus?: FocusEventHandler<E>;
531+
onBlur?: FocusEventHandler<E>;
529532
} = {}) => ({
530533
onClick: composeHandler(composeEventHandlers(onClick, onClickCb)),
531534
onDrop: composeDragHandler(composeEventHandlers(onDrop, onDropCb)),
@@ -634,13 +637,16 @@ function useCSVReaderComponent<T = void>() {
634637
const CSVReader = useMemo(
635638
() => CSVReaderComponent,
636639
[],
637-
) as React.ComponentType<Props<T>>;
640+
) as React.ComponentType<Props<T, E>>;
638641

639642
return CSVReader;
640643
}
641644

642-
export function useCSVReader<T = void>() {
643-
const CSVReader = useCSVReaderComponent<T>();
645+
export function useCSVReader<
646+
T = void,
647+
E extends HTMLDivElement | HTMLButtonElement = HTMLDivElement
648+
>() {
649+
const CSVReader = useCSVReaderComponent<T, E>();
644650

645651
return {
646652
CSVReader,

0 commit comments

Comments
 (0)