- added 'delete' for Table;
This commit is contained in:
@@ -6,8 +6,9 @@ import {
|
|||||||
getCoreRowModel,
|
getCoreRowModel,
|
||||||
flexRender,
|
flexRender,
|
||||||
} from '@tanstack/react-table';
|
} from '@tanstack/react-table';
|
||||||
import { pathOr, isEmpty } from 'ramda';
|
import { pathOr, isEmpty, isNil } from 'ramda';
|
||||||
import { wrap } from 'object-path-immutable';
|
import { wrap } from 'object-path-immutable';
|
||||||
|
import { Button } from 'primereact/button';
|
||||||
|
|
||||||
const Table = ({
|
const Table = ({
|
||||||
fieldName,
|
fieldName,
|
||||||
@@ -20,6 +21,8 @@ const Table = ({
|
|||||||
}) => {
|
}) => {
|
||||||
const [stateFieldData, setStateFieldData] = useState([]);
|
const [stateFieldData, setStateFieldData] = useState([]);
|
||||||
const [rowsData, setRowsData] = useState([]);
|
const [rowsData, setRowsData] = useState([]);
|
||||||
|
const [rowsDataLength, setRowsDataLength] = useState(0);
|
||||||
|
const [rowIndexToDelete, rowRowIndexToDelete] = useState(null);
|
||||||
const [isDisabledNewRow, setIsDisabledNewRow] = useState(false);
|
const [isDisabledNewRow, setIsDisabledNewRow] = useState(false);
|
||||||
const [columns, setColumns] = useState([]);
|
const [columns, setColumns] = useState([]);
|
||||||
const table = useReactTable({
|
const table = useReactTable({
|
||||||
@@ -61,10 +64,14 @@ const Table = ({
|
|||||||
setRowsData([...rowsData, obj]);
|
setRowsData([...rowsData, obj]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const removeRow = (index) => {
|
||||||
|
rowRowIndexToDelete(index)
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let shouldDisableNewRows = false;
|
let shouldDisableNewRows = false;
|
||||||
|
|
||||||
const columns = stateFieldData.map((o) => {
|
let columns = stateFieldData.map((o) => {
|
||||||
const item = {
|
const item = {
|
||||||
accessorKey: o.name,
|
accessorKey: o.name,
|
||||||
header: () => o.label,
|
header: () => o.label,
|
||||||
@@ -82,12 +89,45 @@ const Table = ({
|
|||||||
});
|
});
|
||||||
|
|
||||||
setIsDisabledNewRow(shouldDisableNewRows);
|
setIsDisabledNewRow(shouldDisableNewRows);
|
||||||
|
|
||||||
|
if (!shouldDisableNewRows && !isEmpty(columns)) {
|
||||||
|
columns.push({
|
||||||
|
accessorKey: 'actions',
|
||||||
|
header: () => '',
|
||||||
|
footer: (props) => props.column.id,
|
||||||
|
cell: ({row: { index }}) => <Button
|
||||||
|
type="button"
|
||||||
|
icon="pi pi-times"
|
||||||
|
className="p-button-danger"
|
||||||
|
label=""
|
||||||
|
onClick={() => removeRow(index)}/>
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
setColumns(columns);
|
setColumns(columns);
|
||||||
}, [stateFieldData]);
|
}, [stateFieldData, rowsDataLength]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setRowsDataLength(rowsData.length);
|
||||||
|
}, [rowsData]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isNil(rowIndexToDelete)) {
|
||||||
|
const newRowsData = wrap(rowsData).del([rowIndexToDelete]).value();
|
||||||
|
setRowsData(newRowsData);
|
||||||
|
}
|
||||||
|
rowRowIndexToDelete(null);
|
||||||
|
}, [rowIndexToDelete]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const stateFieldData = pathOr([], ['stateFieldData'], tableColumns);
|
const stateFieldData = pathOr([], ['stateFieldData'], tableColumns);
|
||||||
const rowsData = pathOr([], ['rowsData'], tableColumns);
|
const obj = stateFieldData
|
||||||
|
.reduce((acc, cur) => {
|
||||||
|
acc[cur.name] = ''
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
let rowsData = pathOr([obj], ['rowsData'], tableColumns);
|
||||||
|
rowsData = isEmpty(rowsData) ? [obj] : rowsData;
|
||||||
setStateFieldData(stateFieldData);
|
setStateFieldData(stateFieldData);
|
||||||
setRowsData(rowsData);
|
setRowsData(rowsData);
|
||||||
}, [tableColumns]);
|
}, [tableColumns]);
|
||||||
|
|||||||
Reference in New Issue
Block a user