|
| 1 | +import { Resource, TestMemoryRouter } from 'ra-core'; |
| 2 | +import { AdminContext } from '../AdminContext'; |
| 3 | +import { deepmerge } from '@mui/utils'; |
| 4 | +import { createTheme, ThemeOptions } from '@mui/material'; |
| 5 | +import { AdminUI } from '../AdminUI'; |
| 6 | +import { DeleteWithUndoButton } from './DeleteWithUndoButton'; |
| 7 | +import * as React from 'react'; |
| 8 | +import polyglotI18nProvider from 'ra-i18n-polyglot'; |
| 9 | +import frenchMessages from 'ra-language-french'; |
| 10 | +import englishMessages from 'ra-language-english'; |
| 11 | +import fakeRestDataProvider from 'ra-data-fakerest'; |
| 12 | +import { Datagrid, List } from '../list'; |
| 13 | +import { TextField } from '../field'; |
| 14 | + |
| 15 | +export default { title: 'ra-ui-materialui/button/DeleteWithUndoButton' }; |
| 16 | + |
| 17 | +const i18nProvider = polyglotI18nProvider( |
| 18 | + locale => |
| 19 | + locale === 'fr' |
| 20 | + ? { |
| 21 | + ...frenchMessages, |
| 22 | + resources: { |
| 23 | + books: { |
| 24 | + name: 'Livre |||| Livres', |
| 25 | + fields: { |
| 26 | + id: 'Id', |
| 27 | + title: 'Titre', |
| 28 | + author: 'Auteur', |
| 29 | + year: 'Année', |
| 30 | + }, |
| 31 | + message: { |
| 32 | + delete_title: |
| 33 | + 'Supprimer le livre "%{recordRepresentation}" ?', |
| 34 | + delete_content: |
| 35 | + 'Souhaitez-vous vraiment supprimer le livre "%{recordRepresentation}" ?', |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + } |
| 40 | + : { |
| 41 | + ...englishMessages, |
| 42 | + resources: { |
| 43 | + books: { |
| 44 | + message: { |
| 45 | + delete_title: |
| 46 | + 'Delete the book "%{recordRepresentation}"?', |
| 47 | + delete_content: |
| 48 | + 'Do you really want to delete the book "%{recordRepresentation}"?', |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + // Default locale |
| 54 | + 'en', |
| 55 | + [ |
| 56 | + { locale: 'en', name: 'English' }, |
| 57 | + { locale: 'fr', name: 'Français' }, |
| 58 | + ] |
| 59 | +); |
| 60 | + |
| 61 | +const i18nProviderDefault = polyglotI18nProvider( |
| 62 | + locale => |
| 63 | + locale === 'fr' |
| 64 | + ? { |
| 65 | + ...frenchMessages, |
| 66 | + resources: { |
| 67 | + books: { |
| 68 | + name: 'Livre |||| Livres', |
| 69 | + fields: { |
| 70 | + id: 'Id', |
| 71 | + title: 'Titre', |
| 72 | + author: 'Auteur', |
| 73 | + year: 'Année', |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | + : englishMessages, |
| 79 | + // Default locale |
| 80 | + 'en', |
| 81 | + [ |
| 82 | + { locale: 'en', name: 'English' }, |
| 83 | + { locale: 'fr', name: 'Français' }, |
| 84 | + ] |
| 85 | +); |
| 86 | + |
| 87 | +const dataProvider = fakeRestDataProvider({ |
| 88 | + books: [ |
| 89 | + { |
| 90 | + id: 1, |
| 91 | + title: 'War and Peace', |
| 92 | + author: 'Leo Tolstoy', |
| 93 | + year: 1869, |
| 94 | + }, |
| 95 | + { |
| 96 | + id: 2, |
| 97 | + title: 'Pride and Predjudice', |
| 98 | + author: 'Jane Austen', |
| 99 | + year: 1813, |
| 100 | + }, |
| 101 | + { |
| 102 | + id: 3, |
| 103 | + title: 'The Picture of Dorian Gray', |
| 104 | + author: 'Oscar Wilde', |
| 105 | + year: 1890, |
| 106 | + }, |
| 107 | + { |
| 108 | + id: 4, |
| 109 | + title: 'Le Petit Prince', |
| 110 | + author: 'Antoine de Saint-Exupéry', |
| 111 | + year: 1943, |
| 112 | + }, |
| 113 | + { |
| 114 | + id: 5, |
| 115 | + title: "Alice's Adventures in Wonderland", |
| 116 | + author: 'Lewis Carroll', |
| 117 | + year: 1865, |
| 118 | + }, |
| 119 | + { |
| 120 | + id: 6, |
| 121 | + title: 'Madame Bovary', |
| 122 | + author: 'Gustave Flaubert', |
| 123 | + year: 1856, |
| 124 | + }, |
| 125 | + { |
| 126 | + id: 7, |
| 127 | + title: 'The Lord of the Rings', |
| 128 | + author: 'J. R. R. Tolkien', |
| 129 | + year: 1954, |
| 130 | + }, |
| 131 | + { |
| 132 | + id: 8, |
| 133 | + title: "Harry Potter and the Philosopher's Stone", |
| 134 | + author: 'J. K. Rowling', |
| 135 | + year: 1997, |
| 136 | + }, |
| 137 | + { |
| 138 | + id: 9, |
| 139 | + title: 'The Alchemist', |
| 140 | + author: 'Paulo Coelho', |
| 141 | + year: 1988, |
| 142 | + }, |
| 143 | + { |
| 144 | + id: 10, |
| 145 | + title: 'A Catcher in the Rye', |
| 146 | + author: 'J. D. Salinger', |
| 147 | + year: 1951, |
| 148 | + }, |
| 149 | + { |
| 150 | + id: 11, |
| 151 | + title: 'Ulysses', |
| 152 | + author: 'James Joyce', |
| 153 | + year: 1922, |
| 154 | + }, |
| 155 | + ], |
| 156 | + authors: [ |
| 157 | + { id: 1, fullName: 'Leo Tolstoy' }, |
| 158 | + { id: 2, fullName: 'Jane Austen' }, |
| 159 | + { id: 3, fullName: 'Oscar Wilde' }, |
| 160 | + { id: 4, fullName: 'Antoine de Saint-Exupéry' }, |
| 161 | + { id: 5, fullName: 'Lewis Carroll' }, |
| 162 | + { id: 6, fullName: 'Gustave Flaubert' }, |
| 163 | + { id: 7, fullName: 'J. R. R. Tolkien' }, |
| 164 | + { id: 8, fullName: 'J. K. Rowling' }, |
| 165 | + { id: 9, fullName: 'Paulo Coelho' }, |
| 166 | + { id: 10, fullName: 'J. D. Salinger' }, |
| 167 | + { id: 11, fullName: 'James Joyce' }, |
| 168 | + ], |
| 169 | +}); |
| 170 | + |
| 171 | +const BookList = ({ children }) => { |
| 172 | + return ( |
| 173 | + <List> |
| 174 | + <Datagrid> |
| 175 | + <TextField source="id" /> |
| 176 | + <TextField source="title" /> |
| 177 | + <TextField source="author" /> |
| 178 | + <TextField source="year" /> |
| 179 | + {children} |
| 180 | + </Datagrid> |
| 181 | + </List> |
| 182 | + ); |
| 183 | +}; |
| 184 | + |
| 185 | +export const Basic = () => ( |
| 186 | + <TestMemoryRouter initialEntries={['/books']}> |
| 187 | + <AdminContext dataProvider={dataProvider} i18nProvider={i18nProvider}> |
| 188 | + <AdminUI> |
| 189 | + <Resource |
| 190 | + name="books" |
| 191 | + list={ |
| 192 | + <BookList> |
| 193 | + <DeleteWithUndoButton /> |
| 194 | + </BookList> |
| 195 | + } |
| 196 | + /> |
| 197 | + </AdminUI> |
| 198 | + </AdminContext> |
| 199 | + </TestMemoryRouter> |
| 200 | +); |
| 201 | + |
| 202 | +export const Themed = () => ( |
| 203 | + <TestMemoryRouter initialEntries={['/books']}> |
| 204 | + <AdminContext |
| 205 | + dataProvider={dataProvider} |
| 206 | + i18nProvider={i18nProvider} |
| 207 | + theme={deepmerge(createTheme(), { |
| 208 | + components: { |
| 209 | + RaDeleteWithUndoButton: { |
| 210 | + defaultProps: { |
| 211 | + variant: 'outlined', |
| 212 | + 'data-testid': 'themed', |
| 213 | + }, |
| 214 | + styleOverrides: { |
| 215 | + root: { |
| 216 | + color: 'hotpink', |
| 217 | + }, |
| 218 | + }, |
| 219 | + }, |
| 220 | + }, |
| 221 | + } as ThemeOptions)} |
| 222 | + > |
| 223 | + <AdminUI> |
| 224 | + <Resource |
| 225 | + name="books" |
| 226 | + list={ |
| 227 | + <BookList> |
| 228 | + <DeleteWithUndoButton /> |
| 229 | + </BookList> |
| 230 | + } |
| 231 | + /> |
| 232 | + </AdminUI> |
| 233 | + </AdminContext> |
| 234 | + </TestMemoryRouter> |
| 235 | +); |
0 commit comments