3.1.0 • Published 4 months ago

react-csv-downloader v3.1.0

Weekly downloads
8,020
License
MIT
Repository
github
Last release
4 months ago

React CSV Downloader

Renovate badge CircleCI Status Dependency Status devDependency Status

A simple react component to allow download CSV file from js object

Installation

npm install --save react-csv-downloader

Usage

Use with children component

import CsvDownloader from 'react-csv-downloader';
<CsvDownloader>
  <button>Download</button>
</CsvDownloader>;

Use without children component

<CsvDownloader text="Download" />

Datas

pass the downloaded datas as a component prop

const datas = [
  {
    cell1: 'row 1 - cell 1',
    cell2: 'row 1 - cell 2',
  },
  {
    cell1: 'row 2 - cell 1',
    cell2: 'row 2 - cell 2',
  },
];

<CsvDownloader datas={datas} />;

Datas (on demand with async function resolver)

pass a function to compute datas to be downloaded

const asyncFnComputeDate = () => {
  // do whatever you need async
  return Promise.resolve([
    {
      cell1: 'row 1 - cell 1',
      cell2: 'row 1 - cell 2',
    },
    {
      cell1: 'row 2 - cell 1',
      cell2: 'row 2 - cell 2',
    },
  ]);
};

<CsvDownloader datas={asyncFnComputeDate} />;

Column

pass the columns definition as a component prop to change the cell display name. If column isn't passed the cell display name is automatically defined with datas keys

const columns = [
  {
    id: 'cell1',
    displayName: 'Cell 1',
  },
  {
    id: 'cell2',
    displayName: 'Cell 2',
  },
];

<CsvDownloader columns={columns} />;

You can also use the columns definition to set the columns display order

Props

NameTypeDefaultRequiredDescription
columnsarray of objectnullfalseColumns definition
datasarray of object/Func/PromisenulltrueDownloaded datas or a Promise or a function that can resolve data on demand (async)
filenamestringnulltrueYou can pass the filename without extension. The extension is automatically added
extensionstring'.csv'falseYou can pass the file extension, note that it will affect filename
separatorstring','falseColumns separator
noHeaderbooleanfalsefalseIf true the header isn't added to the csv file
prefixstring or booleanfalsefalseFilename prefix. If true prefix becomes a timestamp
suffixstring or booleanfalsefalseFilename suffix/postfix. If true suffix becomes a timestamp
textstringnullfalseDownload button text. Used if no children component.
wrapColumnCharstring''falseCharacter to wrap every data and header value with.
bombooleantruefalseActivate or deactivate bom mode
newLineAtEndbooleanfalsefalseInsert new line at end of file.
disabledbooleanfalsefalseIf true the download process is blocked.
metabooleanfalsefalseIf true the downloaded file will contain meta instruction sep to help microsoft excel and open office to recognize the sepator character.
handleErrorfunctionundefinedfalseFunction to be invoked on error data
handleEmptyfunctionundefinedfalseFunction to be invoked on empty result data
titlestringundefinedfalseYou can pass a string to be added as a title at the top of the sheet

All other props are passed to button or wrapping component.

Full example

pass the downloaded datas as a component prop

render() {
  const columns = [{
    id: 'first',
    displayName: 'First column'
  }, {
    id: 'second',
    displayName: 'Second column'
  }];

  const datas = [{
    first: 'foo',
    second: 'bar'
  }, {
    first: 'foobar',
    second: 'foobar'
  }];

  return (
    <div>
      <CsvDownloader
        filename="myfile"
        extension=".csv"
        separator=";"
        wrapColumnChar="'"
        columns={columns}
        datas={datas}
        text="DOWNLOAD" />
    </div>
  );
}

// content of myfile.csv
// 'First column';'Second column'
// 'foo';'bar'
// 'foobar';'foobar'

Get CSV contents

If you just need to get CSV contents, use import { toCsv } from 'react-csv-downloader'; to import toCsv function and use it directly.

License

MIT License

3.1.0

4 months ago

3.0.0

6 months ago

2.9.1

1 year ago

2.9.0

1 year ago

2.8.0

2 years ago

2.7.1

2 years ago

2.7.0

3 years ago

2.6.0

3 years ago

2.5.0

3 years ago

2.3.0

3 years ago

2.4.0

3 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

1.9.0

4 years ago

2.0.0

4 years ago

1.8.0

4 years ago

1.7.0

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.0

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.2.0

6 years ago

0.1.4

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago