https://github.com/tannerlinsley/react-table/issues/3111
https://codesandbox.io/s/modest-booth-hei5d?fontsize=14&hidenavigation=1&theme=dark
using react hook components when i'm using an input cell during the onchange event, the state change causes the component to refresh and the input field to lose focus. i looked at the github link posted above, but not results from the recommended fix on react-table v6. im on react-table 7.6.3
const handleInitialDemandChange = (cell, e) => {
const name = cell.row.original.id
e.persist();
setFieldValues({
...fieldValues,
[name]: e.target.value
});
}
function renderCells(cell) {
console.log(cell.row)
return (
<div key={cell.row.original.id}>
{cell.row.original.id == 0 ?
<input style={{color: '#627FE3'}} value={cell.value} disabled /> :
<input style={{color: '#627FE3'}}
value={fieldValues[cell.row.original.id]}
onChange={(e) => handleInitialDemandChange(cell, e)}
/>}
</div>
)
}
my component code
<MyReactTable id="myReactTable" columns={[
{
Header: '',
id: 'id',
accessor: () => {},
Cell: ({ cell }) =>
<div style={{color: '#627FE3', display: 'flex', alignItems:'center'}}>
{cell.row.canExpand ? (
<>
{
cell.row.isExpanded ?
<img
alt="expand"
src="/images/icons/expand.svg"
{...cell.row.getToggleRowExpandedProps({
style: {
paddingLeft: `${cell.row.depth * 2}rem`
}
})}
height="30"
width="30"
/> : <img
alt="collapse"
src="/images/icons/collapse.svg"
{...cell.row.getToggleRowExpandedProps({
style: {
paddingLeft: `${cell.row.depth * 2}rem`
}
})}
height="30"
width="30"
/>
}
</>
) : null}
</div>
},
{
Header: 'Item',
accessor: 'item'
},
{
Header: 'Amount Paid',
accessor: 'amount_paid',
Cell: ({ cell }) => (
<p style={{color: '#627FE3', cursor:'pointer', margin:0}} >
{cell.row.values.amount_paid}
</p>
)
},
{
Header: 'Initial Demand',
accessor: 'initial_demand',
Cell: ({cell}) => (renderCells(cell))
},
]} data={round1Summary || []}/>
MyReactTable.js
import React from 'react'
import CssBaseline from '@material-ui/core/CssBaseline'
import MaUTable from '@material-ui/core/Table'
import TableBody from '@material-ui/core/TableBody'
import TableCell from '@material-ui/core/TableCell'
import TableHead from '@material-ui/core/TableHead'
import TableRow from '@material-ui/core/TableRow'
import { useTable, useSortBy, useExpanded } from 'react-table'
import './index.css'
const MyReactTable = props => {
const { columns, data, key, ...rest } = props;
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow,
} = useTable(
{
columns,
data,
},
useSortBy,
useExpanded
)
return (
<div>
<CssBaseline />
<MaUTable key={key} {...getTableProps()}>
<TableHead>
{headerGroups.map(headerGroup => (
<TableRow {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => (
<TableCell {...column.getHeaderProps(column.getSortByToggleProps())}>
{column.render('Header')}
<span>
{column.isSorted
? column.isSortedDesc
? ' 🔽'
: ' 🔼'
: ''}
</span>
</TableCell>
))}
</TableRow>
))}
</TableHead>
<TableBody>
{rows.map((row, idx) => {
prepareRow(row)
return (
<TableRow {...row.getRowProps()}>
{row.cells.map((cell, idx2) => {
return (
<TableCell {...cell.getCellProps()}>
{cell.render('Cell')}
</TableCell>
)
})}
</TableRow>
)
})}
</TableBody>
</MaUTable>
</div>
)
}
export default MyReactTable