It's important to take note of this:
react-admin bases on material-ui to present almost all (if not all) it's components.
- To style the
react-admin components, you need to make use of the className props which exists on all components (inherited from material-ui).
You can do this in two basic way as follows:
- Material-ui styling:
import { createStyles, makeStyles } from '@material-ui/core';
const useStyles = makeStyles(() =>
createStyles({
root: {
width: '500px',
},
bool: {
width: '100%',
textAlign: 'center',
},
reason: {
textAlign: 'center',
},
}),
);
export const YourComponent = props => {
const classes = useStyles(); // initialize the styles
return (
<ArrayField source="bans" reference="bans" >
<Datagrid className={classes.root}>
<BooleanField label="isBanned" source="isBanned" className={classes.bool} />
<ReasonsField label="reason" source="reason" className={classes.reason} />
</Datagrid>
</ArrayField>
);
};
You can read a bit more about material-ui styling to appreciate it's dynamics.
- Normal CSS styling:
- Within your
src/index.js, you probably have something like this or slightly different:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import './index.css'; // this is the line of interest (update it to call your CSS file)
ReactDOM.render(<App />, document.getElementById('root'));
- Within your CSS file (for the case of this example,
src/index.css)
/* i've used css classes */
.column {
width: 500px;
}
.centered {
text-align: center;
}
.wide {
width: 100%;
}
- Within your component (of interest):
import clsx from 'clsx'; // install this "clsx" package - an alternative is "classnames"
export const YourComponent = props => {
return (
<ArrayField source="bans" reference="bans" >
<Datagrid className={clsx('column')}>
<BooleanField label="isBanned" source="isBanned" className={clsx('centered', 'wide')} />
<ReasonsField label="reason" source="reason" className={clsx('centered')} />
</Datagrid>
</ArrayField>
);
};
If you read more about clsx or classnames, you'll notice that you can even style your component based on some react/javascript logic - quite interesting stuff.