4.0.0 • Published 1 year ago

react-csv-reader v4.0.0

Weekly downloads
24,688
License
MIT
Repository
github
Last release
1 year ago

react-csv-reader

npm version npm Node.js CI a11y axe TypeScript Support

React component that handles csv file input. It handles file input and returns its content as a matrix.

Docs: nzambello.github.io/react-csv-reader

You can try it out in the playground in the docs or in the demo on Codesandbox.

Installation

Install the package with either yarn or npm.

With yarn:

yarn add react-csv-reader

With npm:

npm install --save react-csv-reader

Usage

Basic usage:

import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import CSVReader from 'react-csv-reader'

class App extends Component {
  ...

  render() {
    return (
      <CSVReader onFileLoaded={(data, fileInfo, originalFile) => console.dir(data, fileInfo, originalFile)} />
    )
  }
}

ReactDOM.render(<App />, document.getElementById('root'))

More complex example:

import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import CSVReader from 'react-csv-reader'

class App extends Component {
  ...

  const papaparseOptions = {
    header: true,
    dynamicTyping: true,
    skipEmptyLines: true,
    transformHeader: header =>
      header
        .toLowerCase()
        .replace(/\W/g, '_')
  }

  render() {
    return (
      <CSVReader
        cssClass="csv-reader-input"
        label="Select CSV with secret Death Star statistics"
        onFileLoaded={this.handleForce}
        onError={this.handleDarkSideForce}
        parserOptions={papaparseOptions}
        inputId="ObiWan"
        inputName="ObiWan"
        inputStyle={{color: 'red'}}
      />
    )
  }
}

ReactDOM.render(<App />, document.getElementById('root'))

Parameters

NameTypeDefaultDescription
acceptstring.csv, text/csvFile type accepted by file input.
cssClassstringcsv-reader-inputA CSS class to be applied to the wrapper element.
cssInputClassstringcsv-inputA CSS class to be applied to the <input> element.
cssLabelClassstringcsv-labelA CSS class to be applied to the <label> element.
labelstring, elementIf present, it will be rendered in a <label> to describe input aim.
onFileLoadedfunction(required) The function to be called passing loaded results, see below.
onErrorfunctionError handling function.
parserOptionsobject{}PapaParse configuration object override
inputIdstringreact-csv-reader-inputAn id to be applied to the <input> element.
inputNamestringreact-csv-reader-inputA name attribute to be applied to the <input> element.
inputStyleobject{}Some style to be applied to the <input> element.
fileEncodingstringUTF-8Encoding type of the input file.
disabledbooleanfalseSet input disabled attribute.
strictbooleanfalseThrows error on onError if file type is different from accept.

onFileLoaded

When the file has been loaded, it will be parsed with PapaParse from a CSV formatted text to a matrix of strings or a list of objects (using header option). That parsed data is returned to the parent component with onFileLoaded function (it will be passed as an argument). The second argument to onFileLoaded will be an object with infos about loaded file.

// data: PapaParse.ParseResult.data
// fileInfo: IFileInfo
onFileLoaded: (data: Array<any>, fileInfo: IFileInfo, originalFile: File) => any

For type definitions, see here.

Testing

This packages uses jest for unit tests and snapshot testing.

To run the tests:

yarn test

Automated accessibility tests are run with jest-axe.

Contributions

Please follow our convention on commits format.

4.0.0

1 year ago

3.5.2

2 years ago

3.5.1

2 years ago

3.5.0

2 years ago

3.4.0

3 years ago

3.3.1

3 years ago

3.3.0

3 years ago

3.2.1

3 years ago

3.2.0

3 years ago

3.1.2

3 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.6

4 years ago

3.0.5

4 years ago

3.0.4

4 years ago

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.0

6 years ago

1.0.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago