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] Code cleanup #3688

Merged
merged 3 commits into from
Sep 13, 2019
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
91 changes: 52 additions & 39 deletions docs/List.md
Original file line number Diff line number Diff line change
Expand Up @@ -687,25 +687,30 @@ The `List` component accepts the usual `className` prop but you can override man

Here is an example of how you can override some of these classes:

You can customize the list styles by passing a `classes` object as prop, through `withStyles()`. Here is an example:
You can customize the list styles by passing a `classes` object as prop, through `useStyles()`. Here is an example:

{% raw %}
```jsx
const styles = {
import { makeStyles } from '@material-ui/core';

const useStyles = makeStyles({
header: {
backgroundColor: '#ccc',
},
};
});

const PostList = ({ classes, ...props }) => (
<List {...props} classes={{ header: classes.header }}>
<Datagrid>
...
</Datagrid>
</List>
);
const PostList = props => {
const classes = useStyles();
return (
<List {...props} classes={{ header: classes.header }}>
<Datagrid>
...
</Datagrid>
</List>
);
}

export withStyles(styles)(PostList);
export PostList;
```
{% endraw %}

Expand Down Expand Up @@ -974,56 +979,64 @@ The `Datagrid` component accepts the usual `className` prop but you can override

Here is an example of how you can override some of these classes:

You can customize the datagrid styles by passing a `classes` object as prop, through `withStyles()`. Here is an example:
You can customize the datagrid styles by passing a `classes` object as prop, through `useStyles()`. Here is an example:

{% raw %}
```jsx
const styles = {
import { makeStyles } from '@material-ui/core';

const useStyles = makeStyles({
row: {
backgroundColor: '#ccc',
},
};
});

const PostList = ({ classes, ...props) => (
<List {...props}>
<Datagrid classes={{ row: classes.row }}>
...
</Datagrid>
</List>
);
const PostList = props => {
const classes = useStyles();
return (
<List {...props}>
<Datagrid classes={{ row: classes.row }}>
...
</Datagrid>
</List>
);
}

export withStyles(styles)(PostList);
export PostList;
```
{% endraw %}

**Tip**: If you want to override the `header` and `cell` styles independently for each column, use the `headerClassName` and `cellClassName` props in `<Field>` components. For instance, to hide a certain column on small screens:

```jsx
import { withStyles } from '@material-ui/core/styles';
import { makeStyles } from '@material-ui/core';

const styles = theme => ({
const useStyles = makeStyles(theme => ({
hiddenOnSmallScreens: {
[theme.breakpoints.down('md')]: {
display: 'none',
},
},
});
}));

const PostList = ({ classes, ...props }) => (
<List {...props}>
<Datagrid>
<TextField source="id" />
<TextField source="title" />
<TextField
source="views"
headerClassName={classes.hiddenOnSmallScreens}
cellClassName={classes.hiddenOnSmallScreens}
/>
</Datagrid>
</List>
);
const PostList = props => {
const classes = usestyles();
return (
<List {...props}>
<Datagrid>
<TextField source="id" />
<TextField source="title" />
<TextField
source="views"
headerClassName={classes.hiddenOnSmallScreens}
cellClassName={classes.hiddenOnSmallScreens}
/>
</Datagrid>
</List>
);
};

export default withStyles(styles)(PostList);
export default PostList;
```

## The `<SimpleList>` component
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/src/layout/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Login.propTypes = {
};

// We need to put the ThemeProvider decoration in another component
// Because otherwise the withStyles() HOC used in EnhancedLogin won't get
// Because otherwise the useStyles() hook used in Login won't get
// the right theme
const LoginWithTheme = props => (
<ThemeProvider theme={createMuiTheme(lightTheme)}>
Expand Down
1 change: 0 additions & 1 deletion examples/simple/src/comments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import ChatBubbleIcon from '@material-ui/icons/ChatBubble';
import CommentCreate from './CommentCreate';
import CommentEdit from './CommentEdit';
import CommentList from './CommentList';
import CommentShow from './CommentShow';
import { ShowGuesser } from 'react-admin';

export default {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('useMatchingReferences', () => {
afterEach(cleanup);

it('should fetch matchingReferences only on mount', () => {
const { dispatch, rerender } = renderHook(() => {
const { dispatch } = renderHook(() => {
return useMatchingReferences(defaultProps);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/ra-core/src/controller/usePaginationState.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useReducer, useCallback, useRef } from 'react';
import { useEffect, useReducer, useCallback, useRef } from 'react';
import { Pagination } from '../types';

interface PaginationProps {
Expand Down
3 changes: 1 addition & 2 deletions packages/ra-core/src/controller/useShowController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export interface ShowProps {
hasList?: boolean;
id: Identifier;
resource: string;
undoable?: boolean;
[key: string]: any;
}

Expand Down Expand Up @@ -48,7 +47,7 @@ export interface ShowControllerProps {
*/
const useShowController = (props: ShowProps): ShowControllerProps => {
useCheckMinimumRequiredProps('Show', ['basePath', 'resource'], props);
const { basePath, id, resource, undoable = true } = props;
const { basePath, id, resource } = props;
const translate = useTranslate();
const notify = useNotify();
const redirect = useRedirect();
Expand Down
7 changes: 1 addition & 6 deletions packages/ra-core/src/dataProvider/useGetMany.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ const UseGetMany = ({
};

describe('useGetMany', () => {
const defaultProps = {
ids: [1],
resource: 'posts',
};

afterEach(cleanup);

it('should call the dataProvider with a GET_MANY on mount', async () => {
Expand Down Expand Up @@ -100,7 +95,7 @@ describe('useGetMany', () => {
.mockReturnValueOnce(
Promise.resolve({ data: [{ id: 5 }, { id: 6 }, { id: 7 }] })
);
const { dispatch } = renderWithRedux(
renderWithRedux(
<DataProviderContext.Provider value={dataProvider}>
<UseGetMany resource="posts" ids={[1, 2]} />
<UseGetMany resource="posts" ids={[2, 3]} />
Expand Down
5 changes: 3 additions & 2 deletions packages/ra-core/src/dataProvider/useGetMany.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,9 @@ const callQueries = debounce(() => {
loaded: true,
}));
if (onSuccess) {
const subData = ids.map(id =>
response.data.find(datum => datum.id == id)
const subData = ids.map(
id =>
response.data.find(datum => datum.id == id) // eslint-disable-line eqeqeq
);
onSuccess({ data: subData });
}
Expand Down
42 changes: 27 additions & 15 deletions packages/ra-ui-materialui/src/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,41 @@ import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { Link as RRLink } from 'react-router-dom';
import { withStyles, createStyles } from '@material-ui/core/styles';
import { makeStyles } from '@material-ui/core/styles';

const styles = theme =>
createStyles({
link: {
textDecoration: 'none',
color: theme.palette.primary.main,
},
});
const useStyles = makeStyles(theme => ({
link: {
textDecoration: 'none',
color: theme.palette.primary.main,
},
}));

/**
* @deprecated Use react-router-dom's Link instead
*/
const Link = ({ to, children, className, classes, ...rest }) => (
<RRLink to={to} className={classNames(classes.link, className)} {...rest}>
{children}
</RRLink>
);
const Link = ({
to,
children,
classes: classesOverride,
className,
...rest
}) => {
const classes = useStyles({ classes: classesOverride });
return (
<RRLink
to={to}
className={classNames(classes.link, className)}
{...rest}
>
{children}
</RRLink>
);
};

Link.propTypes = {
className: PropTypes.string,
classes: PropTypes.object,
children: PropTypes.node,
to: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
};

export default withStyles(styles)(Link);
export default Link;
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/field/FileField.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { SFC, ComponentType } from 'react';
import React, { SFC } from 'react';
import PropTypes from 'prop-types';
import get from 'lodash/get';
import { makeStyles } from '@material-ui/core/styles';
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-ui-materialui/src/field/ImageField.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { SFC, ComponentType } from 'react';
import React, { SFC } from 'react';
import PropTypes from 'prop-types';
import get from 'lodash/get';
import { makeStyles } from '@material-ui/core/styles';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ const CheckboxGroupInputItem = ({
className={classes.checkbox}
checked={
value
? value.find(v => v == get(choice, optionValue)) !==
undefined // eslint-disable-line eqeqeq
? value.find(
v => v == get(choice, optionValue) // eslint-disable-line eqeqeq
) !== undefined
: false
}
value={String(get(choice, optionValue))}
Expand Down
58 changes: 21 additions & 37 deletions packages/ra-ui-materialui/src/list/BulkDeleteAction.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,48 @@
import { Component } from 'react';
import { useEffect } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import compose from 'recompose/compose';
import { crudDeleteMany, startUndoable, translate } from 'ra-core';
import { useDispatch } from 'react-redux';
import { crudDeleteMany, startUndoable } from 'ra-core';

class BulkDeleteAction extends Component {
componentDidMount = () => {
/**
*@deprecated use BulkDeleteButton instead
*/
const BulkDeleteAction = props => {
const dispatch = useDispatch();

useEffect(() => {
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line no-console
console.warn(
'<BulkDeleteAction> is deprecated. Use the <BulkDeleteButton> component instead, via the bulkActionButton props.'
);
}
const {
basePath,
dispatchCrudDeleteMany,
resource,
selectedIds,
startUndoable,
undoable,
} = this.props;
const { basePath, resource, selectedIds, undoable, onExit } = props;
if (undoable) {
startUndoable(crudDeleteMany(resource, selectedIds, basePath));
dispatch(
startUndoable(crudDeleteMany(resource, selectedIds, basePath))
);
} else {
dispatchCrudDeleteMany(resource, selectedIds, basePath);
dispatch(crudDeleteMany(resource, selectedIds, basePath));
}
this.props.onExit();
};
onExit();
}, [dispatch, props]);

render() {
return null;
}
}
return null;
};

BulkDeleteAction.propTypes = {
basePath: PropTypes.string,
dispatchCrudDeleteMany: PropTypes.func.isRequired,
label: PropTypes.string,
onExit: PropTypes.func.isRequired,
resource: PropTypes.string.isRequired,
startUndoable: PropTypes.func,
selectedIds: PropTypes.arrayOf(PropTypes.any).isRequired,
translate: PropTypes.func.isRequired,
undoable: PropTypes.bool,
};

const EnhancedBulkDeleteAction = compose(
connect(
undefined,
{
startUndoable,
dispatchCrudDeleteMany: crudDeleteMany,
}
),
translate
)(BulkDeleteAction);

EnhancedBulkDeleteAction.defaultProps = {
BulkDeleteAction.defaultProps = {
label: 'ra.action.delete',
undoable: true,
};

export default EnhancedBulkDeleteAction;
export default BulkDeleteAction;
Loading