UNPKG

@react-md/autocomplete

Version:

Create an accessible autocomplete component that allows a user to get real-time suggestions as they type within an input. This component can also be hooked up to a backend API that handles additional filtering or sorting.

34 lines 1.85 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import cn from "classnames"; /** * The `HighlightedResult` component can be used to bold specific letters * within the `children` if the `children` is a string. */ export function HighlightedResult(_a) { var propId = _a.id, style = _a.style, className = _a.className, _b = _a.enabled, enabled = _b === void 0 ? true : _b, value = _a.value, children = _a.children, _c = _a.repeatable, repeatable = _c === void 0 ? false : _c, _d = _a.index, index = _d === void 0 ? 0 : _d; if (!enabled || !value || typeof children !== "string") { return _jsx(_Fragment, { children: children }); } var i = children.toLowerCase().indexOf(value.toLowerCase()); if (i === -1) { return _jsx(_Fragment, { children: children }); } var end = i + value.length; var id = propId; if (id && index > 0) { id = "".concat(id, "-").concat(index); } return (_jsxs(_Fragment, { children: [i > 0 && children.substring(0, i), _jsx("span", __assign({ id: id, style: style, className: cn("rmd-typography--bold", className) }, { children: children.substring(i, end) })), end < children.length && (_jsx(HighlightedResult, __assign({ style: style, className: className, value: value, enabled: enabled && repeatable, repeatable: repeatable, index: index + 1 }, { children: children.substring(end) })))] })); } //# sourceMappingURL=HighlightedResult.js.map