Skip to content

Commit

Permalink
fix misaligned columns when customized filter option turned on
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnb committed Mar 18, 2018
1 parent b9a0ebf commit fb54a64
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 52 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ The component accepts the following props:
|**`rowsPerPage`**|number|10|Number of rows allowed per page
|**`rowsPerPageOptions`**|array|[10,15,20]|Options to provide in pagination for number of rows a user can select
|**`rowHover`**|boolean|true|Enable/disable hover style over rows
|**`sortFilterList`**|boolean|true|Enable/disable filter list sort
|**`sort`**|boolean|true|Enable/disable sort on all columns
|**`filter`**|boolean|true|Show/hide filter icon from toolbar
|**`search`**|boolean|true|Show/hide search icon from toolbar
Expand Down
7 changes: 6 additions & 1 deletion src/MUIDataTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class MUIDataTable extends React.Component {
rowsPerPage: 10,
rowsPerPageOptions: [10, 15, 100],
filter: true,
sortFilterList: true,
sort: true,
search: true,
print: true,
Expand Down Expand Up @@ -146,7 +147,7 @@ class MUIDataTable extends React.Component {
*/

setTableData(props) {
const { options, data, columns } = props;
const { data, columns } = props;

let columnData = [],
filterData = [],
Expand Down Expand Up @@ -180,6 +181,10 @@ class MUIDataTable extends React.Component {

if (filterData[colIndex].indexOf(value) < 0) filterData[colIndex].push(value);
}

if (this.options.sortFilterList) {
filterData[colIndex].sort();
}
});

/* set source data and display Data set source set */
Expand Down
105 changes: 57 additions & 48 deletions src/MUIDataTableFilter.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,69 +38,78 @@ class MUIDataTableFilter extends React.Component {
renderCheckbox(columns) {
const { filterStyles, filterData, filterList } = this.props;

return columns.map((column, index) => (
<div className={filterStyles.checkboxList} key={index}>
<FormGroup>
<Typography type="caption" className={filterStyles.checkboxListTitle}>
{column.name}
</Typography>
{filterData[index].map((filterColumn, filterIndex) => (
<FormControlLabel
key={filterIndex}
classes={{
root: filterStyles.checkboxFormControl,
label: filterStyles.checkboxFormControlLabel,
}}
control={
<Checkbox
className={filterStyles.checkboxIcon}
onChange={this.handleCheckboxChange.bind(null, index, filterColumn)}
checked={filterList[index].indexOf(filterColumn) >= 0 ? true : false}
return columns.map(
(column, index) =>
column.filter ? (
<div className={filterStyles.checkboxList} key={index}>
<FormGroup>
<Typography type="caption" className={filterStyles.checkboxListTitle}>
{column.name}
</Typography>
{filterData[index].map((filterColumn, filterIndex) => (
<FormControlLabel
key={filterIndex}
classes={{
checked: filterStyles.checked,
root: filterStyles.checkboxFormControl,
label: filterStyles.checkboxFormControlLabel,
}}
value={filterColumn}
control={
<Checkbox
className={filterStyles.checkboxIcon}
onChange={this.handleCheckboxChange.bind(null, index, filterColumn)}
checked={filterList[index].indexOf(filterColumn) >= 0 ? true : false}
classes={{
checked: filterStyles.checked,
}}
value={filterColumn}
/>
}
label={filterColumn}
/>
}
label={filterColumn}
/>
))}
</FormGroup>
</div>
));
))}
</FormGroup>
</div>
) : (
false
),
);
}

renderSelect(columns) {
const { filterStyles, filterData, filterList } = this.props;

return (
<div className={filterStyles.selectRoot}>
{columns.map((column, index) => (
<FormControl className={filterStyles.selectFormControl} key={index}>
<InputLabel htmlFor={column.name}>{column.name}</InputLabel>
<Select
value={filterList[index] && filterList[index][0] ? filterList[index][0] : "All"}
name={column.name}
onChange={event => this.handleDropdownChange(event, index)}
input={<Input name={column.name} id={column.name} />}>
<MenuItem value="All" key={0}>
All
</MenuItem>
{filterData[index].map((filterColumn, filterIndex) => (
<MenuItem value={filterColumn} key={filterIndex + 1}>
{filterColumn}
</MenuItem>
))}
</Select>
</FormControl>
))}
{columns.map(
(column, index) =>
column.filter ? (
<FormControl className={filterStyles.selectFormControl} key={index}>
<InputLabel htmlFor={column.name}>{column.name}</InputLabel>
<Select
value={filterList[index] && filterList[index][0] ? filterList[index][0] : "All"}
name={column.name}
onChange={event => this.handleDropdownChange(event, index)}
input={<Input name={column.name} id={column.name} />}>
<MenuItem value="All" key={0}>
All
</MenuItem>
{filterData[index].map((filterColumn, filterIndex) => (
<MenuItem value={filterColumn} key={filterIndex + 1}>
{filterColumn}
</MenuItem>
))}
</Select>
</FormControl>
) : (
false
),
)}
</div>
);
}

render() {
const { columns, options, filterStyles, onFilterReset } = this.props;
const dislayColumns = columns.filter(item => item.filter);

return (
<div className={filterStyles.root}>
Expand All @@ -120,7 +129,7 @@ class MUIDataTableFilter extends React.Component {
</div>
<div className={filterStyles.filtersSelected} />
</div>
{options.filterType === "checkbox" ? this.renderCheckbox(dislayColumns) : this.renderSelect(dislayColumns)}
{options.filterType === "checkbox" ? this.renderCheckbox(columns) : this.renderSelect(columns)}
</div>
);
}
Expand Down
6 changes: 3 additions & 3 deletions test/MUIDataTable.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ describe("<MUIDataTable />", function() {
const shallowWrapper = shallow(<MUIDataTable columns={columns} data={data} />);
const state = shallowWrapper.state();
const expectedResult = [
["Joe James", "John Walsh", "Bob Herm", "James Houston"],
["Bob Herm", "James Houston", "Joe James", "John Walsh"],
["Test Corp"],
["Yonkers", "Hartford", "Tampa", "Dallas"],
["NY", "CT", "FL", "TX"],
["Dallas", "Hartford", "Tampa", "Yonkers"],
["CT", "FL", "NY", "TX"],
];

assert.deepEqual(state.filterData, expectedResult);
Expand Down

0 comments on commit fb54a64

Please sign in to comment.