javascript - 如何使 Material-UI Dialog 可调整大小

标签 javascript css reactjs material-ui

我有一个元素需要一个可调整大小和可拖动的对话框。 Material-UI Dialog 文档包含如何使其可拖动的步骤。我想找出可调整大小的部分。关于如何做的任何建议?

示例代码可以找到here .

更新:

@Khabir 的功能和 typescript 版本下面的回答

    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 DialogContentText from '@material-ui/core/DialogContentText'
    import DialogTitle from '@material-ui/core/DialogTitle'
    import Paper, { PaperProps } from '@material-ui/core/Paper'
    import { makeStyles, Theme } from '@material-ui/core/styles'
    import TextField from '@material-ui/core/TextField'
    import React from 'react'
    import Draggable from 'react-draggable'
    import { ResizableBox } from 'react-resizable'

    const useStyles = makeStyles((theme: Theme) => ({
        resizable: {
            position: 'relative',
            '& .react-resizable-handle': {
                position: 'absolute',    
                width: 20,  
                height: 20,   
                bottom: 0,
                right: 0,
                background:"url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2IDYiIHN0eWxlPSJiYWNrZ3JvdW5kLWNvbG9yOiNmZmZmZmYwMCIgeD0iMHB4IiB5PSIwcHgiIHdpZHRoPSI2cHgiIGhlaWdodD0iNnB4Ij48ZyBvcGFjaXR5PSIwLjMwMiI+PHBhdGggZD0iTSA2IDYgTCAwIDYgTCAwIDQuMiBMIDQgNC4yIEwgNC4yIDQuMiBMIDQuMiAwIEwgNiAwIEwgNiA2IEwgNiA2IFoiIGZpbGw9IiMwMDAwMDAiLz48L2c+PC9zdmc+')",
                'background-position': 'bottom right',
                padding: '0 3px 3px 0',
                'background-repeat': 'no-repeat',
                'background-origin': 'content-box',
                'box-sizing': 'border-box',
                cursor: 'se-resize',
            },
        },
    }))

    const PaperComponent = (props: PaperProps) => {
        return (
            <Draggable
                handle="#draggable-dialog-title"
                cancel={'[class*="MuiDialogContent-root"]'}
            >
                <Paper {...props} />
            </Draggable>
        )
    }

    export const DialogComponent = () => {
        const [open, setOpen] = React.useState<boolean>(false)
        const handleClickOpen = () => {
            setOpen(true)
        }

        const handleClose = () => {
            setOpen(false)
        }

        const classes = useStyles()

        return (
            <div>
                <Button onClick={handleClickOpen}>Open dd form dialog</Button>
                {open && (
                    <Dialog
                        open={true}
                        onClose={handleClose}
                        maxWidth={false}
                        PaperComponent={PaperComponent}
                        aria-labelledby="draggable-dialog-title"
                    >
                        <ResizableBox
                            height={400}
                            width={600}
                            className={classes.resizable}
                        >
                            <DialogTitle
                                style={{ cursor: 'move' }}
                                id="draggable-dialog-title"
                            >
                                Subscribe
                            </DialogTitle>

                            <DialogContent>
                                <DialogContentText>
                                    To subscribe to this website, please enter your email address here. We will send updates occasionally.
                                </DialogContentText>

                                <TextField
                                    autoFocus
                                    margin="dense"
                                    id="name"
                                    label="Email Address"
                                    type="email"
                                    fullWidth
                                />
                            </DialogContent>

                            <DialogActions>
                                <Button onClick={handleClose} color="primary">
                                    Cancel
                                </Button>
                                <Button onClick={handleClose} color="primary">
                                    Subscribe
                                </Button>
                            </DialogActions>
                        </ResizableBox>
                    </Dialog>
                )}
            </div>
        )
    }

typescript 3.8.3
@material-ui/核心 4.9.7

最佳答案

嗨,我将两个功能合并在一起。请检查示例。它支持拖动和调整大小。

import React from "react";
import Button from "@material-ui/core/Button";
import Draggable from "react-draggable";
import {withStyles} from "@material-ui/core/styles";
import TextField from "@material-ui/core/TextField";
import Dialog from "@material-ui/core/Dialog";
import DialogActions from "@material-ui/core/DialogActions";
import DialogContent from "@material-ui/core/DialogContent";
import DialogContentText from "@material-ui/core/DialogContentText";
import DialogTitle from "@material-ui/core/DialogTitle";
import {ResizableBox} from "react-resizable";
import Paper from "@material-ui/core/Paper";

const styles = theme => ({
    resizable: {
        position: "relative",
        "& .react-resizable-handle": {
            position: "absolute",
            width: 20,
            height: 20,
            bottom: 0,
            right: 0,
            background:
                "url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2IDYiIHN0eWxlPSJiYWNrZ3JvdW5kLWNvbG9yOiNmZmZmZmYwMCIgeD0iMHB4IiB5PSIwcHgiIHdpZHRoPSI2cHgiIGhlaWdodD0iNnB4Ij48ZyBvcGFjaXR5PSIwLjMwMiI+PHBhdGggZD0iTSA2IDYgTCAwIDYgTCAwIDQuMiBMIDQgNC4yIEwgNC4yIDQuMiBMIDQuMiAwIEwgNiAwIEwgNiA2IEwgNiA2IFoiIGZpbGw9IiMwMDAwMDAiLz48L2c+PC9zdmc+')",
            "background-position": "bottom right",
            padding: "0 3px 3px 0",
            "background-repeat": "no-repeat",
            "background-origin": "content-box",
            "box-sizing": "border-box",
            cursor: "se-resize"
        }
    }
});

function PaperComponent(props) {
    return (
        <Draggable handle="#draggable-dialog-title" cancel={'[class*="MuiDialogContent-root"]'}>
            <Paper {...props} />
        </Draggable>
    );
}

class DraggableResizableDialog extends React.Component {
    state = {
        open: false
    };

    handleClickOpen = () => {
        this.setState({open: true});
    };

    handleClose = () => {
        this.setState({open: false});
    };

    render() {
        return (
            <div>
                <Button onClick={this.handleClickOpen}>Open dd form dialog</Button>
                {this.state.open && (
                    <Dialog
                        open={true}
                        onClose={this.handleClose}
                        maxWidth={false}
                        PaperComponent={PaperComponent}
                        aria-labelledby="draggable-dialog-title"
                    >
                        <ResizableBox
                            height={400}
                            width={600}
                            className={this.props.classes.resizable}
                        >
                            <DialogTitle style={{ cursor: 'move' }} id="draggable-dialog-title">Subscribe</DialogTitle>
                            <DialogContent>
                                <DialogContentText>
                                    To subscribe to this website, please enter your email address
                                    here. We will send updates occasionally.
                                </DialogContentText>
                                <TextField
                                    autoFocus
                                    margin="dense"
                                    id="name"
                                    label="Email Address"
                                    type="email"
                                    fullWidth
                                />
                            </DialogContent>
                            <DialogActions>
                                <Button onClick={this.handleClose} color="primary">
                                    Cancel
                                </Button>
                                <Button onClick={this.handleClose} color="primary">
                                    Subscribe
                                </Button>
                            </DialogActions>
                        </ResizableBox>
                    </Dialog>
                )}
            </div>
        );
    }
}

export default withStyles(styles)(DraggableResizableDialog);

来源: Draggable Resizable

关于javascript - 如何使 Material-UI Dialog 可调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61446605/

相关文章:

javascript - onclick按钮展开为表单

javascript - 多过滤器选择器查询

javascript - React - 单击按钮后根据用户输入的文本更新类组件中的状态

javascript - React : Cannot update during an existing state transition (such as within `render` ). 渲染方法应该是 props 和 state 的纯函数

javascript - React tic tac toe - 显示平局发生的时间

javascript - Bootstrap Navbar Toggle 无法正确呈现

javascript - 如何使用 JavaScript 从 div 中的输入字段获取输入值

javascript - 为什么我的简单 jQuery 不起作用?

javascript - 自动滚动到ajax获取结果的底部

javascript - 显示图像预览 + 显示尺寸