@@ -8,6 +8,7 @@ import paginationRightSVG from '@plone/volto/icons/right-key.svg';
8
8
import sortUp from '@plone/volto/icons/sort-up.svg' ;
9
9
import sortDown from '@plone/volto/icons/sort-down.svg' ;
10
10
import { defineMessages , useIntl } from 'react-intl' ;
11
+ import { CSVLink } from 'react-csv' ;
11
12
12
13
const messages = defineMessages ( {
13
14
page_size : {
@@ -18,6 +19,10 @@ const messages = defineMessages({
18
19
id : 'all' ,
19
20
defaultMessage : 'All' ,
20
21
} ,
22
+ export_csv_file : {
23
+ id : 'Export CSV file' ,
24
+ defaultMessage : 'Export CSV file' ,
25
+ } ,
21
26
} ) ;
22
27
23
28
const View = ( { data, id, path, properties } ) => {
@@ -105,6 +110,15 @@ const View = ({ data, id, path, properties }) => {
105
110
} )
106
111
: [ ] ;
107
112
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
+
108
122
// const value = props.value || mockvalue;
109
123
const columns = React . useMemo (
110
124
( ) => [ ...header_columns ] ,
@@ -201,42 +215,57 @@ const View = ({ data, id, path, properties }) => {
201
215
</ Table . Body >
202
216
</ Table >
203
217
204
- { data ?. items && data . items . length > 10 && (
218
+ { data ?. items && (
205
219
< 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 >
240
269
</ div >
241
270
) }
242
271
</ div >
0 commit comments