React使用“re-resizable”实现dom的拉伸缩放效果

目录

 

一、需求

二、插件的安装使用

三、事件

四、案例

五、项目地址 


一、需求

        在工作中遇到一个需求,做一个类似于调试工具栏的效果,点击某个按钮呼出,并且组件可以很便捷的实现可拖拽改变组件大小。

二、插件的安装使用

插件安装:

npm install --save re-resizable

插件引入:

import { Resizable } from "re-resizable";

基本使用:

import React, {Component} from 'react';
import { Resizable } from "re-resizable";

export default class demo extends Component {
    render() {
        return (
            <Resizable
                defaultSize={{width:400, height:400}}
            >
                你好,大聪明!
            </Resizable>
        );
    }
}

常用属性:

属性说明数据类型
defaultSize初始化默认宽高string / number
minWidth / maxWidth最小/最大宽string / number
minHeight / maxHeight最小/最大高string / number

size

设置宽高的数值object

...

......

更多使用方法详见官网。

https://github.com/bokuweb/re-resizablehttps://github.com/bokuweb/re-resizable

三、事件

  • onResizeStart 调整组件开始时调用。

  • onResize 调整组件进行时调用。

  • onResizeStop 调整组件完成时调用。

代码:

import React, {Component} from 'react';
import { Resizable } from "re-resizable";

export default class Demo extends Component {

    onResizeStart = (e) => {
        console.log("onResizeStart执行");
        console.log(e);
    };
    onResize = (e) => {
        console.log("onResize执行");
        console.log(e);
    };
    onResizeStop = (e) => {
        console.log("onResizeStop执行");
        console.log(e);
    };

    render() {
        return (
            <Resizable
                style={{background: "red"}}
                defaultSize={{width:400, height:400}}
                onResize={(e) => this.onResize(e)}
                onResizeStart={(e) => this.onResizeStart(e)}
                onResizeStop={(e) => this.onResizeStop(e)}
            >
                你好,大聪明!
            </Resizable>
        );
    }
}

四、案例

这是需求完成以后,页面的整体代码。

import { Button, Icon, Tooltip } from 'antd';
import { connect } from 'dva';
import { Link, routerRedux } from 'dva/router';
import { formatMessage, FormattedMessage } from 'umi-plugin-locale';
import React, { Component } from 'react';
import { Resizable } from "re-resizable";
import WebConsole from './WebConsole';
import globalUtil from '../../utils/global';
import styles from './index.less'


@connect(null, null, null, { withRef: true })
class Shell extends Component {
    constructor(props) {
        super(props)
        this.state = {
            bool: false,
            height: 400
        };
    }
    onResizeStart = (e) => {
    };
    onResize = (e) => {
    };
    // 收起隐藏
    packUpOrDown = (e) => {
        const { bool } = this.state
        if (bool) {
            this.setState({
                height: 450
            })
        } else {
            this.setState({
                height: 40
            })
        }
        this.setState({
            bool: !this.state.bool,
        })
    }
    // 退出shell终端
    terminalRepeal = () => {
        const { dispatch } = this.props
        dispatch({
            type: 'region/terminalRepeal',
            payload: true,
        });
    }
    // 新开页
    newPage = () =>{
       const { dispatch } = this.props
        dispatch({
            type: 'region/terminalRepeal',
            payload: true,
        });
    }
    render() {
        const { bool, height } = this.state
        const eid = globalUtil.getCurrEnterpriseId();
        return (
            <Resizable
                ref="big"
                style={{ position: 'absolute', bottom: 0, zIndex: 9999 }}
                defaultSize={{ height: 400 }}
                size={{ height: height }}
                onResize={(e) => this.onResize(e)}
                onResizeStart={(e) => this.onResizeStart(e)}
                onResizeStop={(e, direction, ref, d) => {
                    this.setState({
                        height: this.state.height + d.height,
                    });
                }}
                minWidth={'100%'}
                minHeight={40}
                maxHeight={'60vh'}
            >
                <div className={styles.shell_head}>
                    <Tooltip placement="top" title={bool ? formatMessage({ id: 'otherEnterprise.shell.show' }) : formatMessage({ id: 'otherEnterprise.shell.Pack_up' })} >
                        <Button type="primary" icon={bool ? "vertical-align-top" : "vertical-align-bottom"} onClick={this.packUpOrDown} style={{ margin: '0 20px 0 0' }} />
                    </Tooltip>
                    <Link
                        to={`/enterprise/${eid}/shell`}
                        target="_blank"
                    >
                        <Tooltip placement="top" title={formatMessage({ id: 'otherEnterprise.shell.new' })} >
                            <Button type="primary" style={{ margin: '0 20px 0 0' }} icon="arrows-alt" onClick={this.newPage}>
                            </Button>
                        </Tooltip>
                    </Link>

                    <Tooltip placement="top" title={formatMessage({ id: 'otherEnterprise.shell.down' })}>
                        <Button type="primary" icon={"close"} onClick={this.terminalRepeal} style={{ margin: '0 20px 0 0' }} />
                    </Tooltip>
                </div>
                <div style={{ padding: "24px 24px 0 24px", height: "100%" }}>
                    <WebConsole height={height} />
                </div>
            </Resizable>

        );
    }
}

export default Shell;

效果:

五、项目地址 

主项目地址:

GitHub - goodrain/rainbond: Cloud native multi cloud application management platform | 云原生多云应用管理平台

前端项目地址:

GitHub - goodrain/rainbond-ui: Rainbond front-end project

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用antd-mobile和react-router-dom实现tabs功能。首先,你需要安装antd-mobile和react-router-dom依赖包。 ```shell npm install antd-mobile react-router-dom ``` 接下来,你可以创建一个Tabs组件,并在其中使用antd-mobile的TabBar组件来实现tabs布局。然后,使用react-router-dom的Route组件来定义每个tab对应的页面。 ```jsx import React from 'react'; import { TabBar } from 'antd-mobile'; import { BrowserRouter as Router, Route, Link } from 'react-router-dom'; const Tabs = () => { const tabItems = [ { title: 'Tab 1', path: '/tab1', component: Tab1 }, { title: 'Tab 2', path: '/tab2', component: Tab2 }, { title: 'Tab 3', path: '/tab3', component: Tab3 }, ]; const renderTabs = () => { return tabItems.map((item) => ( <TabBar.Item key={item.path} title={item.title} icon={<div />} selectedIcon={<div />} selected={window.location.pathname === item.path} onPress={() => { window.location.href = item.path; }} /> )); }; return ( <Router> <div style={{ position: 'fixed', width: '100%', bottom: 0 }}> <TabBar>{renderTabs()}</TabBar> </div> {/* Define routes */} {tabItems.map((item) => ( <Route key={item.path} path={item.path} component={item.component} /> ))} </Router> ); }; const Tab1 = () => <div>Tab 1 content</div>; const Tab2 = () => <div>Tab 2 content</div>; const Tab3 = () => <div>Tab 3 content</div>; export default Tabs; ``` 在上面的代码中,我们定义了三个tab,分别对应不同的路径和组件。通过点击tab,我们可以切换显示不同的内容。你可以根据自己的需求修改tab的数量、标题、路径和对应的组件。 希望这可以帮助你实现antd-mobile和react-router-dom的tabs功能!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大聪明码农徐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值