|
| 1 | +import * as React from 'react'; |
| 2 | +import fakeRestProvider from 'ra-data-fakerest'; |
| 3 | +import { |
| 4 | + CoreAdminContext, |
| 5 | + Exporter, |
| 6 | + ListBase, |
| 7 | + useBulkExport, |
| 8 | + UseBulkExportOptions, |
| 9 | + useListContext, |
| 10 | +} from '..'; |
| 11 | + |
| 12 | +export default { |
| 13 | + title: 'ra-core/controller/list/useBulkExport', |
| 14 | +}; |
| 15 | + |
| 16 | +const data = { |
| 17 | + books: [ |
| 18 | + { id: 1, title: 'War and Peace' }, |
| 19 | + { id: 2, title: 'The Little Prince' }, |
| 20 | + { id: 3, title: "Swann's Way" }, |
| 21 | + { id: 4, title: 'A Tale of Two Cities' }, |
| 22 | + { id: 5, title: 'The Lord of the Rings' }, |
| 23 | + { id: 6, title: 'And Then There Were None' }, |
| 24 | + { id: 7, title: 'Dream of the Red Chamber' }, |
| 25 | + { id: 8, title: 'The Hobbit' }, |
| 26 | + { id: 9, title: 'She: A History of Adventure' }, |
| 27 | + { id: 10, title: 'The Lion, the Witch and the Wardrobe' }, |
| 28 | + { id: 11, title: 'The Chronicles of Narnia' }, |
| 29 | + { id: 12, title: 'Pride and Prejudice' }, |
| 30 | + { id: 13, title: 'Ulysses' }, |
| 31 | + { id: 14, title: 'The Catcher in the Rye' }, |
| 32 | + { id: 15, title: 'The Little Mermaid' }, |
| 33 | + { id: 16, title: 'The Secret Garden' }, |
| 34 | + { id: 17, title: 'The Wind in the Willows' }, |
| 35 | + { id: 18, title: 'The Wizard of Oz' }, |
| 36 | + { id: 19, title: 'Madam Bovary' }, |
| 37 | + { id: 20, title: 'The Little House' }, |
| 38 | + { id: 21, title: 'The Phantom of the Opera' }, |
| 39 | + { id: 22, title: 'The Adventures of Tom Sawyer' }, |
| 40 | + { id: 23, title: 'The Adventures of Huckleberry Finn' }, |
| 41 | + { id: 24, title: 'The Time Machine' }, |
| 42 | + { id: 25, title: 'The War of the Worlds' }, |
| 43 | + ], |
| 44 | +}; |
| 45 | + |
| 46 | +const dataProvider = fakeRestProvider( |
| 47 | + data, |
| 48 | + process.env.NODE_ENV !== 'test', |
| 49 | + 300 |
| 50 | +); |
| 51 | + |
| 52 | +export const Basic = ({ |
| 53 | + exporter = (data, fetchRelatedRecords, dataProvider, resource) => { |
| 54 | + alert( |
| 55 | + `Exporting ${data.length} items (${data.map(record => record.id).join(', ')}) from ${resource}` |
| 56 | + ); |
| 57 | + }, |
| 58 | +}: { |
| 59 | + exporter?: Exporter; |
| 60 | +}) => ( |
| 61 | + <CoreAdminContext dataProvider={dataProvider}> |
| 62 | + <ListBase resource="books" perPage={5} exporter={exporter}> |
| 63 | + <ListView /> |
| 64 | + <BulkExportButton /> |
| 65 | + </ListBase> |
| 66 | + </CoreAdminContext> |
| 67 | +); |
| 68 | + |
| 69 | +const ListView = () => { |
| 70 | + const { data, error, isPending, selectedIds, onToggleItem } = |
| 71 | + useListContext(); |
| 72 | + |
| 73 | + if (isPending) { |
| 74 | + return <div>Loading...</div>; |
| 75 | + } |
| 76 | + if (error) { |
| 77 | + return <div>Error...</div>; |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <div> |
| 82 | + <ul> |
| 83 | + {data.map((record: any) => ( |
| 84 | + <li key={record.id}> |
| 85 | + <label> |
| 86 | + <input |
| 87 | + type="checkbox" |
| 88 | + style={{ marginRight: '8px' }} |
| 89 | + checked={selectedIds.includes(record.id)} |
| 90 | + onChange={() => onToggleItem(record.id)} |
| 91 | + /> |
| 92 | + {record.title} |
| 93 | + </label> |
| 94 | + </li> |
| 95 | + ))} |
| 96 | + </ul> |
| 97 | + </div> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +export const HookLevelExporter = ({ |
| 102 | + exporter = (data, fetchRelatedRecords, dataProvider, resource) => { |
| 103 | + alert( |
| 104 | + `Exporting ${data.length} items (${data.map(record => record.id).join(', ')}) from ${resource} at the list level` |
| 105 | + ); |
| 106 | + }, |
| 107 | + hookExporter = (data, fetchRelatedRecords, dataProvider, resource) => { |
| 108 | + alert( |
| 109 | + `Exporting ${data.length} items (${data.map(record => record.id).join(', ')}) from ${resource} at the hook level` |
| 110 | + ); |
| 111 | + }, |
| 112 | +}: { |
| 113 | + exporter?: Exporter; |
| 114 | + hookExporter?: Exporter; |
| 115 | +}) => ( |
| 116 | + <CoreAdminContext dataProvider={dataProvider}> |
| 117 | + <ListBase resource="books" perPage={5} exporter={exporter}> |
| 118 | + <ListView /> |
| 119 | + <BulkExportButton exporter={hookExporter} /> |
| 120 | + </ListBase> |
| 121 | + </CoreAdminContext> |
| 122 | +); |
| 123 | + |
| 124 | +const BulkExportButton = ({ exporter }: UseBulkExportOptions) => { |
| 125 | + const bulkExport = useBulkExport({ exporter }); |
| 126 | + return <button onClick={bulkExport}>Export</button>; |
| 127 | +}; |
0 commit comments