Skip to content

Commit

Permalink
Merge 749b5f3 into 5b8b367
Browse files Browse the repository at this point in the history
  • Loading branch information
camdendotlol authored Oct 17, 2023
2 parents 5b8b367 + 749b5f3 commit 5c00297
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/semantic-ui/src/components/DataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ DataTable.defaultProps = {
buttons: [],
count: 0,
className: '',
csvExportButton: undefined,
expandableRows: false,
expandPanel: undefined,
filters: undefined,
Expand Down
73 changes: 73 additions & 0 deletions packages/semantic-ui/src/components/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ type Props = {
*/
className?: string,

/**
* If provided, a CSV export button will be rendered in the list header.
*/
csvExportButton?: {
basic: boolean,
color: string,
location: string,
onClick?: () => void
},

/**
* If provided, a "delete all" button will be rendered in the list header.
*/
Expand Down Expand Up @@ -208,6 +218,7 @@ type State = {
};

const BUTTON_KEY_ADD = 'add';
const BUTTON_KEY_CSV_EXPORT = 'csv-export';
const BUTTON_KEY_DELETE_ALL = 'delete-all';

/**
Expand All @@ -229,6 +240,7 @@ const useList = (WrappedComponent: ComponentType<any>) => (
},
buttons: [],
className: '',
csvExportButton: undefined,
filters: undefined,
modal: undefined,
page: 1,
Expand Down Expand Up @@ -269,6 +281,7 @@ const useList = (WrappedComponent: ComponentType<any>) => (

const {
addButton = {},
csvExportButton = {},
deleteButton = {},
modal,
selectable
Expand All @@ -288,6 +301,13 @@ const useList = (WrappedComponent: ComponentType<any>) => (
});
}

// Add the CSV export button to the list if the csvExport prop is passed
if (csvExportButton.location === location && !selectable) {
buttons.push({
render: this.renderCsvExportButton.bind(this)
});
}

// Resolve the array of other buttons
buttons.push(..._.filter(this.props.buttons, (button) => {
let include = false;
Expand Down Expand Up @@ -331,6 +351,36 @@ const useList = (WrappedComponent: ComponentType<any>) => (
this.setState({ selectedItem: copy, modalEdit: true });
}

/**
* Generates and downloads a CSV file containing all
* the data in the table.
*
* @param items
*/
onCsvExportButton() {
let csv = `${this.props.columns.map((col) => `"${col.label}"`).join(',')}\n`;

this.props.items.forEach((item) => {
csv = csv.concat(`${this.props.columns.map((col) => {
if (col.resolve) {
return `"${col.resolve(item)}"`;
}
return `"${item[col.name]}"`;
}).join(',')}\n`);
});

const element = document.createElement('a');
element.setAttribute('href', `data:text/plain;charset=utf-8,${encodeURIComponent(csv)}`);
element.setAttribute('download', `${this.props.collectionName || 'table'}.csv`);

element.style.display = 'none';
document.body.appendChild(element);

element.click();

document.body.removeChild(element);
}

/**
* Deletes the currently selected item and clears the state.
*
Expand Down Expand Up @@ -533,6 +583,29 @@ const useList = (WrappedComponent: ComponentType<any>) => (
);
}

/**
* Renders the CSV export button.
*
* @returns {null|*}
*/
renderCsvExportButton() {
if (!this.props.csvExportButton) {
return null;
}

return (
<Button
basic={this.props.csvExportButton.basic}
color={this.props.csvExportButton.color}
key={BUTTON_KEY_CSV_EXPORT}
onClick={this.onCsvExportButton.bind(this)}
>
<Icon name='download' />
{ i18n.t('List.buttons.csvExport') }
</Button>
);
}

/**
* Renders the delete all button.
*
Expand Down
1 change: 1 addition & 0 deletions packages/semantic-ui/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
"List": {
"buttons": {
"add": "Add",
"csvExport": "CSV Export",
"deleteAll": "Delete all"
},
"deleteAllContent": "Are you sure you want to remove all records? This action cannot be undone.",
Expand Down
21 changes: 21 additions & 0 deletions packages/storybook/src/semantic-ui/DataTable.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,27 @@ export const Pagination = useDragDrop(() => (
/>
));

export const CsvExport = useDragDrop(() => (
<DataTable
actions={actions}
buttons={[
{
render: () => <Button key='1'>Test</Button>
},
]}
columns={columns}
csvExportButton={{
color: 'blue',
location: 'bottom'
}}
items={items}
onColumnClick={action('column-click')}
onCopy={action('copy')}
onDelete={action('delete')}
onSave={action('save')}
/>
));

export const CustomDeleteModal = useDragDrop(() => (
<DataTable
actions={actions}
Expand Down
20 changes: 20 additions & 0 deletions packages/storybook/src/semantic-ui/ListTable.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@ export const Count = useDragDrop(() => {
);
});

export const CsvExport = useDragDrop(() => (
<ListTable
actions={actions}
collectionName='items'
columns={columns}
csvExportButton={{
color: 'blue',
location: 'bottom'
}}
onCopy={action('copy')}
onLoad={(params) => Api.onLoad(_.extend(params, {
items,
perPage: number('Per page', 10)
}))}
onDelete={action('delete')}
onSave={action('save')}
searchable={boolean('Searchable', true)}
/>
));

export const CustomizableColumns = useDragDrop(() => (
<ListTable
actions={actions}
Expand Down

0 comments on commit 5c00297

Please sign in to comment.