reactjs 材料UI对话框在打开时更改其他组件的样式

swvgeqrz  于 2023-03-08  发布在  React
关注(0)|答案(3)|浏览(123)

我有一个对话框组件,我通过AppBar组件上的一个按钮来显示它。每当我打开这个对话框时,导航栏元素的边距都会发生变化,我不知道为什么。使用开发工具检查html后,没有任何变化,组件上的任何元素都没有css变化。
任何关于如何正确调试此问题或在MUI文档中何处查找的建议都是有帮助的。
编辑:类.MuiTouchRipple-root附加到我的AddCircle组件。

import React from 'react';
import Navbar from './components/Navbar';
import Home from './pages/Home';
import Box from '@mui/material/Box';
import {
  BrowserRouter as Router,
  Switch,
  Route
} from "react-router-dom";
import { useState } from 'react';
import SelectInvoiceDialog from './components/SelectInvoiceDialog';
import ProcessInvoice from './pages/ProcessInvoice';

function App() {
  const [open, setOpen] = useState(false);
  const openSelectDialog = () => setOpen(true);
  const closeSelectDialog = () => setOpen(false);

  return (
  <Router>
    <Box>
      <Navbar openDialog={openSelectDialog}/>
      <Switch>
        <Route exact path="/process">
          <ProcessInvoice />
        </Route>
        <Route exact path="/">
          <Home />
        </Route>
      </Switch>
    </Box>
    <SelectInvoiceDialog open={open} closeAction={closeSelectDialog}/>
  </Router>
  );
}

export default App;
import React from "react";
import AppBar from '@mui/material/AppBar';
import Box from '@mui/material/Box';
import Toolbar from '@mui/material/Toolbar';
import Typography from '@mui/material/Typography';
import AddCircle from '@material-ui/icons/AddCircle'
import IconButton from '@mui/material/IconButton';
import MenuIcon from '@material-ui/icons/Menu';
import { makeStyles, createStyles } from "@material-ui/core";

const useStyles = makeStyles((theme) => createStyles({
    navBarStyle: {
        background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
        display: 'flex',
        flexDirection: 'row',
        color: 'white'
    }
}));

export default function Navbar ({ openDialog }) {
    const classes = useStyles();
    return (
        <Box>
            <AppBar position="static">
                <Toolbar className={classes.navBarStyle}>
                    <IconButton
                        size="large"
                        edge="start"
                        color="inherit"
                        aria-label="menu"
                    >
                        <MenuIcon />
                    </IconButton>
                    <Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>Invoice processor</Typography>
                    <IconButton
                        size="large"
                        color="inherit"
                        onClick={openDialog}
                    >
                        <AddCircle />
                    </IconButton>
                </Toolbar>
            </AppBar>
        </Box>
    );
}
import Button from '@material-ui/core/Button';
import Dialog from '@material-ui/core/Dialog';
import DialogActions from '@material-ui/core/DialogActions';
import DialogContent from '@material-ui/core/DialogContent';
import { DialogTitle, Select, MenuItem, FormControl, InputLabel, Box, TextField, Typography} from "@mui/material";
import { Link } from 'react-router-dom'
import { useState } from 'react';

export default function SelectInvoiceDialog ({ open, closeAction }) {
    const [filePath, setFilePath] = useState('');
    const [selection, setSelection] = useState('');

    const handleChange = (e) => setSelection(e.target.value)

    return (
            <Dialog 
                open={open} 
                onClose={closeAction}
                fullWidth
                disableEnforceFocus
                >
                <DialogTitle>Process Invoice</DialogTitle>
                <DialogContent>
                    <FormControl fullWidth>
                        <InputLabel id="selectTemplateLabel">Template</InputLabel>
                        <Select
                            labelId="selectTemplateLabel"
                            id="selectTemplate"
                            value={selection}
                            onChange={handleChange}
                            label="Template"
                        >
                            <MenuItem value={'DELL'}>DELL</MenuItem>
                            <MenuItem value={'AI'}>Automatic AI</MenuItem>
                            <MenuItem value={'30'}>Thirty</MenuItem>
                        </Select>
                    </FormControl>
                    <FormControl fullWidth>
                        <TextField label="Debtor Number"/>
                    </FormControl>
                    <FormControl>
                        <Typography>{filePath ? 'File name:' : ''} {filePath}</Typography>
                        <Button
                            variant="contained"
                            component="label"
                            size="large"
                        >
                            SELECT FILE
                            <input type="file" hidden onChange={(e) => setFilePath(e.target.files[0].name)}/>
                        </Button>
                    </FormControl>
                    <DialogActions>
                        <Button 
                        variant="contained" 
                        onClick={() => {
                            closeAction();
                            setFilePath('');
                        }}
                        component={Link} 
                        to={'/process'}
                        >Process</Button>
                    </DialogActions>
                </DialogContent>
            </Dialog>
    );
}
31moq8wy

31moq8wy1#

很有可能是因为您混用了@mui和@material-ui。

798qvoo8

798qvoo82#

我通过在html正文中添加一些css来修复这个问题。

body {
  margin: 0;
  padding: 0;
}
okxuctiv

okxuctiv3#

正如@Jeffrey Pinyan所说,混合导入可能会破坏样式。

'@material-ui/core/....' represents older version of MUI (means that you import an old version of...whatever)
'@mui/material/....' represents the new version of MUI ( this is how you should import ... whatever)`

我只需要使用相同的位置导入MUI组件,就可以了

相关问题