UNPKG

react-bootstrap-typeahead

Version:
89 lines (86 loc) 3.75 kB
"use strict"; var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.useHint = exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); var _Context = require("../../core/Context"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; } // IE doesn't seem to get the composite computed value (eg: 'padding', // 'borderStyle', etc.), so generate these from the individual values. function interpolateStyle(styles, attr) { var subattr = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ''; // Title-case the sub-attribute. if (subattr) { /* eslint-disable-next-line no-param-reassign */ subattr = subattr.replace(subattr[0], subattr[0].toUpperCase()); } return ['Top', 'Right', 'Bottom', 'Left'].map(function (dir) { return styles["".concat(attr).concat(dir).concat(subattr)]; }).join(' '); } function copyStyles(inputNode, hintNode) { var inputStyle = window.getComputedStyle(inputNode); /* eslint-disable no-param-reassign */ hintNode.style.borderStyle = interpolateStyle(inputStyle, 'border', 'style'); hintNode.style.borderWidth = interpolateStyle(inputStyle, 'border', 'width'); hintNode.style.fontSize = inputStyle.fontSize; hintNode.style.fontWeight = inputStyle.fontWeight; hintNode.style.height = inputStyle.height; hintNode.style.lineHeight = inputStyle.lineHeight; hintNode.style.margin = interpolateStyle(inputStyle, 'margin'); hintNode.style.padding = interpolateStyle(inputStyle, 'padding'); /* eslint-enable no-param-reassign */ } var useHint = exports.useHint = function useHint() { var _useTypeaheadContext = (0, _Context.useTypeaheadContext)(), hintText = _useTypeaheadContext.hintText, inputNode = _useTypeaheadContext.inputNode; var hintRef = (0, _react.useRef)(null); (0, _react.useEffect)(function () { if (inputNode && hintRef.current) { copyStyles(inputNode, hintRef.current); } }); return { hintRef: hintRef, hintText: hintText }; }; var Hint = function Hint(_ref) { var children = _ref.children, className = _ref.className; var _useHint = useHint(), hintRef = _useHint.hintRef, hintText = _useHint.hintText; return /*#__PURE__*/_react["default"].createElement("div", { className: className, style: { display: 'flex', flex: 1, height: '100%', position: 'relative' } }, children, /*#__PURE__*/_react["default"].createElement("input", { "aria-hidden": true, className: "rbt-input-hint", ref: hintRef, readOnly: true, style: { backgroundColor: 'transparent', borderColor: 'transparent', boxShadow: 'none', color: 'rgba(0, 0, 0, 0.54)', left: 0, pointerEvents: 'none', position: 'absolute', top: 0, width: '100%' }, tabIndex: -1, value: hintText })); }; var _default = exports["default"] = Hint;