import * as React from 'react'; import { onlineManager, QueryClient } from '@tanstack/react-query'; import fakeRestDataProvider from 'ra-data-fakerest'; import { CoreAdmin } from '../../core/CoreAdmin'; import { Resource } from '../../core/Resource'; import { ShowBase } from '../../controller/show/ShowBase'; import { TestMemoryRouter } from '../../routing'; import { ReferenceFieldBase } from './ReferenceFieldBase'; import { useFieldValue } from '../../util/useFieldValue'; import { useReferenceFieldContext } from './ReferenceFieldContext'; import { DataProvider } from '../../types'; import { useIsOffline } from '../../core/useIsOffline'; import { IsOffline } from '../..'; export default { title: 'ra-core/controller/field/ReferenceFieldBase', 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' }, ]; export 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)), }), } as any; export const Basic = ({ dataProvider = dataProviderWithAuthors }) => ( } /> ); const dataProviderWithAuthorsError = { 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 => Promise.reject(new Error('Error')), } as any; export const Errored = ({ dataProvider = dataProviderWithAuthorsError }) => ( } /> ); const dataProviderWithAuthorsLoading = { 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 => new Promise(() => {}), } as any; export const Loading = ({ dataProvider = dataProviderWithAuthorsLoading }) => ( } /> ); const BookShowQueryOptions = () => { const [enabled, setEnabled] = React.useState(false); return ( <> ); }; export const QueryOptions = () => ( ); const BookShowMeta = () => { 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 WithRenderProp = ({ dataProvider = dataProviderWithAuthors }) => ( { if (isPending) { return

Loading...

; } if (error) { return (

{error.message}

); } return ; }} /> } />
); export const ZeroIndex = ({ dataProvider = fakeRestDataProvider( { books: [{ id: 1, title: 'War and Peace', author: 0 }], authors: [{ id: 0, first_name: 'Leo', last_name: 'Tolstoy' }], }, process.env.NODE_ENV === 'development' ), }: { dataProvider?: DataProvider; }) => ( ( Should not appear} > )} /> ); export const Offline = () => { return (
You are offline, cannot load data

} >

You are offline, the data may be outdated

} />
); }; const SimulateOfflineButton = () => { const isOffline = useIsOffline(); return ( ); }; const RenderChildOnDemand = ({ children }) => { const [showChild, setShowChild] = React.useState(false); return ( <> {showChild &&
{children}
} ); }; const MyReferenceField = (props: { children: React.ReactNode }) => { const context = useReferenceFieldContext(); if (context.isPending) { return

Loading...

; } if (context.error) { return

{context.error.toString()}

; } return props.children; }; const TextField = ({ source }: { source: string }) => { const value = useFieldValue({ source }); return

{value}

; };