react封装一个可自定义内容的modal弹框组件

使用方法:

npm i react-component-modal -S

import CustomModal from 'react-component-modal';

constructor(props) {
    super(props);
    this.handleClose = this.handleClose.bind(this);
    this.closeModal = this.closeModal.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.state = {
        visible: false
    };
}

render(){
  const { visible } = this.state;
  return (
    <CustomModal visible={visible} title="标题" negative_button_text="取消" positive_button_text="确定" hideModal={this.closeModal} handleSubmit={this.handleSubmit}>
        <div>
            <p>自定义内容</p>
        </div>
    </CustomModal>
  )
}

showModal() {
    this.setState({visible: true});
}
closeModal() {
    this.setState({visible: false});
}
handleSubmit() {
    this.setState({visible: false});
    //
}

组件地址github

部分源码:

import React, {Component} from 'react';
import PropTypes from 'prop-types';
import { Modal } from 'antd-mobile';

class CustomModal extends Component {
    constructor(props) {
        super(props);
        this.hideModal = this.hideModal.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
    }
    render() {
        const { visible, title, negative_button_text, positive_button_text, children } = this.props;
        return (
            <Modal
                visible={visible}
                transparent
                maskClosable={false}
                title={title}
                onClose={this.hideModal}
                footer={[
                    { text: negative_button_text, onPress: this.hideModal},
                    { text: positive_button_text, onPress: this.handleSubmit} 
                ]}
                wrapProps={{ onTouchStart: this.onWrapTouchStart }}
            >
                {children}
            </Modal>
        );
    }

    hideModal() {//hide modal
        const {hideModal} = this.props;
        hideModal();
    }

    handleSubmit() {//submit
        const {handleSubmit} = this.props;
        handleSubmit();
    }
}

CustomModal.propTypes = {//参数类型及是否必传
    visible: PropTypes.bool,
    title: PropTypes.string,
    negative_button_text: PropTypes.string.isRequired,
    positive_button_text: PropTypes.string.isRequired,
    children: PropTypes.node //自定义内容
};

CustomModal.defaultProps = {//默认参数
    visible: false,
    title: '标题',
    negative_button_text: '取消',
    positive_button_text: '确定'
};

export default CustomModal;

组件地址github

更多angular1/2/4/5、ionic1/2/3、react、vue、微信小程序、nodejs等技术文章、视频教程和开源项目,请关注微信公众号——全栈弄潮儿

微信公众号.png

前端最火框架排行榜——小程序二维码

前端排行榜.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过在 React 中使用 Portal 来实现弹窗。Portal 可以将组件渲染到 DOM 树中的另一个位置,这就允许我们在页面的任意位置渲染弹窗。 下面是一个简单的代码示例,演示如何使用 Portal 封装一个自定义内容弹窗方法: ```jsx import React, { useState } from 'react'; import ReactDOM from 'react-dom'; const Modal = ({ onClose, children }) => { return ReactDOM.createPortal( <div className="modal-overlay"> <div className="modal"> <button onClick={onClose}>Close</button> {children} </div> </div>, document.body ); }; const App = () => { const [showModal, setShowModal] = useState(false); return ( <div> <button onClick={() => setShowModal(true)}>Open Modal</button> {showModal && ( <Modal onClose={() => setShowModal(false)}> <h1>Custom Content</h1> <p>This is some custom content for the modal.</p> </Modal> )} </div> ); }; ReactDOM.render(<App />, document.getElementById('root')); ``` 这个示例中,我们定义了一个 Modal 组件,它接收两个 props:onClose 和 children。onClose 是一个回调函数,用于关闭弹窗;children 是一个传递给 Modal 组件自定义内容。 在 Modal 组件内部,我们使用 createPortal 将 Modal 组件内容渲染到 document.body 上,以实现弹窗效果。 在 App 组件中,我们使用 useState 来管理 showModal 状态,它控制着 Modal 组件是否显示。当用户点击 Open Modal 按钮时,我们将 showModal 设置为 true,从而显示 Modal 组件。当用户点击 Modal 中的 Close 按钮时,我们将 showModal 设置为 false,从而关闭 Modal 组件。 这个示例只是一个基本的弹窗实现方法,你可以根据自己的需求对 Modal 组件进行更多的定制,例如添加动画效果、调整样等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值