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.

41 lines 2.29 kB
"use strict"; 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); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HighlightedResult = void 0; var jsx_runtime_1 = require("react/jsx-runtime"); var classnames_1 = __importDefault(require("classnames")); /** * The `HighlightedResult` component can be used to bold specific letters * within the `children` if the `children` is a string. */ 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 (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children }); } var i = children.toLowerCase().indexOf(value.toLowerCase()); if (i === -1) { return (0, jsx_runtime_1.jsx)(jsx_runtime_1.Fragment, { children: children }); } var end = i + value.length; var id = propId; if (id && index > 0) { id = "".concat(id, "-").concat(index); } return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [i > 0 && children.substring(0, i), (0, jsx_runtime_1.jsx)("span", __assign({ id: id, style: style, className: (0, classnames_1.default)("rmd-typography--bold", className) }, { children: children.substring(i, end) })), end < children.length && ((0, jsx_runtime_1.jsx)(HighlightedResult, __assign({ style: style, className: className, value: value, enabled: enabled && repeatable, repeatable: repeatable, index: index + 1 }, { children: children.substring(end) })))] })); } exports.HighlightedResult = HighlightedResult; //# sourceMappingURL=HighlightedResult.js.map