MaterialTable re-render causes component overrides to render with default values
Describe the bug
When using a component override on one of the Rows (in my case MTableFilterRow), if the new component contains any kind of form field (Input, select, radio, etc) and the MaterialTable re-renders, it resets the child component back to it's initial state (IE: Filter text field with a value to be used for column filtering is reset to '')
To Reproduce Steps to reproduce the behavior:
- Create a Material-Table with a list of filterable data
- Use the components property to override a row as documented in Component Overriding
- Type a value or change the value of your new row
- See error
Expected behavior If a user inputs a value, the value should remain and the child component being used should not be reset to its default state.
The example below is the expected behavior, I'm using a standard Material-UI table with the same custom filter component.

Screenshots
The issue as seen in Material-Table

Desktop (please complete the following information):
- OS: MacOS 10.14.5
- Browse: Chrome Version 75.0.3770.142 (Official Build) (64-bit)
Additional context I'm using Material-Table 1.36.2 as I'm unable to upgrade at this time, I am not using Hooks for this use case.
My custom FilterInput component is found here:
import React from "react";
import Input from "@material-ui/core/Input";
import InputAdornment from "@material-ui/core/InputAdornment";
import FormControl from "@material-ui/core/FormControl";
import Menu from "@material-ui/core/Menu";
import MenuItem from "@material-ui/core/MenuItem";
import IconButton from "@material-ui/core/IconButton";
import FilterListIcon from "@material-ui/icons/FilterList";
class FilterInput extends React.Component {
constructor(props) {
super(props);
this.state = {
name: this.props.name,
value: "",
placeholder: "LIKE",
filterOptions: this.props.filterOptions,
filterOpen: false,
anchorEl: null
};
this.timeout = 0;
}
handleFilterClick = event => {
this.setState({ anchorEl: event.currentTarget });
};
handleClose = option => {
if (typeof option === "string") {
this.setState({ anchorEl: null, placeholder: option }, () => {
if (option === "LIKE" || option === "NOT LIKE") {
this.props.handleFilters({
paramName: this.props.field,
paramValue: `${this.state.value}*`,
inputLabel: this.props.name,
searchOperator: this.state.placeholder,
inputType: this.props.inputType,
inputOrder: null
});
} else {
this.props.handleFilters({
paramName: this.props.field,
paramValue: this.state.value,
inputLabel: this.props.name,
searchOperator: this.state.placeholder,
inputType: this.props.inputType,
inputOrder: null
});
}
});
} else {
this.setState({ anchorEl: null });
}
};
handleChange = event => {
this.setState({ value: event.target.value });
if (this.timeout) clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.props.handleFilters({
paramName: this.props.field,
paramValue: `${this.state.value}*`,
inputLabel: this.props.name,
searchOperator: this.state.placeholder,
inputType: this.props.inputType,
inputOrder: null
});
}, 500);
};
render() {
return (
<div>
<FormControl>
<Input
id={this.state.name}
type="text"
value={this.state.value}
onChange={this.handleChange}
placeholder={`${this.state.placeholder} ${this.state.name}`}
endAdornment={
<InputAdornment position="end">
<IconButton
aria-label="Select Filter"
onClick={this.handleFilterClick}
>
<FilterListIcon onClick={this.handleFilterOpen} />
</IconButton>
</InputAdornment>
}
/>
</FormControl>
<Menu
id={`${this.state.name}-menu`}
anchorEl={this.state.anchorEl}
open={Boolean(this.state.anchorEl)}
onClose={this.handleClose}
>
{this.state.filterOptions.map(option => (
<MenuItem key={option} onClick={() => this.handleClose(option)}>
{option}
</MenuItem>
))}
</Menu>
</div>
);
}
}
export default FilterInput;
It takes props from the parent material table like below:
<FilterInput
name={column.title}
field={column.field}
type={column.inputType}
handleFilters={this.handleFilters.bind(this)}
filterOptions={[
"=",
"!=",
"<",
">",
">=",
"<=",
"LIKE",
"NOT LIKE"
]}
/>
My component override is setup as shown:
components={{
FilterRow: props => (
<tr>
<td style={{ width: "48px" }} />
{props.columns.map(column => (
<td>
<FilterInput
// style={{ width: "400px" }}
name={column.title}
field={column.field}
type={column.inputType}
handleFilters={this.handleFilters.bind(this)}
filterOptions={[
"=",
"!=",
"<",
">",
">=",
"<=",
"LIKE",
"NOT LIKE"
]}
/>
</td>
))}
</tr>
)
}}
The handle filters function that is being passed as a prop is below:
handleFilters = filter => {
let array = this.state.searchParams;
if (array.length > 0) {
let found = array.some(el => el.paramName === filter.paramName);
if (!found) {
array.push(filter);
} else {
let index = array.findIndex(el => el.paramName === filter.paramName);
array[index] = filter;
}
this.setState({ searchParams: array, needsUpdate: true });
} else {
this.setState({ searchParams: [filter], needsUpdate: true });
}
};
For more information on how this was found see #446
I think it's broken in design (material-table). "Filterrow" should be stateless, otherwise your component will be mounted(with a new state) every time you update the properties of the table.
I had the same issue, if I tried to filter my table data and pass it back down through the m-table-filter-row.js in the material-table node module. From there i was able to determine what function was being called to filter the data without passing a modified state to the component. Ultimately it ended up looking like this.
FilterRow: props => {
return (
<tr>
<td>
<div>
// Patient Name Filter
<TextField
onChange={(event) => {
props.onFilterChanged(0, event.target.value);
}}
/>
</div>
</td>
<td>
<div>
// Alert Text filter
<TextField
onChange={(event) => {
props.onFilterChanged(1, event.target.value);
}}
/>
</div>
</td>
</tr>
)
}
}}
The onFilterChanged function has two arguments, the first one is the index of the column in your column Data, and the second is the string to filter by.
My colData looks like this
const columnData = [
{
title: 'Patient Name',
field: 'name',
type: 'string',
},
{
title: 'Alert',
field: 'alert',
type: 'string'
}
]
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.