2023-02-23 06:35:29 +00:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
|
|
|
|
import _ from 'lodash';
|
2023-03-02 06:20:27 +00:00
|
|
|
import { validateWidget } from '@/_helpers/utils';
|
2023-07-21 05:51:34 +00:00
|
|
|
import { useMounted } from '@/_hooks/use-mount';
|
2023-02-23 06:35:29 +00:00
|
|
|
|
|
|
|
|
export default function GenerateEachCellValue({
|
|
|
|
|
cellValue,
|
|
|
|
|
globalFilter,
|
|
|
|
|
cellRender,
|
|
|
|
|
rowChangeSet,
|
|
|
|
|
rowData,
|
|
|
|
|
isEditable,
|
|
|
|
|
columnType,
|
|
|
|
|
isColumnTypeAction,
|
2023-03-02 06:20:27 +00:00
|
|
|
cellTextColor,
|
|
|
|
|
cell,
|
|
|
|
|
currentState,
|
2023-02-23 06:35:29 +00:00
|
|
|
}) {
|
2023-07-21 05:51:34 +00:00
|
|
|
const mounted = useMounted();
|
2023-02-23 06:35:29 +00:00
|
|
|
const updateCellValue = useRef();
|
2023-07-21 05:51:34 +00:00
|
|
|
const isTabKeyPressed = useRef(false);
|
2023-02-23 06:35:29 +00:00
|
|
|
const [showHighlightedCells, setHighlighterCells] = React.useState(globalFilter ? true : false);
|
|
|
|
|
const columnTypeAllowToRenderMarkElement = ['text', 'string', 'default', 'number', undefined];
|
2023-03-02 06:20:27 +00:00
|
|
|
let validationData = {};
|
|
|
|
|
if (cell.column.isEditable && showHighlightedCells) {
|
|
|
|
|
if (cell.column.columnType === 'number') {
|
|
|
|
|
validationData = {
|
|
|
|
|
...validateWidget({
|
|
|
|
|
validationObject: {
|
|
|
|
|
minValue: {
|
|
|
|
|
value: cell.column.minValue,
|
|
|
|
|
},
|
|
|
|
|
maxValue: {
|
|
|
|
|
value: cell.column.maxValue,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
widgetValue: cellValue,
|
|
|
|
|
currentState,
|
|
|
|
|
customResolveObjects: { cellValue },
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (['string', undefined, 'default', 'text'].includes(cell.column.columnType)) {
|
|
|
|
|
validationData = {
|
|
|
|
|
...validateWidget({
|
|
|
|
|
validationObject: {
|
|
|
|
|
regex: {
|
|
|
|
|
value: cell.column.regex,
|
|
|
|
|
},
|
|
|
|
|
minLength: {
|
|
|
|
|
value: cell.column.minLength,
|
|
|
|
|
},
|
|
|
|
|
maxLength: {
|
|
|
|
|
value: cell.column.maxLength,
|
|
|
|
|
},
|
|
|
|
|
customRule: {
|
|
|
|
|
value: cell.column.customRule,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
widgetValue: cellValue,
|
|
|
|
|
currentState,
|
|
|
|
|
customResolveObjects: { cellValue },
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-02-23 06:35:29 +00:00
|
|
|
useEffect(() => {
|
2023-07-21 05:51:34 +00:00
|
|
|
if (mounted && _.isEmpty(rowChangeSet)) {
|
2023-02-23 06:35:29 +00:00
|
|
|
setHighlighterCells(true);
|
|
|
|
|
}
|
2023-07-21 05:51:34 +00:00
|
|
|
//In the dependency array to ingnore linting warning, added mounted but it's not working out, any way to avoid ingnoring dependency array
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
2023-02-23 06:35:29 +00:00
|
|
|
}, [rowData, rowChangeSet]);
|
|
|
|
|
|
|
|
|
|
let htmlElement = cellValue;
|
|
|
|
|
if (cellValue?.toString()?.toLowerCase().includes(globalFilter?.toLowerCase())) {
|
|
|
|
|
if (globalFilter) {
|
|
|
|
|
var normReq = globalFilter
|
|
|
|
|
.toLowerCase()
|
|
|
|
|
.replace(/\s+/g, ' ')
|
|
|
|
|
.trim()
|
|
|
|
|
.split(' ')
|
|
|
|
|
.sort((a, b) => b.length - a.length);
|
|
|
|
|
htmlElement = cellValue
|
|
|
|
|
.toString()
|
|
|
|
|
.replace(new RegExp(`(${normReq.join('|')})`, 'gi'), (match) => `<mark>${match}</mark>`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
<div
|
2023-07-24 11:08:22 +00:00
|
|
|
onClick={() => {
|
2023-02-23 06:35:29 +00:00
|
|
|
if (isEditable && columnTypeAllowToRenderMarkElement.includes(columnType)) {
|
|
|
|
|
setHighlighterCells(false);
|
|
|
|
|
}
|
|
|
|
|
}}
|
2023-03-02 06:20:27 +00:00
|
|
|
onBlur={(e) => {
|
2023-07-21 05:51:34 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
|
if (isTabKeyPressed.current) {
|
|
|
|
|
isTabKeyPressed.current = false;
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
updateCellValue.current = e.target.value;
|
|
|
|
|
//removing _.isEmpty(rowChangeSet) flag from if statement at the end
|
|
|
|
|
if (!showHighlightedCells && updateCellValue.current === cellValue) {
|
|
|
|
|
updateCellValue.current = null;
|
|
|
|
|
setHighlighterCells(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
onKeyUp={(e) => {
|
|
|
|
|
if (e.key === 'Tab') {
|
|
|
|
|
isTabKeyPressed.current = true;
|
|
|
|
|
setHighlighterCells(false);
|
2023-02-23 06:35:29 +00:00
|
|
|
}
|
|
|
|
|
}}
|
2023-09-04 04:31:40 +00:00
|
|
|
className={`w-100 h-100 ${columnType === 'selector' && 'd-flex align-items-center justify-content-center'}`}
|
2023-02-23 06:35:29 +00:00
|
|
|
>
|
|
|
|
|
{!isColumnTypeAction && columnTypeAllowToRenderMarkElement.includes(columnType) && showHighlightedCells ? (
|
2023-07-21 05:51:34 +00:00
|
|
|
<div className="d-flex justify-content-center flex-column w-100 h-100 generate-cell-value-component-div-wrapper">
|
2023-03-02 06:20:27 +00:00
|
|
|
<div
|
2023-07-21 05:51:34 +00:00
|
|
|
style={{
|
|
|
|
|
color: cellTextColor,
|
|
|
|
|
}}
|
2023-03-02 06:20:27 +00:00
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
|
__html: htmlElement,
|
|
|
|
|
}}
|
2023-07-21 05:51:34 +00:00
|
|
|
tabIndex={0}
|
|
|
|
|
className={`form-control-plaintext form-control-plaintext-sm ${columnType === 'text' && 'h-100 my-1'}`}
|
2023-03-02 06:20:27 +00:00
|
|
|
></div>
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
display: cell.column.isEditable && validationData.validationError ? 'block' : 'none',
|
|
|
|
|
width: '100%',
|
|
|
|
|
marginTop: ' 0.25rem',
|
|
|
|
|
fontSize: ' 85.7142857%',
|
|
|
|
|
color: '#d63939',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{validationData.validationError}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2023-02-23 06:35:29 +00:00
|
|
|
) : (
|
|
|
|
|
cellRender
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|