React-Admin:如何从AppBar隐藏刷新按钮?

时间:2019-08-15 14:10:37

标签: react-admin admin-on-rest

我正在基于react-admin创建admin ui,目前正在寻找一种解决方案来隐藏AppBar中的刷新按钮。 禁用导出按钮很简单(exporter={false})。 RefreshButton是否同样简单?我找不到任何有效的解决方案。

我的React-admin版本:2.9.3

我知道您可以使用List组件上的“ actions”道具自定义动作,但是由于将“刷新”按钮移到了应用栏上,因此这似乎已经过时了。

如果需要,我可以将代码提供给自定义AppBar

3 个答案:

答案 0 :(得分:1)

没有简单的解决方案,在AppBar中未配置LoadingIndicator组件的存在: https://github.com/marmelab/react-admin/blob/master/packages/ra-ui-materialui/src/layout/AppBar.js

您可以实现AppBar组件,然后将其连接到AppLayout:

// Layout.js

import React from 'react';
import { Layout } from 'react-admin';
import AppBarEx from './components/AppBarEx';

const AppLayout = (props) => ( 
  <Layout
    {...props}
    appBar={AppBarEx}
  />
);

export default AppLayout;

// AppBarEx.js

import React, { Children, cloneElement } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import classNames from 'classnames';
import MuiAppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import IconButton from '@material-ui/core/IconButton';
import Typography from '@material-ui/core/Typography';
import { withStyles, createStyles } from '@material-ui/core/styles';
import MenuIcon from '@material-ui/icons/Menu';
import withWidth from '@material-ui/core/withWidth';
import compose from 'recompose/compose';
import { toggleSidebar as toggleSidebarAction } from 'ra-core';
import { UserMenu, Headroom } from 'react-admin';

const styles = theme =>
    createStyles({
        toolbar: {
            paddingRight: 24,
        },
        menuButton: {
            marginLeft: '0.5em',
            marginRight: '0.5em',
        },
        menuButtonIconClosed: {
            transition: theme.transitions.create(['transform'], {
                easing: theme.transitions.easing.sharp,
                duration: theme.transitions.duration.leavingScreen,
            }),
            transform: 'rotate(0deg)',
        },
        menuButtonIconOpen: {
            transition: theme.transitions.create(['transform'], {
                easing: theme.transitions.easing.sharp,
                duration: theme.transitions.duration.leavingScreen,
            }),
            transform: 'rotate(180deg)',
        },
        title: {
            flex: 1,
            textOverflow: 'ellipsis',
            whiteSpace: 'nowrap',
            overflow: 'hidden',
        },
    });

const AppBar = ({
    children,
    classes,
    className,
    logo,
    logout,
    open,
    title,
    toggleSidebar,
    userMenu,
    width,
    ...rest
}) => (
    <Headroom>
        <MuiAppBar
            className={className}
            color="secondary"
            position="static"
            {...rest}
        >
            <Toolbar
                disableGutters
                variant={width === 'xs' ? 'regular' : 'dense'}
                className={classes.toolbar}
            >
                <IconButton
                    color="inherit"
                    aria-label="open drawer"
                    onClick={toggleSidebar}
                    className={classNames(classes.menuButton)}
                >
                    <MenuIcon
                        classes={{
                            root: open
                                ? classes.menuButtonIconOpen
                                : classes.menuButtonIconClosed,
                        }}
                    />
                </IconButton>
                {Children.count(children) === 0 ? (
                    <Typography
                        variant="title"
                        color="inherit"
                        className={classes.title}
                        id="react-admin-title"
                    />
                ) : (
                    children
                )}
                {cloneElement(userMenu, { logout })}
            </Toolbar>
        </MuiAppBar>
    </Headroom>
);

AppBar.propTypes = {
    children: PropTypes.node,
    classes: PropTypes.object,
    className: PropTypes.string,
    logout: PropTypes.element,
    open: PropTypes.bool,
    title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]).isRequired,
    toggleSidebar: PropTypes.func.isRequired,
    userMenu: PropTypes.node,
    width: PropTypes.string,
};

AppBar.defaultProps = {
    userMenu: <UserMenu />,
};

const enhance = compose(
    connect(
        state => ({
            locale: state.i18n.locale, // force redraw on locale change
        }),
        {
            toggleSidebar: toggleSidebarAction,
        }
    ),
    withStyles(styles),
    withWidth()
);

export default enhance(AppBar);

答案 1 :(得分:0)

对于那些只想隐藏刷新按钮的人,我通过拥有自己的自定义主题来实现(有关更多信息,请参见react admin theming documentation page)。

例如,我有两个主题,一个是明暗主题。 这是我如何使用CSS定位刷新按钮的示例:

 export const lightTheme = {
    palette: {
        background: {
            paper: "#fff",
            default: "#fff"
        },
        primary: {
            main: '#607D8B'
        },
        secondary: {
            main: '#607D8B'
        },
        sidebarColor: '#E7ECEE',
        menuItemsColor: '#4B6471'
    },

// overrides default theme css
    overrides: {
        // targeting refresh button
        RaAppBar: {
            toolbar: {
                '& button': {
                    '&:not(:nth-child(1))': {
                        display: 'none'
                    }
                }
            }
        }
    }
};

这不是一个好方法,但是由于某种原因,如果使用:nth-​​child(1),则同时定位:抽屉(应用程序栏中的第一个按钮)和自定义配置文件按钮,后者位于html DOM的最后(查看图片)下面)。

targeting using :nth-child(1) IMG

因此,我想到一个使用:not((:nth-​​child(1))的想法,如果您的目标是隐藏它,则可以解决该问题,但是您也可以使用其他CSS属性对其进行操作。

答案 2 :(得分:0)

也不太理想,但我将其定位为“刷新”咏叹调标签。我通常不会采用这种方法,但这是区分React Admin CSS中按钮的唯一方法。

header.MuiPaper-root button[aria-label="Refresh"] {
  display: none;
}