Skip to content

Commit

Permalink
Merge pull request #2340 from marmelab/easier-custom-toolbar
Browse files Browse the repository at this point in the history
[RFR] [BC break] Easier custom toolbar
  • Loading branch information
Gildas Garcia authored Sep 19, 2018
2 parents 59c4c73 + ede32dd commit c12ece5
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 44 deletions.
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

0 comments on commit c12ece5

Please sign in to comment.