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

[RFR] [BC break] Easier custom toolbar #2340

Merged
merged 3 commits into from
Sep 19, 2018
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
24 changes: 22 additions & 2 deletions docs/CreateEdit.md
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ The most common use case is to display two submit buttons in the `<Create>` view
For that use case, use the `<SaveButton>` component with a custom `redirect` prop:

```jsx
import { Edit, SimpleForm, SaveButton, Toolbar } from 'react-admin';
import { Create, SimpleForm, SaveButton, Toolbar } from 'react-admin';

const PostCreateToolbar = props => (
<Toolbar {...props} >
Expand All @@ -630,9 +630,29 @@ const PostCreateToolbar = props => (
</Toolbar>
);

export const PostCreate = (props) => (
<Create {...props}>
<SimpleForm toolbar={<PostCreateToolbar />} redirect="show">
...
</SimpleForm>
</Create>
);
```

Another use case is to remove the `<DeleteButton>` from the toolbar in an edit view. In that case, create a custom toolbar containing only the `<SaveButton>` as child;

```jsx
import { Edit, SimpleForm, SaveButton, Toolbar } from 'react-admin';

const PostEditToolbar = props => (
<Toolbar {...props} >
<SaveButton />
</Toolbar>
);

export const PostEdit = (props) => (
<Edit {...props}>
<SimpleForm toolbar={<PostCreateToolbar />} redirect="show">
<SimpleForm toolbar={<PostEditToolbar />}>
...
</SimpleForm>
</Edit>
Expand Down
85 changes: 43 additions & 42 deletions packages/ra-ui-materialui/src/form/Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import classnames from 'classnames';
import { SaveButton, DeleteButton } from '../button';

const styles = {
desktopToolbar: {
justifyContent: 'space-between',
},
mobileToolbar: {
position: 'fixed',
bottom: 0,
Expand All @@ -22,9 +19,13 @@ const styles = {
boxSizing: 'border-box',
flexShrink: 0,
backgroundColor: 'white',
justifyContent: 'space-between',
zIndex: 2,
},
defaultToolbar: {
flex: 1,
display: 'flex',
justifyContent: 'space-between',
},
};

const valueOrDefault = (value, defaultValue) =>
Expand All @@ -49,56 +50,56 @@ const Toolbar = ({
}) => (
<MuiToolbar
className={classnames(
width === 'xs' ? classes.mobileToolbar : classes.desktopToolbar,
{ [classes.mobileToolbar]: width === 'xs' },
className
)}
disableGutters
{...rest}
>
<span>
{Children.count(children) === 0 ? (
{Children.count(children) === 0 ? (
<div className={classes.defaultToolbar}>
<SaveButton
handleSubmitWithRedirect={handleSubmitWithRedirect}
invalid={invalid}
redirect={redirect}
saving={saving}
submitOnEnter={submitOnEnter}
/>
) : (
Children.map(
children,
button =>
button
? React.cloneElement(button, {
basePath,
handleSubmit: valueOrDefault(
button.props.handleSubmit,
handleSubmit
),
handleSubmitWithRedirect: valueOrDefault(
button.props.handleSubmitWithRedirect,
handleSubmitWithRedirect
),
invalid,
pristine,
saving,
submitOnEnter: valueOrDefault(
button.props.submitOnEnter,
submitOnEnter
),
})
: null
)
)}
</span>
{record &&
record.id && (
<DeleteButton
basePath={basePath}
record={record}
resource={resource}
/>
)}
{record &&
record.id && (
<DeleteButton
basePath={basePath}
record={record}
resource={resource}
/>
)}
</div>
) : (
Children.map(
children,
button =>
button
? React.cloneElement(button, {
basePath,
handleSubmit: valueOrDefault(
button.props.handleSubmit,
handleSubmit
),
handleSubmitWithRedirect: valueOrDefault(
button.props.handleSubmitWithRedirect,
handleSubmitWithRedirect
),
invalid,
pristine,
saving,
submitOnEnter: valueOrDefault(
button.props.submitOnEnter,
submitOnEnter
),
})
: null
)
)}
</MuiToolbar>
);

Expand Down