react-data-table-component
react-data-table-component copied to clipboard
Expanded Component Input Losing Focus
Hi,
I created a sandbox to demonstrate the problem.
The first component loses focus on every input keystroke in expandableRowsComponent. This is expected because props are changing and it causes the component to re-render.
For the second component, I applied Clemios answer which workaround the problem but it's a bit hacky and not good practice.
My main goal here is, on mobile devices, that instead of showing users endless columns, I would like to show vertical inputs with labels like the example. My question is, is there a proper way to tackle this problem?
Many thanks,
Sener
I added 3rd solution which solves my problem and maybe helps someone.
`
import { useMemo, useState } from "react";
import DataTable from "react-data-table-component";
import "./styles.css";
import mockData from "./mockData.json";
export default function OnBlurFix() {
const [data, setData] = useState(mockData.data);
// Handles input states dynamically
const handleInputChange = (rowData, name, e) => {
// Get Value
const { value } = e.target;
// Clone data
const tmpData = [...data];
// Find row
const tmpRow = tmpData.find((person) => person.id === rowData.id);
// Update value
tmpRow[name] = value;
// Update state
setData(tmpData);
};
// Handles row change from expandableRowsComponent
const handleRowChange = (rowData) => {
// Clone data
const tmpData = [...data];
// Find row
const tmpRow = tmpData.find((person) => person.id === rowData.id);
// Update row
Object.assign(tmpRow, rowData);
// Update state
setData(tmpData);
};
// Colums
const columns = useMemo(() => [
{
name: "Id",
selector: (row) => row.id,
width: "50px"
},
{
name: "Name",
selector: (row) => row.firstName,
cell: (row) => (
<input
value={row.firstName}
onChange={(e) => handleInputChange(row, "firstName", e)}
/>
)
},
{
name: "Surname",
selector: (row) => row.surname,
cell: (row) => (
<input
value={row.surname}
onChange={(e) => handleInputChange(row, "surname", e)}
/>
),
hide: "md"
},
{
name: "Age",
selector: (row) => row.age,
cell: (row) => (
<input
value={row.age}
onChange={(e) => handleInputChange(row, "age", e)}
/>
),
hide: "md"
}
]);
return (
<div className="App">
<DataTable
columns={columns}
data={data}
keyField="id"
expandableRows
expandableRowsComponent={(row) => (
<ExpandedComponent row={row} handleRowChange={handleRowChange} />
)}
/>
<div className="App_State">
<p>App State</p>
{JSON.stringify(data)}
</div>
</div>
);
}
const ExpandedComponent = ({ row, handleRowChange }) => {
// Local Row State
const [rowData, setRowData] = useState(row.data);
// Handles local input state dynamically
const handleLocalInputChange = (rowData, name, e) => {
// Get Value
const { value } = e.target;
// Clone data
const tmpRowData = { ...rowData };
tmpRowData[name] = value;
setRowData(tmpRowData);
};
const handleRowUpdate = (e) => {
// Prevent update on tab or clicking to other row elements.
if (e.relatedTarget?.getAttribute("data-input") !== rowData.id.toString()) {
// Update Row
handleRowChange(rowData);
}
};
return (
<div className="ExpandedComponent">
<div className="ExpandedComponent_Row">
<label>Surname</label>
<input
value={rowData.surname}
onChange={(e) => handleLocalInputChange(rowData, "surname", e)}
onBlur={(e) => handleRowUpdate(e)}
data-input={rowData.id}
/>
</div>
<div className="ExpandedComponent_Row">
<label>Age</label>
<input
value={rowData.age}
onChange={(e) => handleLocalInputChange(rowData, "age", e)}
onBlur={(e) => handleRowUpdate(e)}
data-input={rowData.id}
/>
</div>
</div>
);
};
`