Skip to content

Commit

Permalink
Merge pull request #7981 from marmelab/fix-admin-children-function-type
Browse files Browse the repository at this point in the history
[TypeScript] Fix `<Admin>` used with function child and fragment does not compile
  • Loading branch information
djhi authored Jul 18, 2022
2 parents 56f6538 + 543f70c commit 05942f9
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
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

0 comments on commit 05942f9

Please sign in to comment.