3.0.2 • Published 6 years ago

@vtex/react-csv-parse v3.0.2

Weekly downloads
1,717
License
MIT
Repository
github
Last release
6 years ago

React Csv Parse

Goal: Parse content of a csv file.

Example

From:

Account,Balance,Document,Document Type,Limit,Description,Email
acc1,0,3i563784658,cpf,2000,,k@email.com
acc2,10,3468723468,cpf,10000,Some text,j@email.com

To:

[
  {
    account: "acc1",
    balance: "0",
    document: "3i563784658",
    documentType: "cpf",
    limit: "2000",
    description: "",
    email: "k@gmail.com"
  },
  {
    account: "acc2",
    balance: "10",
    document: "3468723468",
    documentType: "cpf",
    limit: "10000",
    description: "Some text",
    email: "j@email.com"
  }
]

Given the following keys:

const keys = [
  'account',
  'balance',
  'document',
  'documentType',
  'limit',
  'description',
  'email'
]

Inspiration: paypal/downshift

Development structure: github.com/insin/nwb

Development

ActionCommand
Installnpm i -g nwb & npm i
Startnpm start
Buildnwb build
Local testnpm pack
Publish to npmnpm publish --access public

Usage

npm install @vtex/react-csv-parse --save
import CsvParse from '@vtex/react-csv-parse'
handleData = data => {
  this.setState({ data })
}
render() {
  const keys = [
    "header1",
    "header2",
    "header3",
    "header4",
    "header5",
  ]

  return (
    <CsvParse
      keys={keys}
      onDataUploaded={this.handleData}
      onError={this.handleError}
      render={onChange => <input type="file" onChange={onChange} />}
    />
  )
}

CsvParse is the only component. It doesn't render anything itself, it just calls the child function and renders that. Wrap everything in <CsvParse>{/* your function here! */}</CsvParse>.

Props

Prop nameTypeDefaultRequiredDescription
keysarraytrueThe keys used to create the objects.
onDataUploadedfunctrueCallback function with the data parsed as parameter.
onErrorfuncfalseCallback function with the following data: { err, file, inputElem, reason }.

Data split rules

Based on Papaparse.

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.0-beta.1

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.3-beta.3

7 years ago

1.0.3-beta.2

7 years ago

1.0.3-beta.1

7 years ago