UNPKG

react-bootstrap-typeahead

Version:
106 lines (88 loc) 4.11 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(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } // 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 = 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 }; }; exports.useHint = useHint; 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 = Hint; exports["default"] = _default;