Skip to content

Commit

Permalink
feat: onFileLoaded support returning raw text.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Jul 5, 2023
1 parent b1fc6a3 commit 887492f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
31 changes: 30 additions & 1 deletion core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ npm install @uiw/react-csv-reader
```jsx
import CSVReader from '@uiw/react-csv-reader';

<CSVReader />
<CSVReader
onFileLoaded={(data, iFileInfo, iOriginalFile) => { }}
/>
```


Expand Down Expand Up @@ -118,6 +120,33 @@ export default function Demo() {
}
```

**Get csv raw text content**

The original text can also be obtained using `await iOriginalFile.text()`.

```tsx mdx:preview
import React, { useState } from 'react';
import CSVReader from '@uiw/react-csv-reader';
import JsonView from '@uiw/react-json-view';
import { lightTheme } from '@uiw/react-json-view/light';

export default function Demo() {
const [value, setValue] = useState('');
return (
<React.Fragment>
<CSVReader
onFileLoaded={(data, iFileInfo, iOriginalFile, text) => {
setValue(text);
}}
/>
{value && value.length > 0 && (
<pre>{value}</pre>
)}
</React.Fragment>
);
}
```

## Props

```ts
Expand Down
4 changes: 2 additions & 2 deletions core/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export interface CSVReaderProps<T, TFile extends LocalFile = LocalFile> extends
encoding?: string;
parserOptions?: Partial<ParseWorkerConfig<T>> & Partial<ParseLocalConfig<T>> & ParseConfig<T, TFile>;
onError?: (error: Error) => void;
onFileLoaded: (data: Array<any>, fileInfo: IFileInfo, originalFile?: File) => void;
onFileLoaded: (data: Array<any>, fileInfo: IFileInfo, originalFile?: File, text?: string) => void;
}

const CSVReader = forwardRef<HTMLInputElement, CSVReaderProps<unknown>>((props, ref) => {
Expand Down Expand Up @@ -50,7 +50,7 @@ const CSVReader = forwardRef<HTMLInputElement, CSVReaderProps<unknown>>((props,
error: onError,
encoding: encoding,
} as unknown as CSVReaderProps<any, any>['parserOptions']) as unknown as ParseResult<any>;
onFileLoaded && onFileLoaded(csvData?.data ?? [], fileInfo, files[0]);
onFileLoaded && onFileLoaded(csvData?.data ?? [], fileInfo, files[0], reader.result as string);
}

reader.readAsText(files[0], encoding)
Expand Down
5 changes: 3 additions & 2 deletions www/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ export default function App() {
// header: true,
// worker: true,
}}
onFileLoaded={(data) => {
onFileLoaded={async (data, iFileInfo, iOriginalFile) => {
setValue(data);
console.log('data:', data)
const txt = await iOriginalFile?.text()
console.log('data:', data, iFileInfo, iOriginalFile, txt)
}}
/>
{value && value.length > 0 && (
Expand Down

0 comments on commit 887492f

Please sign in to comment.