Skip to content

Commit 76e1266

Browse files
committed
Refactor useCSVReader
1 parent 3f30ea0 commit 76e1266

File tree

2 files changed

+74
-3
lines changed

2 files changed

+74
-3
lines changed

src/model.tsx

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,78 @@ import { ParseConfig, ParseResult, Parser } from 'papaparse';
33
// 5.3 => https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/papaparse/index.d.ts
44
// 5.2 => https://github.com/DefinitelyTyped/DefinitelyTyped/blob/d3737ebd9125505f7ea237b9f17f1426579a3917/types/papaparse/index.d.ts
55

6-
export interface CustomConfig<T = any, TInput = undefined>
6+
export interface CSVReaderConfig<T = any, TInput = undefined>
7+
extends ParseConfig<T, TInput> {
8+
/**
9+
* * * * * * * * * *
10+
* ParseAsyncConfig
11+
* * * * * * * * * *
12+
*/
13+
14+
/**
15+
* Whether or not to use a worker thread.
16+
* Using a worker will keep your page reactive, but may be slightly slower.
17+
* @default false
18+
*/
19+
worker?: boolean | undefined;
20+
/**
21+
* Overrides `Papa.LocalChunkSize` and `Papa.RemoteChunkSize`.
22+
*/
23+
chunkSize?: number | undefined;
24+
/**
25+
* A callback function, identical to `step`, which activates streaming.
26+
* However, this function is executed after every chunk of the file is loaded and parsed rather than every row.
27+
* Works only with local and remote files.
28+
* Do not use both `chunk` and `step` callbacks together.
29+
*/
30+
chunk?(results: ParseResult<T>, parser: Parser): void;
31+
/**
32+
* A callback to execute if FileReader encounters an error.
33+
* The function is passed two arguments: the error and the File.
34+
*/
35+
error?(error: Error, file: TInput): void;
36+
37+
// ParseLocalConfig
38+
/** The encoding to use when opening local files. If specified, it must be a value supported by the FileReader API. */
39+
encoding?: string | undefined;
40+
41+
/**
42+
* * * * * * * * * * *
43+
* ParseRemoteConfig
44+
* * * * * * * * * * *
45+
*/
46+
47+
/**
48+
* This indicates that the string you passed as the first argument to `parse()`
49+
* is actually a URL from which to download a file and parse its contents.
50+
*/
51+
// download: true;
52+
download?: boolean | true; // default: false
53+
/**
54+
* If defined, should be an object that describes the headers.
55+
* @example { 'Authorization': 'token 123345678901234567890' }
56+
* @default undefined
57+
*/
58+
downloadRequestHeaders?: { [headerName: string]: string } | undefined;
59+
/**
60+
* Use POST request on the URL of the download option. The value passed will be set as the body of the request.
61+
* @default undefined
62+
*/
63+
downloadRequestBody?:
64+
| Blob
65+
| BufferSource
66+
| FormData
67+
| URLSearchParams
68+
| string
69+
| undefined;
70+
/**
71+
* A boolean value passed directly into XMLHttpRequest's "withCredentials" property.
72+
* @default undefined
73+
*/
74+
withCredentials?: boolean | undefined;
75+
}
76+
77+
export interface ReadStringConfig<T = any, TInput = undefined>
778
extends ParseConfig<T, TInput> {
879
/**
980
* * * * * * * * * *

src/useCSVReader.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, {
88
useRef,
99
} from 'react';
1010
import PapaParse, { ParseResult } from 'papaparse';
11-
import { CustomConfig } from './model';
11+
import { CSVReaderConfig } from './model';
1212
import {
1313
composeEventHandlers,
1414
isIeOrEdge,
@@ -30,7 +30,7 @@ const DEFAULT_ACCEPT = 'text/csv, .csv, application/vnd.ms-excel';
3030
export interface Props<T> {
3131
children: (fn: any) => void | ReactNode;
3232
accept?: string;
33-
config?: CustomConfig<T>;
33+
config?: CSVReaderConfig<T>;
3434
minSize?: number;
3535
maxSize?: number;
3636
maxFiles?: number;

0 commit comments

Comments
 (0)