React高阶组件的实战应用

近期项目中对于展示数据的table有一个特殊需求:
1.数据不够时,要求table的高度撑满页面空白区域
2.没有数据时展示暂无数据的logo


image.png

第一个思路肯定是写一个自定义公共组件,包裹一个table,在组件内部设置table的特殊样式,和自定义方法。

高阶组件为可以说是操作props的最优方案。关于原理和使用方法,官网文档介绍非常翔实,https://react.docschina.org/docs/higher-order-components.html。本文介绍一下实际运用。

我定义一个高级组件,命名为EmptyTableHOC,代码如下

import React, {Component} from 'react';
import {Table, Button} from 'antd';
import PropTypes from 'prop-types';

const EmptyTableHOC = (WrappedComponent) =>
    class Wrapper extends Component {
        emptyText = (height) => {
            if (this.props.emptyBtnText) {
                let loading = this.props.emptyBtnLoading
                return (
                    <div style={{height: (height - 32) + 'px'}}>
                        <i className='icon font_family icon-pic-empty empty-icon-style'></i>
                        <div>暂无数据</div>
                        <div style={{marginTop: '20px'}}>
                            <Button loading={loading} type="primary"
                                    onClick={() => this.props.emptyBtnAction()}>{this.props.emptyBtnText}</Button>
                        </div>
                    </div>
                )
            } else {
                return (
                    <div style={{height: (height - 32) + 'px'}}>
                        <i className='icon font_family icon-pic-empty empty-icon-style'></i>
                        <div>暂无数据</div>
                    </div>
                )
            }
        }

        componentDidMount() {

        }

        componentWillReceiveProps = (nextProps) => {
            if (document.getElementsByClassName('ant-table-body') && nextProps.dataSource && nextProps.dataSource.length > 0) {
                let height = this.props.tableBodyHeight + 'px';
                document.getElementsByClassName('ant-table-body')[0].style.height = height;
                document.getElementsByClassName('ant-table-body')[0].style.maxHeight = height;
            } else {
                if (document.getElementsByClassName('ant-table-body') && this.props.dataSource.length != 0) {
                    document.getElementsByClassName('ant-table-body')[0].style.height = 0;
                }
            }
        }

        render() {
            const {components, tableBodyHeight} = this.props;
            return <WrappedComponent
                {...this.props}
                components={components ? components : {body: {}}}
                locale={{emptyText: () => this.emptyText(tableBodyHeight)}}
            />;
        }
    };
export default EmptyTableHOC;

使用方式如下:

import React, {Component} from 'react';
import {Modal, Table} from 'antd';
import EmptyTableHOC from "./emptyTableLynn";
const MytableC = EmptyTableHOC(Table);
export default class CopyMetaDataModal extends Component {
    constructor(props) {
        super(props);
        this.state = {
            selectedIds: [],
            dataSource: []
        }
    }
    render() {
        const rowSelection = {
            onChange: this.onChange,
            selectedRowKeys: this.state.selectedIds
        };
        return (
            <div>
                <Modal
                    title="主数据模型"
                    visible={this.props.visible}
                    onOk={this.handleSubmit}
                    onCancel={this.props.handleCancel}
                    okText="确认"
                    cancelText="取消"
                >
                    <MytableC
                        columns={columns}
                        rowSelection={rowSelection}
                        dataSource={this.state.dataSource}
                        className="mapping-table-scroll"
                        indentSize={20}
                        pagination={false}
                        tableBodyHeight={330}
                        scroll={{ y: 330 }}
                    />
                </Modal>
            </div>
        )
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,277评论 3 119
  • 这两天被滴滴收购优步中国的事情刷屏了,我也来发表一下自己的看法吧:一句话就是滴滴赢了,Uber小看滴滴了! 我们来...
    abf9c7dfa3b1阅读 280评论 0 0
  • 这幅年画,最能体现年味!
    淮澜阅读 285评论 0 1
  • 我一个人吃饭旅行到处走走停停 十月一国庆节室友们都回家了,由于没买到票,可怜的我,所以晚了一天回家,就在十月一这天...
    樱肆阅读 323评论 14 3
  • 人生像一条从港口出发的船,在旅途中能看到美丽的夕阳翱翔的海鸥遨游的海豚,在旅途中同样也能看到险壑的冰...
    流年若溪阅读 173评论 0 0

友情链接更多精彩内容