- saving progress;

This commit is contained in:
Vitalii Kiiko
2025-01-23 12:54:27 +01:00
parent d9bd7f3578
commit e99a9b2058
7 changed files with 62 additions and 55 deletions

View File

@@ -88,7 +88,6 @@
color: var(--global-textColor); color: var(--global-textColor);
font-size: 15px; font-size: 15px;
text-align: left; text-align: left;
text-align: start;
} }
th { th {
@@ -97,6 +96,7 @@
} }
td { td {
min-width: 120px;
input { input {
width: 100%; width: 100%;
padding: 3px 5px; padding: 3px 5px;

View File

@@ -1,18 +1,11 @@
import { head, isEmpty, isNil, sum } from 'ramda'; import { head, isNil, pathOr } from 'ramda';
const LastRowCell = ({columnId, lastRows, columnMeta = {}, getColumnDataFn}) => { const LastRowCell = ({columnId, lastRowCfg, columnMeta = {}, tableValue = []}) => {
const cellData = head(lastRows.filter(o => !isNil(o[columnId]))); const cellData = head(lastRowCfg.filter(o => !isNil(o[columnId])));
let cellValue = cellData[columnId]; let cellValue = cellData[columnId];
if (columnMeta.enableFormula) { if (columnMeta.enableFormula) {
const getAllRowsValues = getColumnDataFn(columnId) cellValue = pathOr(0, ['total'], tableValue);
.map(v => isEmpty(v) || isNil(v) ? 0 : v);
if (cellValue === 'sum') {
cellValue = sum(getAllRowsValues);
} else {
cellValue = 0;
}
} }
return <td>{cellValue}</td>; return <td>{cellValue}</td>;

View File

@@ -1,16 +1,17 @@
import React from 'react'; import React from 'react';
import { flexRender, getCoreRowModel, useReactTable } from '@tanstack/react-table'; import { flexRender, getCoreRowModel, useReactTable } from '@tanstack/react-table';
import { wrap } from 'object-path-immutable'; import { wrap } from 'object-path-immutable';
import { isEmpty } from 'ramda'; import { head, isEmpty, isNil, pathOr, sum } from 'ramda';
// components // components
import DefaultCell from './components/DefaultCell'; import DefaultCell from './components/DefaultCell';
import LastRowCell from './components/LastRowCell'; import LastRowCell from './components/LastRowCell';
const RenderTable = ({ data, columns, lastRow, setRowsFn, disabled }) => { const RenderTable = ({ tableValue = {}, columnsCfg, lastRowCfg, setTableValueFn, disabled }) => {
const rows = pathOr([], ['rows'], tableValue)
const table = useReactTable({ const table = useReactTable({
data, data: rows,
columns, columns: columnsCfg,
defaultColumn: { defaultColumn: {
cell: DefaultCell cell: DefaultCell
}, },
@@ -18,17 +19,30 @@ const RenderTable = ({ data, columns, lastRow, setRowsFn, disabled }) => {
meta: { meta: {
disabled, disabled,
updateData: (rowIndex, columnId, value) => { updateData: (rowIndex, columnId, value) => {
const newRowsData = wrap(data).set([rowIndex, columnId], value).value(); const columnCfgData = head(columnsCfg.filter(o => o.accessorKey === columnId));
setRowsFn(newRowsData); const cellData = head(lastRowCfg.filter(o => !isNil(o[columnId])));
const cellValue = cellData[columnId];
let newRowsData = wrap(tableValue).set(['rows', rowIndex, columnId], value).value();
let total = pathOr(0, ['total'], tableValue);
if (columnCfgData.meta.enableFormula) {
const getAllRowsValues = newRowsData.rows
.map(row => row[columnId])
.map(v => isEmpty(v) || isNil(v) ? 0 : v);
if (cellValue === 'sum') {
total = sum(getAllRowsValues);
} else {
total = 0;
}
}
newRowsData = wrap(newRowsData).set(['total'], total).value();
setTableValueFn(newRowsData);
}, },
} }
}); });
const getColumnData = (columnId) => {
const rows = table.getRowModel().rows;
return rows.map(row => row.getValue(columnId));
};
return ( return (
<table> <table>
<thead> <thead>
@@ -68,14 +82,14 @@ const RenderTable = ({ data, columns, lastRow, setRowsFn, disabled }) => {
</tr> </tr>
); );
})} })}
{!isEmpty(lastRow) {!isEmpty(lastRowCfg)
? <tr> ? <tr>
{columns.map((o) => <LastRowCell {columnsCfg.map((o) => <LastRowCell
key={o.accessorKey} key={o.accessorKey}
columnId={o.accessorKey} columnId={o.accessorKey}
columnMeta={o.meta} columnMeta={o.meta}
lastRows={lastRow} lastRowCfg={lastRowCfg}
getColumnDataFn={getColumnData}/>)} tableValue={tableValue}/>)}
</tr> </tr>
: null} : null}
</tbody> </tbody>

View File

@@ -27,12 +27,12 @@ const Table = ({
const [rowsCfg, setRowsCfg] = useState([]); const [rowsCfg, setRowsCfg] = useState([]);
const [columns, setColumns] = useState([]); const [columns, setColumns] = useState([]);
const [lastRow, setLastRow] = useState([]); const [lastRow, setLastRow] = useState([]);
const [rows, setRows] = useState(null); const [tableValue, setTableValue] = useState(null);
const updateRows = useCallback((data) => { const updateValue = useCallback((data) => {
setRows(data); setTableValue(data);
setDataFn(fieldName, data, { shouldValidate: true }); setDataFn(fieldName, data, { shouldValidate: true });
}, [rows, defaultValue]); }, [tableValue, defaultValue]);
const properConfig = (config) => { const properConfig = (config) => {
let newConfig = klona(config); let newConfig = klona(config);
@@ -71,7 +71,7 @@ const Table = ({
}, [columnsCfg, disabled]); }, [columnsCfg, disabled]);
useEffect(() => { useEffect(() => {
setRows(rowsCfg); setTableValue(rowsCfg);
}, [rowsCfg]); }, [rowsCfg]);
useEffect(() => { useEffect(() => {
@@ -84,7 +84,7 @@ const Table = ({
let rowsData = pathOr([obj], ['rowsData'], tableColumns); let rowsData = pathOr([obj], ['rowsData'], tableColumns);
rowsData = isEmpty(rowsData) ? [obj] : rowsData; rowsData = isEmpty(rowsData) ? [obj] : rowsData;
setColumnsCfg(stateFieldData); setColumnsCfg(stateFieldData);
setRowsCfg(rowsData); setRowsCfg({rows: rowsData, total: 0});
let lastRowData = stateFieldData.reduce((acc, cur) => { let lastRowData = stateFieldData.reduce((acc, cur) => {
const value = cur.enableFormula ? cur.lastRowFormula : (cur.lastRowText ? cur.lastRowText : ''); const value = cur.enableFormula ? cur.lastRowFormula : (cur.lastRowText ? cur.lastRowText : '');
@@ -103,29 +103,29 @@ const Table = ({
}, [tableColumns]); }, [tableColumns]);
useEffect(() => { useEffect(() => {
if (!equal(rows, defaultValue)) { if (!equal(tableValue, defaultValue)) {
setRows(defaultValue); setTableValue(defaultValue);
} }
}, [defaultValue]); }, [defaultValue]);
useEffect(() => { useEffect(() => {
setRows(defaultValue); setTableValue(defaultValue);
register(fieldName, properConfig(config)); register(fieldName, properConfig(config));
}, []); }, []);
console.log('rows', rows, lastRow, tableColumns)
return ( return (
<> <>
<label htmlFor={fieldName} className={classNames({ 'p-error': errors[fieldName] })}> <label htmlFor={fieldName} className={classNames({ 'p-error': errors[fieldName] })}>
{label}{config.required || config.isRequired || (config.validate && config.validate.nonEmptyTables) {label}{config.required || config.isRequired || (config.validate && config.validate.nonEmptyTables)
? <span className="appForm__field--required">*</span> : null} ? <span className="appForm__field--required">*</span> : null}
</label> </label>
{rows ? <RenderTable {tableValue
columns={columns} ? <RenderTable
data={rows} columnsCfg={columns}
lastRow={lastRow} tableValue={tableValue}
setRowsFn={updateRows} lastRowCfg={lastRow}
disabled={disabled}/> : null} setTableValueFn={updateValue}
disabled={disabled}/> : null}
</>) </>)
} }

View File

@@ -1,7 +1,7 @@
import { head, isEmpty, isNil, sum } from 'ramda'; import { head, isEmpty, isNil, sum } from 'ramda';
const LastRowCell = ({columnId, lastRows, columnMeta = {}, getColumnDataFn}) => { const LastRowCell = ({columnId, lastRowCfg, columnMeta = {}, getColumnDataFn}) => {
const cellData = head(lastRows.filter(o => !isNil(o[columnId]))); const cellData = head(lastRowCfg.filter(o => !isNil(o[columnId])));
let cellValue = cellData[columnId]; let cellValue = cellData[columnId];
if (columnMeta.enableFormula) { if (columnMeta.enableFormula) {

View File

@@ -7,10 +7,10 @@ import { isEmpty } from 'ramda';
import DefaultCell from './components/DefaultCell'; import DefaultCell from './components/DefaultCell';
import LastRowCell from './components/LastRowCell'; import LastRowCell from './components/LastRowCell';
const RenderTable = ({ data, columns, lastRow, setRowsFn, disabled }) => { const RenderTable = ({ rowsData, columnsCfg, lastRowCfg, setRowsFn, disabled }) => {
const table = useReactTable({ const table = useReactTable({
data, data: rowsData,
columns, columns: columnsCfg,
defaultColumn: { defaultColumn: {
cell: DefaultCell cell: DefaultCell
}, },
@@ -18,7 +18,7 @@ const RenderTable = ({ data, columns, lastRow, setRowsFn, disabled }) => {
meta: { meta: {
disabled, disabled,
updateData: (rowIndex, columnId, value) => { updateData: (rowIndex, columnId, value) => {
const newRowsData = wrap(data).set([rowIndex, columnId], value).value(); const newRowsData = wrap(rowsData).set([rowIndex, columnId], value).value();
setRowsFn(newRowsData); setRowsFn(newRowsData);
}, },
} }
@@ -68,13 +68,13 @@ const RenderTable = ({ data, columns, lastRow, setRowsFn, disabled }) => {
</tr> </tr>
); );
})} })}
{!isEmpty(lastRow) {!isEmpty(lastRowCfg)
? <tr> ? <tr>
{columns.map((o) => <LastRowCell {columnsCfg.map((o) => <LastRowCell
key={o.accessorKey} key={o.accessorKey}
columnId={o.accessorKey} columnId={o.accessorKey}
columnMeta={o.meta} columnMeta={o.meta}
lastRows={lastRow} lastRowCfg={lastRowCfg}
getColumnDataFn={getColumnData}/>)} getColumnDataFn={getColumnData}/>)}
</tr> </tr>
: null} : null}

View File

@@ -170,9 +170,9 @@ const Table = ({
? <span className="appForm__field--required">*</span> : null} ? <span className="appForm__field--required">*</span> : null}
</label> </label>
{rows ? <RenderTable {rows ? <RenderTable
columns={columns} columnsCfg={columns}
data={rows} rowsData={rows}
lastRow={lastRow} lastRowCfg={lastRow}
setRowsFn={updateRows} setRowsFn={updateRows}
disabled={disabled}/> : null} disabled={disabled}/> : null}
{!isEmpty(columns) && !shouldDisableNewRows {!isEmpty(columns) && !shouldDisableNewRows