diff --git a/example/index.js b/example/index.js index 05d9ab002..f93f5738d 100644 --- a/example/index.js +++ b/example/index.js @@ -79,7 +79,7 @@ class Example extends React.Component { cell: { root: { - }, + }, sortLabel: { }, } @@ -87,11 +87,18 @@ class Example extends React.Component { }, body: { - row: { + main: { + + }, + row: { + root: { + color: "#FF0000" + } }, cell: { root: { + color: "#000" } } @@ -114,16 +121,10 @@ class Example extends React.Component { }, iconActive: { }, - search: { - }, searchIcon: { }, - searchText: { - }, - clearIcon: { - color: "#000", - border: "solid 1px #000" - }, + }, + filterView: { }, filterList: { root: { @@ -134,7 +135,22 @@ class Example extends React.Component { }, }, pagination: { + root: { + border: "solid 1px #000" + } + }, + viewColumns: { + }, + search: { + main: { + }, + searchIcon: { + }, + searchText: { + }, + clearIcon: { + }, } } }; diff --git a/package.json b/package.json index 566390a3a..8100b2920 100644 --- a/package.json +++ b/package.json @@ -76,7 +76,8 @@ "material-ui": "^1.0.0-beta.22", "material-ui-icons": "^1.0.0-beta.17", "prop-types": "^15.6.0", - "react": "^16.0.0" + "react": "^16.0.0", + "react-popper": "^0.7.4" }, "side-effects": false, "nyc": { diff --git a/src/DataStyles.js b/src/DataStyles.js new file mode 100644 index 000000000..49d912730 --- /dev/null +++ b/src/DataStyles.js @@ -0,0 +1,65 @@ +import React from "react"; +import PropTypes from "prop-types"; +import merge from "lodash.merge"; +import get from "lodash.get"; +import { withStyles } from "material-ui/styles"; + +const getStyle = (obj, name) => { + return get(obj, "styles." + name, null); +}; + +const stylePass = (displayName, setFn) => { + const result = class StylePass extends React.Component { + componentWillMount() { + setFn(this.props.classes); + } + + componentWillReceieveProps(nextProps) { + if (this.props.classes !== nextprops.classes) setFn(nextProps.classes); + } + + render() { + return this.props.children; + } + }; + result.displayName = displayName; + return result; +}; + +class DataStyles extends React.Component { + state = { + data: null, + }; + + setStyleClass = data => { + this.setState(() => ({ + styleData: data, + })); + }; + + constructor(props) { + super(props); + this.buildComponent(props); + } + + componentWillReceieveProps(nextProps) { + if (nextProps.styles !== this.props.styles) this.buildComponent(nextProps); + } + + buildComponent(props) { + const defaultStyles = props.defaultStyles ? props.defaultStyles : {}; + const finalStyles = merge(defaultStyles, props.styles); + + // just a pass-through + this.component = withStyles(finalStyles)(stylePass(props.name, this.setStyleClass)); + } + + render() { + const { children, defaultStyles } = this.props; + const WrappedComponent = this.component; + + return {this.state.styleData ? children(this.state.styleData) : false}; + } +} + +export { DataStyles, getStyle }; diff --git a/src/MUIDataTable.js b/src/MUIDataTable.js index 5329a5096..675a89898 100644 --- a/src/MUIDataTable.js +++ b/src/MUIDataTable.js @@ -9,13 +9,12 @@ import MUIDataTableBody from "./MUIDataTableBody"; import MUIDataTableHead from "./MUIDataTableHead"; import MUIDataTablePagination from "./MUIDataTablePagination"; import debounce from "lodash.debounce"; -import { getStyle, withDataStyles } from "./withDataStyles"; +import { getStyle, DataStyles } from "./DataStyles"; -const styles = theme => ({ - table: { - fontFamily: "Roboto", - }, -}); +const defaultTableStyles = { + main: { + } +}; class MUIDataTable extends React.Component { static propTypes = { @@ -38,8 +37,6 @@ class MUIDataTable extends React.Component { }), /** Pass and use className to style MUIDataTable as desired */ className: PropTypes.string, - /** Extend the style applied to components */ - classes: PropTypes.object, }; state = { @@ -325,7 +322,6 @@ class MUIDataTable extends React.Component { this.tableRef} filterUpdate={this.filterUpdate} @@ -335,45 +331,45 @@ class MUIDataTable extends React.Component { searchTextUpdate={this.searchTextUpdate} toggleViewColumn={this.toggleViewColumn} /> - - (this.tableRef = el)} className={classes.table}> - - - {this.options.pagination ? ( - - ) : ( - false + + + {tableStyles => ( +
(this.tableRef = el)} className={tableStyles.main}> + + + {this.options.pagination ? ( + + ) : ( + false + )} +
)} - + ); } } -export default withDataStyles(styles)(MUIDataTable); +export default MUIDataTable; diff --git a/src/MUIDataTableBody.js b/src/MUIDataTableBody.js index 3438d0ef8..6c9b3b485 100644 --- a/src/MUIDataTableBody.js +++ b/src/MUIDataTableBody.js @@ -4,16 +4,26 @@ import Table from "material-ui/Table"; import Typography from "material-ui/Typography"; import { TableBody, TableRow } from "material-ui/Table"; import MUIDataTableBodyCell from "./MUIDataTableBodyCell"; -import { getStyle, withDataStyles } from "./withDataStyles"; +import { getStyle, DataStyles } from "./DataStyles"; -const bodyStyles = theme => ({ +const defaultBodyStyles = { root: { borderBottom: "solid 1px #bdbdbd", }, emptyTitle: { textAlign: "center", }, -}); +}; + +const defaultBodyRowStyles = { + root: {}, +}; + +const defaultBodyCellStyles = { + root: { + border: "solid 1px #FF0000", + }, +}; class MUIDataTableBody extends React.Component { static propTypes = { @@ -32,52 +42,73 @@ class MUIDataTableBody extends React.Component { }; buildRows() { - const { data, columns, options, page, rowsPerPage } = this.props; + const { data, page, rowsPerPage } = this.props; - const newRows = []; + let rows = []; const fromIndex = page === 0 ? 0 : page * rowsPerPage; const toIndex = Math.min(data.length, (page + 1) * rowsPerPage); for (let rowIndex = fromIndex; rowIndex < data.length && rowIndex < toIndex; rowIndex++) { - newRows.push( - - {data[rowIndex].map( - (column, index) => - columns[index].display ? ( - - {column} - - ) : ( - false - ), - )} - , - ); + rows.push(data[rowIndex]); } - return newRows; + return rows.length ? rows : null; } render() { - const { columns, classes, options } = this.props; + const { columns, options } = this.props; const tableRows = this.buildRows(); return ( - - {tableRows.length ? ( - tableRows - ) : ( - - - - Sorry, no matching records found - - - + + {bodyStyles => ( + + + {rowStyles => ( + + {cellStyles => { + return tableRows ? ( + tableRows.map((row, rowIndex) => ( + + {row.map( + (column, index) => + columns[index].display ? ( + + {column} + + ) : ( + false + ), + )} + + )) + ) : ( + + + + Sorry, no matching records found + + + + ); + }} + + )} + + )} - + ); } } -export default withDataStyles(bodyStyles)(MUIDataTableBody); +export default MUIDataTableBody; diff --git a/src/MUIDataTableBodyCell.js b/src/MUIDataTableBodyCell.js index d0aba9d52..3bf4836a6 100644 --- a/src/MUIDataTableBodyCell.js +++ b/src/MUIDataTableBodyCell.js @@ -2,23 +2,13 @@ import React from "react"; import PropTypes from "prop-types"; import Table from "material-ui/Table"; import { TableCell } from "material-ui/Table"; -import { getStyle, withDataStyles } from "./withDataStyles"; - -const cellStyles = theme => ({ - root: { - borderBottom: "solid 1px #bdbdbd", - }, -}); class MUIDataTableBodyCell extends React.Component { render() { - const { children, classes, ...otherProps } = this.props; - return ( - - {children} - - ); + const { children, ...otherProps } = this.props; + + return {children}; } } -export default withDataStyles(cellStyles)(MUIDataTableBodyCell); +export default MUIDataTableBodyCell; diff --git a/src/MUIDataTableFilter.js b/src/MUIDataTableFilter.js index 2b86b60ed..fbf4349ed 100644 --- a/src/MUIDataTableFilter.js +++ b/src/MUIDataTableFilter.js @@ -8,94 +8,7 @@ import Input, { InputLabel } from "material-ui/Input"; import { MenuItem } from "material-ui/Menu"; import Select from "material-ui/Select"; import Checkbox from "material-ui/Checkbox"; -import { getStyle, withDataStyles } from "./withDataStyles"; - -const filterStyles = theme => ({ - root: { - padding: "16px 24px 16px 24px", - fontFamily: "Roboto", - }, - header: { - flex: "0 0 auto", - marginBottom: "16px", - width: "100%", - display: "flex", - justifyContent: "space-between", - }, - title: { - display: "inline-block", - marginLeft: "7px", - fontSize: "14px", - fontWeight: 500, - }, - noMargin: { - marginLeft: "0px", - }, - reset: { - alignSelf: "left", - }, - resetLink: { - color: "#027cb5", - display: "inline-block", - marginLeft: "24px", - fontSize: "12px", - cursor: "pointer", - "&:hover": { - color: "#FF0000", - }, - }, - filtersSelected: { - alignSelf: "right", - }, - /* checkbox */ - checkboxList: { - flex: "1 1 100%", - display: "inline-flex", - marginRight: "24px", - }, - checkboxListTitle: { - marginLeft: "7px", - marginBottom: "8px", - fontSize: "14px", - color: "#424242", - textAlign: "left", - fontWeight: 500, - }, - checkboxFormGroup: { - marginTop: "8px", - }, - checkboxFormControl: { - margin: "0px", - }, - checkboxFormControlLabel: { - fontSize: "15px", - marginLeft: "8px", - color: "#4a4a4a", - }, - checkboxIcon: { - //color: "#027cb5", - width: "32px", - height: "32px", - }, - checked: { - color: "#027CB5", - }, - /* selects */ - selectRoot: { - display: "flex", - marginTop: "16px", - flexDirection: "row", - flexWrap: "wrap", - width: "100%", - height: "80%", - justifyContent: "space-between", - }, - selectFormControl: { - flex: "1 1 calc(50% - 24px)", - marginRight: "24px", - marginBottom: "24px", - }, -}); +import { getStyle, DataStyles } from "./DataStyles"; class MUIDataTableFilter extends React.Component { static propTypes = { @@ -108,7 +21,7 @@ class MUIDataTableFilter extends React.Component { /** Callback to trigger filter reset */ onFilterRest: PropTypes.func, /** Extend the style applied to components */ - classes: PropTypes.object, + filterStyles: PropTypes.object, }; handleCheckboxChange = (index, column) => { @@ -121,47 +34,47 @@ class MUIDataTableFilter extends React.Component { }; render() { - const { classes, columns, options, filterData, filterList, onFilterReset } = this.props; - - const titleClass = classNames({ - [classes.title]: true, - [classes.noMargin]: options.filterType === "dropdown" ? true : false, - }); + const { classes, columns, options, filterStyles, filterData, filterList, onFilterReset } = this.props; return ( -
-
-
- +
+
+
+ FILTERS - + RESET
-
+
{options.filterType === "checkbox" ? ( columns.map((column, index) => ( -
+
- + {column.name} {filterData[index].map((filterColumn, filterIndex) => ( = 0 ? true : false} classes={{ - checked: classes.checked, + checked: filterStyles.checked, }} value={filterColumn} /> @@ -173,10 +86,10 @@ class MUIDataTableFilter extends React.Component {
)) ) : ( -
+
{columns.map((column, index) => { return ( - + {column.name}