Skip to content

Commit baf3010

Browse files
committed
feat: export csv
1 parent 657064a commit baf3010

File tree

2 files changed

+71
-35
lines changed

2 files changed

+71
-35
lines changed

src/components/Blocks/DataTableBlock/View.jsx

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import paginationRightSVG from '@plone/volto/icons/right-key.svg';
88
import sortUp from '@plone/volto/icons/sort-up.svg';
99
import sortDown from '@plone/volto/icons/sort-down.svg';
1010
import { defineMessages, useIntl } from 'react-intl';
11+
import { CSVLink } from 'react-csv';
1112

1213
const messages = defineMessages({
1314
page_size: {
@@ -18,6 +19,10 @@ const messages = defineMessages({
1819
id: 'all',
1920
defaultMessage: 'All',
2021
},
22+
export_csv_file: {
23+
id: 'Export CSV file',
24+
defaultMessage: 'Export CSV file',
25+
},
2126
});
2227

2328
const View = ({ data, id, path, properties }) => {
@@ -105,6 +110,15 @@ const View = ({ data, id, path, properties }) => {
105110
})
106111
: [];
107112

113+
const csv_columns = schema
114+
? schema.fieldsets[0].fields.map((field) => {
115+
return {
116+
label: schema.properties[field]?.title || field,
117+
key: field,
118+
};
119+
})
120+
: [];
121+
108122
// const value = props.value || mockvalue;
109123
const columns = React.useMemo(
110124
() => [...header_columns],
@@ -201,42 +215,57 @@ const View = ({ data, id, path, properties }) => {
201215
</Table.Body>
202216
</Table>
203217

204-
{data?.items && data.items.length > 10 && (
218+
{data?.items && (
205219
<div className="pagination-wrapper react-table-pagination cms-ui">
206-
<Pagination
207-
activePage={pageIndex + 1}
208-
totalPages={pageCount}
209-
onPageChange={(e, { activePage }) => {
210-
gotoPage(activePage - 1);
211-
}}
212-
firstItem={null}
213-
lastItem={null}
214-
prevItem={{
215-
content: <Icon name={paginationLeftSVG} size="18px" />,
216-
icon: true,
217-
'aria-disabled': pageIndex + 1 === 1,
218-
className: pageIndex + 1 === 1 ? 'disabled' : null,
219-
}}
220-
nextItem={{
221-
content: <Icon name={paginationRightSVG} size="18px" />,
222-
icon: true,
223-
'aria-disabled': pageIndex + 1 === pageCount,
224-
className: pageIndex + 1 === pageCount ? 'disabled' : null,
225-
}}
226-
></Pagination>
227-
<select
228-
style={{ maxWidth: '7rem' }}
229-
value={pageSize}
230-
onChange={(e) => {
231-
setPageSize(Number(e.target.value));
232-
}}
233-
>
234-
{[10, 25, 50, 100].map((pageSize) => (
235-
<option key={pageSize} value={pageSize}>
236-
{intl.formatMessage(messages.page_size, { pageSize })}
237-
</option>
238-
))}
239-
</select>
220+
{data.items.length > 10 && (
221+
<>
222+
<Pagination
223+
activePage={pageIndex + 1}
224+
totalPages={pageCount}
225+
onPageChange={(e, { activePage }) => {
226+
gotoPage(activePage - 1);
227+
}}
228+
firstItem={null}
229+
lastItem={null}
230+
prevItem={{
231+
content: <Icon name={paginationLeftSVG} size="18px" />,
232+
icon: true,
233+
'aria-disabled': pageIndex + 1 === 1,
234+
className: pageIndex + 1 === 1 ? 'disabled' : null,
235+
}}
236+
nextItem={{
237+
content: <Icon name={paginationRightSVG} size="18px" />,
238+
icon: true,
239+
'aria-disabled': pageIndex + 1 === pageCount,
240+
className: pageIndex + 1 === pageCount ? 'disabled' : null,
241+
}}
242+
></Pagination>
243+
<select
244+
style={{ maxWidth: '7rem' }}
245+
value={pageSize}
246+
onChange={(e) => {
247+
setPageSize(Number(e.target.value));
248+
}}
249+
>
250+
{[10, 25, 50, 100].map((pageSize) => (
251+
<option key={pageSize} value={pageSize}>
252+
{intl.formatMessage(messages.page_size, { pageSize })}
253+
</option>
254+
))}
255+
</select>
256+
</>
257+
)}
258+
<div className="actions">
259+
<CSVLink
260+
className="ui button"
261+
filename={schema.title ? `${schema.title}.csv` : 'export.csv'}
262+
separator=";"
263+
headers={csv_columns}
264+
data={data?.items || []}
265+
>
266+
{intl.formatMessage(messages.export_csv_file)}
267+
</CSVLink>
268+
</div>
240269
</div>
241270
)}
242271
</div>

src/components/Widgets/react-table-widget.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@
2626
display: inline-flex;
2727
width: 100%;
2828
justify-content: center;
29+
position: relative;
2930
}
3031

32+
.react-table-pagination .actions {
33+
right: 0;
34+
position: absolute;
35+
}
36+
37+
3138
.selected-row
3239
.ui.selection.dropdown:not(.multiple):not(.search)
3340
> .dropdown.icon {

0 commit comments

Comments
 (0)