import { faAngleDown, faAngleUp, faSearch, faSpinner, faTimes } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { ICellRendererParams, IFloatingFilterParams } from '@ag-grid-community/core'; import debounce from 'lodash.debounce'; import * as React from 'react'; type CellRendererProps = ICellRendererParams & { filterModel: Map; searchResultsColor: string; }; type SearchFilterRendererProps = IFloatingFilterParams & { onFilterChange: (colName: string, filterValue: string) => void; onclickNext: () => Promise; onclickPrevious: () => Promise; colName: string; filterModel: Map; }; interface SearchFilterRendererState { hasHovered: boolean; hasClicked: boolean; isSearchSuccessful: boolean; isSearching: boolean; } export class LoadingRenderer extends React.Component { render(): JSX.Element { return ( {this.props.value ? this.props.value : } ); } } export class CellRenderer extends React.Component { render(): JSX.Element { if (this.props.value === undefined) { return ( ); } const currField = this.props.colDef?.field; const currFieldVal = this.props.value || ''; const isMatched = this.props.data && this.props.data['isMatched']; let cellElement: JSX.Element; const searchTerm = (currField && this.props.filterModel.get(currField)) || ''; if (this.props.filterModel.size > 0) { if (isMatched) { const regex = new RegExp(searchTerm, 'g'); const matches = currFieldVal.match(regex); cellElement = ( <> {currFieldVal.split(regex).map((tag: string, index: number, array: string[]) => ( {array.length > matches.length + 1 ? ( // regex contains groups, evens are non-matching strings, odds are captured groups or undefined <> {index % 2 === 0 ? ( <>{tag} ) : ( {matches[(index / 2) | 0]} )} ) : ( // regex does not contain groups, all elements are non-matching strings <> {tag} {index < matches.length ? ( {matches[index]} ) : ( <> )} )} ))} ); } else { cellElement = {currFieldVal} ; } } else { cellElement = <>{currFieldVal || ''}; } return {cellElement}; } } export class SearchFilterRenderer extends React.Component { private debouncedChangeHandler: (colName: string, inputVal: string) => void; constructor(props: SearchFilterRendererProps) { super(props); this.state = { hasHovered: false, hasClicked: this.props.filterModel.has(this.props.colName), isSearchSuccessful: true, isSearching: false }; this.debouncedChangeHandler = debounce((colName, inputVal) => { this.onFilterChange(colName, inputVal); }, 10); this.onInputBoxChanged = this.onInputBoxChanged.bind(this); this.onMouseEnterHandler = this.onMouseEnterHandler.bind(this); this.onMouseLeaveHandler = this.onMouseLeaveHandler.bind(this); this.onClickHandler = this.onClickHandler.bind(this); this.onDownClickHandler = this.onDownClickHandler.bind(this); this.onUpClickHandler = this.onUpClickHandler.bind(this); this.onCloseClickHandler = this.onCloseClickHandler.bind(this); this.onKeyDownEvent = this.onKeyDownEvent.bind(this); this.onFilterChange = this.onFilterChange.bind(this); this.runSearch = this.runSearch.bind(this); } render(): JSX.Element { return (
{!this.state.hasClicked && !this.state.hasHovered && ( )} {!this.state.hasClicked && this.state.hasHovered && (
Search
)} {this.state.hasClicked && (
{this.state.isSearching && (
)}
)}
); } private async onFilterChange(colName: string, inputVal: string) { this.setState({ isSearchSuccessful: true, isSearching: false }); this.props.onFilterChange(colName, inputVal); } private runSearch(callback: () => Promise | undefined): void { this.setState({ isSearching: true, isSearchSuccessful: true }, () => { this.forceUpdate(() => { const result = callback(); if (result === undefined) { return; } result.then(isFound => { // need to trigger backend query this.setState({ isSearching: false, isSearchSuccessful: isFound }); }); result.catch(() => { this.setState({ isSearching: false, isSearchSuccessful: false }); }); }); }); } private onKeyDownEvent(event: React.KeyboardEvent) { if (event.key === 'Enter') { if (event.shiftKey) { this.runSearch(this.props.onclickPrevious); } else { this.runSearch(this.props.onclickNext); } } else if (event.key === 'Escape') { this.setState({ hasClicked: false }); this.onFilterChange(this.props.colName, ''); } return; } private onInputBoxChanged(event: React.ChangeEvent) { this.debouncedChangeHandler(this.props.colName, event.target.value); return; } private onMouseEnterHandler() { this.setState({ hasHovered: true }); return; } private onMouseLeaveHandler() { this.setState({ hasHovered: false }); return; } private onClickHandler() { this.setState({ hasClicked: true }); return; } private onDownClickHandler() { this.runSearch(this.props.onclickNext); return; } private onUpClickHandler() { this.runSearch(this.props.onclickPrevious); return; } private onCloseClickHandler(event: React.MouseEvent) { this.setState({ hasClicked: false }); this.onFilterChange(this.props.colName, ''); event.stopPropagation(); return; } }