Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeScript] Fix <Admin> used with function child and fragment does not compile #7981

Merged
merged 1 commit into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions packages/ra-core/src/core/CoreAdmin.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import * as React from 'react';
import { render, screen } from '@testing-library/react';

import { CoreAdmin } from './CoreAdmin';
import { Resource } from './Resource';

describe('CoreAdmin', () => {
describe('children', () => {
it('should accept Resources as children', async () => {
const Foo = () => <div>Foo</div>;
const App = () => (
<CoreAdmin>
<Resource name="posts" list={Foo} />
<Resource name="comments" list={Foo} />
</CoreAdmin>
);
render(<App />);
await screen.findByText('Foo');
});
it('should accept a function returning an array of Resources as children', async () => {
const Foo = () => <div>Foo</div>;
const App = () => (
<CoreAdmin>
{() => [
<Resource name="posts" key="posts" list={Foo} />,
<Resource name="comments" key="comments" list={Foo} />,
]}
</CoreAdmin>
);
render(<App />);
await screen.findByText('Foo');
});
it('should accept a function returning a fragment of Resources as children', async () => {
const Foo = () => <div>Foo</div>;
const App = () => (
<CoreAdmin>
{() => (
<>
<Resource name="posts" list={Foo} />
<Resource name="comments" list={Foo} />
</>
)}
</CoreAdmin>
);
render(<App />);
await screen.findByText('Foo');
});
it('should accept a function returning a promise for an array of Resources as children', async () => {
const Foo = () => <div>Foo</div>;
const App = () => (
<CoreAdmin>
{() =>
Promise.resolve([
<Resource name="posts" key="posts" list={Foo} />,
<Resource
name="comments"
key="comments"
list={Foo}
/>,
])
}
</CoreAdmin>
);
render(<App />);
await screen.findByText('Foo');
});
it('should accept a function returning a promise for a fragment of Resources as children', async () => {
const Foo = () => <div>Foo</div>;
const App = () => (
<CoreAdmin>
{() =>
Promise.resolve(
<>
<Resource name="posts" list={Foo} />
<Resource name="comments" list={Foo} />
</>
)
}
</CoreAdmin>
);
render(<App />);
await screen.findByText('Foo');
});
});
});
6 changes: 5 additions & 1 deletion packages/ra-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,11 @@ export type Dispatch<T> = T extends (...args: infer A) => any
export type ResourceElement = ReactElement<ResourceProps>;
export type RenderResourcesFunction = (
permissions: any
) => ResourceElement[] | Promise<ResourceElement[]>;
) =>
| ReactNode // (permissions) => <><Resource /><Resource /><Resource /></>
| Promise<ReactNode> // (permissions) => fetch().then(() => <><Resource /><Resource /><Resource /></>)
| ResourceElement[] // // (permissions) => [<Resource />, <Resource />, <Resource />]
| Promise<ResourceElement[]>; // (permissions) => fetch().then(() => [<Resource />, <Resource />, <Resource />])
export type AdminChildren = RenderResourcesFunction | ReactNode;

export type TitleComponent = string | ReactElement<any>;
Expand Down