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

Add ability to pass multiple children to <List> and <Create> #8533

Merged
merged 1 commit into from
Dec 22, 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
15 changes: 15 additions & 0 deletions packages/ra-ui-materialui/src/detail/Create.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,19 @@ describe('<Create />', () => {
);
expect(screen.queryAllByText('Hello')).toHaveLength(1);
});

it('should accept more than one child', () => {
const Form = () => <div>form</div>;
const HelpText = () => <div>help</div>;
render(
<CoreAdminContext dataProvider={testDataProvider()}>
<Create {...defaultCreateProps}>
<Form />
<HelpText />
</Create>
</CoreAdminContext>
);
expect(screen.queryAllByText('form')).toHaveLength(1);
expect(screen.queryAllByText('help')).toHaveLength(1);
});
});
6 changes: 3 additions & 3 deletions packages/ra-ui-materialui/src/detail/Create.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { ReactElement } from 'react';
import { ReactElement, ReactNode } from 'react';
import PropTypes from 'prop-types';
import { RaRecord, useCheckMinimumRequiredProps } from 'ra-core';

Expand Down Expand Up @@ -51,7 +51,7 @@ import { CreateBase } from 'ra-core';
* export default App;
*/
export const Create = <RecordType extends RaRecord = any>(
props: CreateProps<RecordType> & { children: ReactElement }
props: CreateProps<RecordType> & { children: ReactNode }
): ReactElement => {
useCheckMinimumRequiredProps('Create', ['children'], props);
const {
Expand Down Expand Up @@ -84,7 +84,7 @@ export const Create = <RecordType extends RaRecord = any>(
Create.propTypes = {
actions: PropTypes.oneOfType([PropTypes.element, PropTypes.bool]),
aside: PropTypes.element,
children: PropTypes.element,
children: PropTypes.node,
className: PropTypes.string,
disableAuthentication: PropTypes.bool,
hasEdit: PropTypes.bool,
Expand Down
6 changes: 3 additions & 3 deletions packages/ra-ui-materialui/src/detail/CreateView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react';
import { ReactElement } from 'react';
import { ReactNode } from 'react';
import PropTypes from 'prop-types';
import { Card } from '@mui/material';
import { styled } from '@mui/material/styles';
Expand Down Expand Up @@ -48,13 +48,13 @@ export const CreateView = (props: CreateViewProps) => {
interface CreateViewProps<RecordType extends RaRecord = any>
extends CreateProps<RecordType>,
Omit<CreateControllerProps<RecordType>, 'resource'> {
children: ReactElement;
children: ReactNode;
}

CreateView.propTypes = {
actions: PropTypes.oneOfType([PropTypes.element, PropTypes.bool]),
aside: PropTypes.element,
children: PropTypes.element,
children: PropTypes.node,
className: PropTypes.string,
defaultTitle: PropTypes.any,
hasList: PropTypes.bool,
Expand Down
17 changes: 17 additions & 0 deletions packages/ra-ui-materialui/src/list/List.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ describe('<List />', () => {
expect(screen.queryAllByText('datagrid')).toHaveLength(1);
});

it('should accept more than one child', () => {
const Filter = () => <div>filter</div>;
const Datagrid = () => <div>datagrid</div>;
render(
<CoreAdminContext dataProvider={testDataProvider()}>
<ThemeProvider theme={theme}>
<List resource="posts">
<Filter />
<Datagrid />
</List>
</ThemeProvider>
</CoreAdminContext>
);
expect(screen.queryAllByText('filter')).toHaveLength(1);
expect(screen.queryAllByText('datagrid')).toHaveLength(1);
});

it('should display aside component', () => {
const Dummy = () => <div />;
const Aside = () => <div id="aside">Hello</div>;
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/list/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ List.propTypes = {
// @ts-ignore-line
actions: PropTypes.oneOfType([PropTypes.bool, PropTypes.element]),
aside: PropTypes.element,
children: PropTypes.element.isRequired,
children: PropTypes.node.isRequired,
className: PropTypes.string,
emptyWhileLoading: PropTypes.bool,
filter: PropTypes.object,
Expand Down
19 changes: 14 additions & 5 deletions packages/ra-ui-materialui/src/list/ListView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as React from 'react';
import { styled } from '@mui/material/styles';
import { Children, cloneElement, ReactElement, ElementType } from 'react';
import {
Children,
cloneElement,
ReactElement,
ReactNode,
ElementType,
} from 'react';
import PropTypes from 'prop-types';
import { SxProps } from '@mui/system';
import Card from '@mui/material/Card';
Expand Down Expand Up @@ -61,8 +67,11 @@ export const ListView = <RecordType extends RaRecord = any>(
/>
)}
<Content className={ListClasses.content}>
{bulkActionButtons && children
? cloneElement(Children.only(children), {
{bulkActionButtons &&
children &&
React.isValidElement<any>(children)
? // FIXME remove in 5.0
cloneElement(children, {
bulkActionButtons,
})
: children}
Expand Down Expand Up @@ -101,7 +110,7 @@ ListView.propTypes = {
// @ts-ignore-line
actions: PropTypes.oneOfType([PropTypes.bool, PropTypes.element]),
aside: PropTypes.element,
children: PropTypes.element,
children: PropTypes.node,
className: PropTypes.string,
component: ComponentPropType,
// @ts-ignore-line
Expand Down Expand Up @@ -152,7 +161,7 @@ export interface ListViewProps {
*/
bulkActionButtons?: ReactElement | false;
className?: string;
children: ReactElement;
children: ReactNode;
component?: ElementType;
empty?: ReactElement | false;
emptyWhileLoading?: boolean;
Expand Down