@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.
50 lines • 2.39 kB
JavaScript
"use strict";
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 react_1 = __importDefault(require("react"));
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 react_1.default.createElement(react_1.default.Fragment, null, children);
}
var i = children.toLowerCase().indexOf(value.toLowerCase());
if (i === -1) {
return react_1.default.createElement(react_1.default.Fragment, null, children);
}
var end = i + value.length;
var id = propId;
if (id && index > 0) {
id = id + "-" + index;
}
return (react_1.default.createElement(react_1.default.Fragment, null,
i > 0 && children.substring(0, i),
react_1.default.createElement("span", { id: id, style: style, className: classnames_1.default("rmd-typography--bold", className) }, children.substring(i, end)),
end < children.length && (react_1.default.createElement(HighlightedResult, { style: style, className: className, value: value, enabled: enabled && repeatable, repeatable: repeatable, index: index + 1 }, children.substring(end)))));
}
exports.HighlightedResult = HighlightedResult;
/* 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