import * as React from 'react'; import { onlineManager, QueryClient } from '@tanstack/react-query'; import polyglotI18nProvider from 'ra-i18n-polyglot'; import englishMessages from 'ra-language-english'; import fakeRestDataProvider from 'ra-data-fakerest'; import { TestMemoryRouter } from '../../routing'; import { EditBase } from '../edit'; import { ChoicesProps, Form, InputProps, useChoices, useChoicesContext, useInput, } from '../../form'; import { ReferenceInputBase } from './ReferenceInputBase'; import { CoreAdmin, CoreAdminContext, DataProvider, ListBase, Resource, testDataProvider, useIsOffline, useListContext, useRedirect, } from '../..'; export default { title: 'ra-core/controller/input/ReferenceInputBase', excludeStories: ['dataProviderWithAuthors'], }; const authors = [ { id: 1, first_name: 'Leo', last_name: 'Tolstoy', language: 'Russian' }, { id: 2, first_name: 'Victor', last_name: 'Hugo', language: 'French' }, { id: 3, first_name: 'William', last_name: 'Shakespeare', language: 'English', }, { id: 4, first_name: 'Charles', last_name: 'Baudelaire', language: 'French', }, { id: 5, first_name: 'Marcel', last_name: 'Proust', language: 'French' }, ]; const dataProviderWithAuthors = { getOne: () => Promise.resolve({ data: { id: 1, title: 'War and Peace', author: 1, summary: "War and Peace broadly focuses on Napoleon's invasion of Russia, and the impact it had on Tsarist society. The book explores themes such as revolution, revolution and empire, the growth and decline of various states and the impact it had on their economies, culture, and society.", year: 1869, }, }), getMany: (_resource, params) => Promise.resolve({ data: authors.filter(author => params.ids.includes(author.id)), }), getList: () => new Promise(resolve => { setTimeout( () => resolve({ data: authors, total: authors.length, }), 500 ); return; }), update: (_resource, params) => Promise.resolve(params), } as any; const AutocompleteInput = ( props: Omit & Partial> & ChoicesProps & { source?: string } ) => { const { allChoices, error, source, setFilters } = useChoicesContext(props); const { getChoiceValue, getChoiceText } = useChoices(props); const { field } = useInput({ ...props, source }); if (error) { return
{error.message}
; } return (
{ const choice = allChoices?.find( choice => getChoiceText(choice).toString() === e.target.value ); if (choice) { field.onChange(getChoiceValue(choice)); return; } setFilters({ q: e.target.value }, {}, true); }} /> {allChoices?.map(choice => ( ))}
); }; const SelectInput = ( props: Omit & Partial> & ChoicesProps & { source?: string } ) => { const { allChoices, error, isPending, source } = useChoicesContext(props); const { getChoiceValue, getChoiceText } = useChoices(props); const { field, id } = useInput({ ...props, source }); if (error) { return
{error.message}
; } return (
); }; const TextInput = (props: InputProps) => { const { field } = useInput(props); return (
); }; const BookEdit = () => ( { console.log(data); }, }} >
); export const Basic = ({ dataProvider = dataProviderWithAuthors }) => ( ); const tags = [ { id: 5, name: 'lorem' }, { id: 6, name: 'ipsum' }, ]; const dataProvider = testDataProvider({ getList: () => new Promise(resolve => { setTimeout( () => resolve({ // @ts-ignore data: tags, total: tags.length, }), process.env.NODE_ENV === 'test' ? 0 : 1500 ); }), // @ts-ignore getMany: (resource, params) => { return Promise.resolve({ data: tags.filter(tag => params.ids.includes(tag.id)), }); }, }); const i18nProvider = polyglotI18nProvider(() => englishMessages); export const Loading = () => (
{}} defaultValues={{ tag_ids: [5] }}>
); const book = { id: 1, title: 'War and Peace', author: 1, summary: "War and Peace broadly focuses on Napoleon's invasion of Russia, and the impact it had on Tsarist society. The book explores themes such as revolution, revolution and empire, the growth and decline of various states and the impact it had on their economies, culture, and society.", year: 1869, }; export const Error = () => ( Promise.resolve({ data: book }), getMany: (_resource, params) => Promise.resolve({ data: authors.filter(author => params.ids.includes(author.id) ), }), getList: (_resource, params) => { return params.filter.q === 'lorem' ? Promise.reject({ message: 'An error occured' }) : Promise.resolve({ data: authors, total: authors.length, }); }, } as any } queryClient={ new QueryClient({ defaultOptions: { queries: { retry: false } }, }) } > (
<>

Enter "lorem" to trigger the error

)} />
); const AuthorList = () => ( ); const ListView = () => { const context = useListContext(); return (
    {context.data?.map(record => (
  • {record.first_name} {record.last_name}
  • ))}
); }; const BookEditWithSelfReference = () => { const redirect = useRedirect(); return ( { // Redirecting to another page is an indirect way to make sure that // no errors happened during the update nor its side effects // (used by the jest tests) redirect('/authors'); }, }} >
); }; export const SelfReference = ({ dataProvider = dataProviderWithAuthors }) => ( ); const BookEditQueryOptions = () => { const [enabled, setEnabled] = React.useState(false); return (
); }; export const QueryOptions = () => ( ); const BookEditMeta = () => { return (
); }; export const Meta = ({ dataProvider = fakeRestDataProvider( { books: [ { id: 1, title: 'War and Peace', author: 1, summary: "War and Peace broadly focuses on Napoleon's invasion of Russia, and the impact it had on Tsarist society. The book explores themes such as revolution, revolution and empire, the growth and decline of various states and the impact it had on their economies, culture, and society.", year: 1869, }, ], authors: [ { id: 1, first_name: 'Leo', last_name: 'Tolstoy', language: 'Russian', }, { id: 2, first_name: 'Victor', last_name: 'Hugo', language: 'French', }, { id: 3, first_name: 'William', last_name: 'Shakespeare', language: 'English', }, { id: 4, first_name: 'Charles', last_name: 'Baudelaire', language: 'French', }, { id: 5, first_name: 'Marcel', last_name: 'Proust', language: 'French', }, ], }, process.env.NODE_ENV === 'development' ), }: { dataProvider: DataProvider; }) => ( ); export const Offline = () => (
{}} defaultValues={{ tag_ids: 5 }}>
You are offline, cannot load data

} >
); const SimulateOfflineButton = () => { const isOffline = useIsOffline(); return ( ); }; const RenderChildOnDemand = ({ children }) => { const [showChild, setShowChild] = React.useState(false); return ( <> {showChild &&
{children}
} ); }; export const FullHeadlessStory = () => { return ( { console.log(data); }, }} >
} />
); }; const CustomSelector = ( props: Omit & Partial> & ChoicesProps & { source?: string } ) => { const { allChoices, isPending, error, source } = useChoicesContext(props); const { field, id } = useInput({ ...props, source }); if (error) { return
{error.message}
; } return (
); };