zim-fuzzysearch
Version:
Client side fuzzy search combined with seperate configurable result highlighting.
23 lines (22 loc) • 1.52 kB
JavaScript
import React from "react";
var FuzzyResultCtx = React.createContext(null);
/**
* Component to provide the result context in the children components.
* Can then be accessed via useFuzzyResult.
*/
export var FuzzyResultProvider = function (_a) {
var fuzzyResult = _a.fuzzyResult, tagConceptMap = _a.tagConceptMap, onSelect = _a.onSelect, onTagSelect = _a.onTagSelect, onFuseScoreToCssClass = _a.onFuseScoreToCssClass, resultCountLabel = _a.resultCountLabel, showResultGrid = _a.showResultGrid, children = _a.children;
if (!FuzzyResultCtx)
throw new ReferenceError("FuzzyResult context validated false. There must be an error inside the useFuzzyResult custom hook");
return (React.createElement(FuzzyResultCtx.Provider, { value: { fuzzyResult: fuzzyResult, onSelect: onSelect, onTagSelect: onTagSelect, resultCountLabel: resultCountLabel, showResultGrid: showResultGrid, tagConceptMap: tagConceptMap, onFuseScoreToCssClass: onFuseScoreToCssClass } }, children));
};
/**
* Uses the FuzzyResult context and exposes therefore exposes the fuzzyResult variable.
* Do not forget to previously import the FuzzyResultProvider and to assign it as parent
* component somewhere. (otherwise useFuzzyResult cannot work)
*/
export var useFuzzyResult = function () {
if (!FuzzyResultCtx)
throw new ReferenceError("FuzzyInputCtx is still null. Make sure to call the correct FuzzyResultProvider somewhere in the parent components.");
return React.useContext(FuzzyResultCtx);
};