Create class with common imports material-ui and react

1

I would like to create a common class in my application with react and material-ui. I have, for example, that create a series of elements with tables. Every time I use I have to import some libs. For example:

const Table = require('material-ui/lib/table/table');
const TableBody = require('material-ui/lib/table/table-body');
const TableFooter = require('material-ui/lib/table/table-footer');
const TableHeader = require('material-ui/lib/table/table-header');
const TableHeaderColumn = require('material-ui/lib/table/table-header-column');
const TableRow = require('material-ui/lib/table/table-row');
const TableRowColumn = require('material-ui/lib/table/table-row-column');

I would like to do something similar there:

/* <path>/Template/index.jsx */

const React = require('react'),
ReactDOM    = require('react-dom'),
Router      = require('react-router'),
mui         = require('material-ui'),
Icon        = mui.Icon;

class Template extends React.Component {
    render() {
        return (
            {this.props.element}
        );
    }
};

module.exports = Template;

/* <path>/Table/index.jsx */

const Template    = require('../template'),
Table             = require('material-ui/lib/table/table'),
TableBody         = require('material-ui/lib/table/table-body'),
TableFooter       = require('material-ui/lib/table/table-footer'),
TableHeader       = require('material-ui/lib/table/table-header'),
TableHeaderColumn = require('material-ui/lib/table/table-header-column'),
TableRow          = require('material-ui/lib/table/table-row'),
TableRowColumn    = require('material-ui/lib/table/table-row-column');

class Table extends Template {
    render() {
        return (
            <Template element={this.props.element}/>
        );
    }
};

module.exports = Table;

/* <path>/home/index.jsx */

var Table = require('../../../common/table');

var _rows = [];
for (var i = 1; i < 100; i++) {
    _rows.push({
        id: i,
        title: 'Title ' + i,
        count: i * 100
    });
}

class Item extends Table {
    handleOnClick: function(event) {
        event.preventDefault();
        this.props.setApplicationDetails("item", this.props.item);
    },

    render: function() {
        return (
            <TableRow selected={true}>
                <TableRowColumn>
                    <a href="#" onClick={this.handleOnClick}> {this.props.item.id}</a>
                    <i className="fa fa-caret-right"></i>
                </TableRowColumn>
                <TableRowColumn>{this.props.item.Title}</TableRowColumn>
            </TableRow>
        );
    }
};

class ItemItem extends Table{
    mixins: [Router.Navigation, Router.State],
    setApplicationDetails: function(pageData, item) {
        this.transitionTo("/home?id=" + item.id);
    },
    render: function() {
        var self = this;
        var state = {
            fixedHeader: true,
            stripedRows: false,
            showRowHover: false,
            selectable: true,
            multiSelectable: false,
            enableSelectAll: false,
            deselectOnClickaway: true,
            height: '300px',
        };

        var item = _rows.map(function(item, index) {
            return (
                <Item item={item} index={++index} setApplicationDetails={self.setApplicationDetails} key={index} />
            );
        });

        return (
            <Table height={state.height} fixedHeader={state.fixedHeader}
                fixedFooter={state.fixedFooter} selectable={state.selectable}
                multiSelectable={state.multiSelectable}>
                <TableHeader enableSelectAll={state.enableSelectAll}>
                    <TableRow>
                        <TableHeaderColumn colSpan="3" tooltip='Super Header' style={{textAlign: 'center'}}>
                        Super Header
                        </TableHeaderColumn>
                    </TableRow>
                    <TableRow>
                        <TableHeaderColumn tooltip='The ID'>ID</TableHeaderColumn>
                        <TableHeaderColumn tooltip='The Name'>Name</TableHeaderColumn>
                        <TableHeaderColumn tooltip='The Status'>Status</TableHeaderColumn>
                    </TableRow>
                </TableHeader>
                <TableBody deselectOnClickaway={state.deselectOnClickaway}
                    showRowHover={state.showRowHover} stripedRows={state.stripedRows}>
                    <TableRow selected={true}>
                        <TableRowColumn>1</TableRowColumn>
                        <TableRowColumn>John Smith</TableRowColumn>
                        <TableRowColumn>Employed</TableRowColumn>
                    </TableRow>
                </TableBody>
            </Table>
        );
    }
};

module.exports = ItemItem;

That is, create a class that encapsulates these libs and then on each element that uses extender table this lib.

Any suggestions?

    
asked by anonymous 04.12.2015 / 10:58

1 answer

1

It is possible but somehow it is not recommended, I will explain later why, but what you need is something like this:

Put this in a file as components/table/index.js , so you can only import it using the folder name, but this is optional okay? So you have two options:

export const { Table } = require('material-ui/lib/table/table');
export const { TableBody } = require('material-ui/lib/table/table-body');
export const { TableFooter } = require('material-ui/lib/table/table-footer');
export const { TableHeader } = require('material-ui/lib/table/table-header');
export const { TableHeaderColumn } = require('material-ui/lib/table/table-header-column');
export const { TableRow } = require('material-ui/lib/table/table-row');
export const { TableRowColumn } = require('material-ui/lib/table/table-row-column');

or

export.Table = require('material-ui/lib/table/table');
export.TableBody = require('material-ui/lib/table/table-body');
export.TableFooter = require('material-ui/lib/table/table-footer');
export.TableHeader = require('material-ui/lib/table/table-header');
export.TableHeaderColumn = require('material-ui/lib/table/table-header-column');
export.TableRow = require('material-ui/lib/table/table-row');
export.TableRowColumn = require('material-ui/lib/table/table-row-column');

Then you can load them like this:

import { * as TableElements } from './components/table'

or

const TableElements = require('./components/table')

And to use them: TableElement.Table , or TableElement.TableBody and so on.

Now why is it not recommended?

If you are using a task runner like gulp or grunt, this will not really make a difference, but if you are using webpack, you may end up injecting more code into your final project than it should.

In a simple way, the webpack is smart enough to import and inject into your project, just the modules you are using, so if for example you are using only a few modules of the material, just the modules you will be added to your final project. In the case of gulp or grunt, they import the entire ui material for your project and in the end you will have module that you may not even be using, making unnecessary volume in your project.

I hope you have helped.

    
11.05.2016 / 16:13