From f97067e3033aedbcb5757937f9e09a1255eb4ec6 Mon Sep 17 00:00:00 2001 From: Manish Kushare Date: Thu, 23 Feb 2023 12:05:29 +0530 Subject: [PATCH] [ Feature request ] : [ Table ] Added feature to highlight a word in the column if matched with searched term (#5413) * init * added feature to highlight the cell if it's value matches with searched tern * reverting the unwanted changes * removed unwanted changes * added edge case for isEditable on click event * minor bug fixes * made changes to fix the edge case * bug fixed: action button was not displaying in the column --- .../Table/GenerateEachCellValue.jsx | 66 +++++++++++++++++++ .../src/Editor/Components/Table/Table.jsx | 12 +++- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 frontend/src/Editor/Components/Table/GenerateEachCellValue.jsx diff --git a/frontend/src/Editor/Components/Table/GenerateEachCellValue.jsx b/frontend/src/Editor/Components/Table/GenerateEachCellValue.jsx new file mode 100644 index 0000000000..071508a6e1 --- /dev/null +++ b/frontend/src/Editor/Components/Table/GenerateEachCellValue.jsx @@ -0,0 +1,66 @@ +import React, { useEffect, useRef } from 'react'; +import _ from 'lodash'; + +export default function GenerateEachCellValue({ + cellValue, + globalFilter, + cellRender, + rowChangeSet, + rowData, + isEditable, + columnType, + isColumnTypeAction, +}) { + const updateCellValue = useRef(); + const [showHighlightedCells, setHighlighterCells] = React.useState(globalFilter ? true : false); + const columnTypeAllowToRenderMarkElement = ['text', 'string', 'default', 'number', undefined]; + + useEffect(() => { + if (_.isEmpty(rowChangeSet)) { + setHighlighterCells(true); + } + }, [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) => `${match}`); + } + } + return ( +
{ + e.persist(); + if (isEditable && columnTypeAllowToRenderMarkElement.includes(columnType)) { + setHighlighterCells(false); + } + }} + onMouseLeave={(e) => { + e.persist(); + updateCellValue.current = e.target.value; + if (!showHighlightedCells && updateCellValue.current === cellValue && _.isEmpty(rowChangeSet)) { + updateCellValue.current = null; + setHighlighterCells(true); + } + }} + > + {!isColumnTypeAction && columnTypeAllowToRenderMarkElement.includes(columnType) && showHighlightedCells ? ( + + ) : ( + cellRender + )} +
+ ); +} diff --git a/frontend/src/Editor/Components/Table/Table.jsx b/frontend/src/Editor/Components/Table/Table.jsx index f2366e664d..a5fee97007 100644 --- a/frontend/src/Editor/Components/Table/Table.jsx +++ b/frontend/src/Editor/Components/Table/Table.jsx @@ -40,6 +40,8 @@ import * as XLSX from 'xlsx/xlsx.mjs'; import OverlayTrigger from 'react-bootstrap/OverlayTrigger'; import Popover from 'react-bootstrap/Popover'; import { useMounted } from '@/_hooks/use-mount'; +import GenerateEachCellValue from './GenerateEachCellValue'; +// eslint-disable-next-line import/no-unresolved import { toast } from 'react-hot-toast'; export function Table({ @@ -901,7 +903,15 @@ export function Table({
- {cell.render('Cell')} +
);