gpt4 book ai didi

reactjs - ButtonGroup 内的按钮组件

转载 作者:行者123 更新时间:2023-12-03 13:44:59 28 4
gpt4 key购买 nike

我正在使用 React 16.9.0 和 Material-UI 4.4.2,但遇到以下问题。

我想渲染一个 ButtonGroup,其中包含 Button 元素,但这些按钮来自其他自定义组件,这些组件返回一个 Button 渲染,其中包含链接到该按钮的模态视图。问题是,我无法使它们看起来像具有相同样式的 ButtonGroup,因为 Button 元素似乎只采用“分组”样式,而不采用“视觉”样式。

重现行为的示例代码:

<ButtonGroup variant="outlined">
<AModal/>
<BModal/>
<CModal/>
</ButtonGroup>

Output

如您所见,渲染输出看起来并不符合预期。请记住,我正在使用 outlined 变体定义按钮,因为如果不是,它们只会呈现为文本按钮。

非常感谢任何帮助

根据要求添加AModal:

import React from 'react';
import { makeStyles } from '@material-ui/core/styles';
import { Button } from '@material-ui/core';
import Modal from '@material-ui/core/Modal';
import Backdrop from '@material-ui/core/Backdrop';
import Fade from '@material-ui/core/Fade';
import InnerModalComponent from './InnerModalComponent';

const useStyles = makeStyles((theme) => ({
modal: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
},
paper: {
backgroundColor: theme.palette.background.paper,
border: '2px solid #000',
boxShadow: theme.shadows[5],
padding: theme.spacing(2, 4, 3),
},
}));

export default function AModal() {
const classes = useStyles();
const [open, setOpen] = React.useState(false);

function handleOpen() {
setOpen(true);
}

function handleClose() {
setOpen(false);
}

return (
<div>
<Button variant="contained" onClick={handleOpen}> A </Button>
<Modal
aria-labelledby="transition-modal-title"
aria-describedby="transition-modal-description"
className={classes.modal}
open={open}
onClose={handleClose}
closeAfterTransition
BackdropComponent={Backdrop}
BackdropProps={{ timeout: 500 }}
>
<Fade in={open}>
<div className={classes.paper}>
<div
style={{
display: 'flex',
flexDirection: 'row',
alignItems: 'stretch',
justifyContent: 'center',
}}
>
<InnerModalComponent/>
</div>
<Button variant="contained" color="secondary" style={{ marginTop: '10px' }}> Button inside Modal</Button>
</div>
</Fade>
</Modal>
</div>
);
}

最佳答案

有两个主要问题:

  1. 您正在添加 div每个按钮周围。这会稍微干扰造型。将其更改为 fragment (例如 <><React.Fragment> )。

  2. 这样ButtonGroup作品来自 cloning the child Button elements并添加 Prop 来控制样式。当您在中间引入自定义组件时,您需要传递到 Button您的自定义组件未使用的任何 Prop 。

这是一个工作示例:

import React from "react";
import ReactDOM from "react-dom";

import ButtonGroup from "@material-ui/core/ButtonGroup";
import Button from "@material-ui/core/Button";
import Modal from "@material-ui/core/Modal";

const AModal = props => {
return (
<>
<Button {...props}>A</Button>
<Modal open={false}>
<div>Hello Modal</div>
</Modal>
</>
);
};
const OtherModal = ({ buttonText, ...other }) => {
return (
<>
<Button {...other}>{buttonText}</Button>
<Modal open={false}>
<div>Hello Modal</div>
</Modal>
</>
);
};
// I don't recommend this approach due to maintainability issues,
// but if you have a lint rule that disallows prop spreading, this is a workaround.
const AvoidPropSpread = ({
className,
disabled,
color,
disableFocusRipple,
disableRipple,
fullWidth,
size,
variant
}) => {
return (
<>
<Button
className={className}
disabled={disabled}
color={color}
disableFocusRipple={disableFocusRipple}
disableRipple={disableRipple}
fullWidth={fullWidth}
size={size}
variant={variant}
>
C
</Button>
<Modal open={false}>
<div>Hello Modal</div>
</Modal>
</>
);
};
function App() {
return (
<ButtonGroup>
<AModal />
<OtherModal buttonText="B" />
<AvoidPropSpread />
</ButtonGroup>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Edit ButtonGroup with Modals

关于reactjs - ButtonGroup 内的按钮组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57962146/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com