Skip to content

Commit

Permalink
Optional extension
Browse files Browse the repository at this point in the history
  • Loading branch information
dsounded committed May 11, 2021
1 parent 308136e commit a88bddf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ You can also use the columns definition to set the columns display order
| columns | array of object | null | false | Columns definition |
| datas | array of object | null | true | Downloaded datas |
| filename | string | null | true | You can pass the filename without extension. The extension is automatically added |
| extension | string | '.csv' | false | You can pass the file extension, note that it will affect filename |
| separator | string | ',' | false | Columns separator |
| noHeader | boolean | false | false | If `true` the header isn't added to the csv file |
| prefix | string or boolean | false | false | Filename prefix. If `true` prefix becomes a timestamp |
Expand Down Expand Up @@ -103,6 +104,7 @@ render() {
<div>
<CsvDownloader
filename="myfile"
extension=".csv"
separator=";"
wrapColumnChar="'"
columns={columns}
Expand Down
12 changes: 7 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@ export type PrefixSuffix = boolean | string | number
export interface ICsvDownloadProps extends ICsvProps, Omit<React.HTMLAttributes<HTMLDivElement | HTMLButtonElement>, 'prefix'> {
bom?: boolean
filename: string
extension?: string
prefix?: PrefixSuffix
suffix?: PrefixSuffix
text?: string
}

export default class CsvDownload extends React.Component<ICsvDownloadProps> {
public handleClick = async () => {
const { suffix, prefix, bom } = this.props
const { suffix, prefix, bom, extension } = this.props
let { filename } = this.props
const csv = await toCsv(this.props)

const bomCode = bom !== false ? '\ufeff' : ''

if (filename.indexOf('.csv') === -1) {
filename += '.csv'
const resolvedExtension = extension || '.csv'
if (filename.indexOf(resolvedExtension) === -1) {
filename += resolvedExtension
}

if (suffix) {
filename =
typeof suffix === 'string' || typeof suffix === 'number'
? filename.replace('.csv', `_${suffix}.csv`)
: filename.replace('.csv', `_${new Date().getTime()}.csv`)
? filename.replace(resolvedExtension, `_${suffix}${resolvedExtension}`)
: filename.replace(resolvedExtension, `_${new Date().getTime()}${resolvedExtension}`)
}

if (prefix) {
Expand Down

0 comments on commit a88bddf

Please sign in to comment.