import React, { useState, useEffect } from 'react';
import { validateWidget, determineJustifyContentValue } from '@/_helpers/utils';
import OverlayTrigger from 'react-bootstrap/OverlayTrigger';
const StringColumn = ({
isEditable,
darkMode,
handleCellValueChange,
cellTextColor,
cellValue,
column,
currentState,
containerWidth,
cell,
horizontalAlignment,
isMaxRowHeightAuto,
cellSize,
maxRowHeightValue,
}) => {
const validationData = validateWidget({
validationObject: {
regex: {
value: column.regex,
},
minLength: {
value: column.minLength,
},
maxLength: {
value: column.maxLength,
},
customRule: {
value: column.customRule,
},
},
widgetValue: cellValue,
currentState,
customResolveObjects: { cellValue },
});
const { isValid, validationError } = validationData;
const cellStyles = {
color: cellTextColor ?? 'inherit',
};
const ref = React.useRef(null);
const [showOverlay, setShowOverlay] = useState(false);
const [hovered, setHovered] = useState(false);
const [isEditing, setIsEditing] = useState(false);
useEffect(() => {
if (hovered) {
setShowOverlay(true);
} else {
setShowOverlay(false);
}
}, [hovered]);
useEffect(() => {
if (!isEditable && isEditing) {
setIsEditing(false);
}
}, [isEditable]);
const _renderString = () => (
{
setIsEditing(false);
if (cellValue !== e.target.textContent) {
handleCellValueChange(cell.row.index, column.key || column.name, e.target.textContent, cell.row.original);
}
}}
onKeyDown={(e) => {
if (e.key === 'Enter') {
ref.current.blur();
if (cellValue !== e.target.textContent) {
handleCellValueChange(cell.row.index, column.key || column.name, e.target.textContent, cell.row.original);
}
}
}}
onFocus={(e) => {
setIsEditing(true);
e.stopPropagation();
}}
>
{String(cellValue)}
);
const getOverlay = () => {
return (
setHovered(true)}
onMouseLeave={() => setHovered(false)}
style={{ color: 'var(--text-primary)' }}
>
{String(cellValue)}
);
};
const _showOverlay =
ref?.current &&
(ref?.current?.clientWidth < ref?.current?.children[0]?.offsetWidth ||
ref?.current?.clientHeight < ref?.current?.children[0]?.offsetHeight);
return (
<>
}
trigger={_showOverlay && ['hover', 'focus']}
rootClose={true}
show={_showOverlay && showOverlay && !isEditing}
>
{!isEditable ? (
{
if (!hovered) setHovered(true);
}}
onMouseLeave={() => {
setHovered(false);
}}
ref={ref}
>
{String(cellValue)}
) : (
{
if (!hovered) setHovered(true);
}}
onMouseLeave={() => setHovered(false)}
className={`${!isValid ? 'is-invalid h-100' : ''} ${isEditing ? 'h-100 content-editing' : ''} h-100`}
>
{_renderString()}
{validationError}
)}
>
);
};
export default StringColumn;