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.

43 lines 1.95 kB
import React from "react"; 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 React.createElement(React.Fragment, null, children); } var i = children.toLowerCase().indexOf(value.toLowerCase()); if (i === -1) { return React.createElement(React.Fragment, null, children); } var end = i + value.length; var id = propId; if (id && index > 0) { id = id + "-" + index; } return (React.createElement(React.Fragment, null, i > 0 && children.substring(0, i), React.createElement("span", { id: id, style: style, className: cn("rmd-typography--bold", className) }, children.substring(i, end)), end < children.length && (React.createElement(HighlightedResult, { style: style, className: className, value: value, enabled: enabled && repeatable, repeatable: repeatable, index: index + 1 }, children.substring(end))))); } /* istanbul ignore next */ if (process.env.NODE_ENV !== "production") { try { var PropTypes = require("prop-types"); HighlightedResult.propTypes = { id: PropTypes.string, index: PropTypes.number, value: PropTypes.string.isRequired, style: PropTypes.object, className: PropTypes.string, children: PropTypes.node, enabled: PropTypes.bool, repeatable: PropTypes.bool, }; } catch (e) { } } //# sourceMappingURL=HighlightedResult.js.map