I am using the react-table
library to create a table with filterable columns. The table is of the following format:
const defaultFilter = [{
id: 'title',
value: '',
}];
const ExampleTable = ({ documents, filter = defaultFilter }) => {
const columns = [
{
accessor: 'title',
filterable: true,
id: 'title',
width: 375,
Cell: props => {
return (
<div>
{ props.value }
</div>
);
},
Filter: ({ filter, onChange }) => (
<input
onChange={ event => onChange(event.target.value) }
style={ { width: '100%' } }
placeholder={ 'Enter Filter' }
value={ filter ? filter.value : '' }
/>
),
filterMethod: (filter, row, column) => (row.title.toLowerCase().includes(filter.value.toLowerCase())),
Header: () => (
<span>
Column Header
</span>
),
}
];
return (
<ReactTable
columns={ columns }
data={ documents } // an array of objects
manual={ true }
filtered={ filter }
onFilteredChange={
(filteredColumns, value) => {
Store.updateFilter(filteredColumns);
}
}
/>
);
};
Whenever I type into the input element created by the Filter
attribute of my single column, a callback is made to my flux store that updates the filter, of which gets passed back into my stateless table component. However, upon this specific action, the filter input gets recreated, and the input consequently loses focus as a result.
Thus, I'd have to manually put the focus back onto the input each time. Might there be a better way of maintaining the reference to that input, so that React doesn't recreate the input every time the props change?