UNPKG

4.11 kBJavaScriptView Raw
1"use strict";
2
3var _typeof = require("@babel/runtime/helpers/typeof");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.useHint = exports["default"] = void 0;
9
10var _react = _interopRequireWildcard(require("react"));
11
12var _Context = require("../../core/Context");
13
14function _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); }
15
16function _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; }
17
18// IE doesn't seem to get the composite computed value (eg: 'padding',
19// 'borderStyle', etc.), so generate these from the individual values.
20function interpolateStyle(styles, attr) {
21 var subattr = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
22
23 // Title-case the sub-attribute.
24 if (subattr) {
25 /* eslint-disable-next-line no-param-reassign */
26 subattr = subattr.replace(subattr[0], subattr[0].toUpperCase());
27 }
28
29 return ['Top', 'Right', 'Bottom', 'Left'].map(function (dir) {
30 return styles["".concat(attr).concat(dir).concat(subattr)];
31 }).join(' ');
32}
33
34function copyStyles(inputNode, hintNode) {
35 var inputStyle = window.getComputedStyle(inputNode);
36 /* eslint-disable no-param-reassign */
37
38 hintNode.style.borderStyle = interpolateStyle(inputStyle, 'border', 'style');
39 hintNode.style.borderWidth = interpolateStyle(inputStyle, 'border', 'width');
40 hintNode.style.fontSize = inputStyle.fontSize;
41 hintNode.style.fontWeight = inputStyle.fontWeight;
42 hintNode.style.height = inputStyle.height;
43 hintNode.style.lineHeight = inputStyle.lineHeight;
44 hintNode.style.margin = interpolateStyle(inputStyle, 'margin');
45 hintNode.style.padding = interpolateStyle(inputStyle, 'padding');
46 /* eslint-enable no-param-reassign */
47}
48
49var useHint = function useHint() {
50 var _useTypeaheadContext = (0, _Context.useTypeaheadContext)(),
51 hintText = _useTypeaheadContext.hintText,
52 inputNode = _useTypeaheadContext.inputNode;
53
54 var hintRef = (0, _react.useRef)(null);
55 (0, _react.useEffect)(function () {
56 if (inputNode && hintRef.current) {
57 copyStyles(inputNode, hintRef.current);
58 }
59 });
60 return {
61 hintRef: hintRef,
62 hintText: hintText
63 };
64};
65
66exports.useHint = useHint;
67
68var Hint = function Hint(_ref) {
69 var children = _ref.children,
70 className = _ref.className;
71
72 var _useHint = useHint(),
73 hintRef = _useHint.hintRef,
74 hintText = _useHint.hintText;
75
76 return /*#__PURE__*/_react["default"].createElement("div", {
77 className: className,
78 style: {
79 display: 'flex',
80 flex: 1,
81 height: '100%',
82 position: 'relative'
83 }
84 }, children, /*#__PURE__*/_react["default"].createElement("input", {
85 "aria-hidden": true,
86 className: "rbt-input-hint",
87 ref: hintRef,
88 readOnly: true,
89 style: {
90 backgroundColor: 'transparent',
91 borderColor: 'transparent',
92 boxShadow: 'none',
93 color: 'rgba(0, 0, 0, 0.54)',
94 left: 0,
95 pointerEvents: 'none',
96 position: 'absolute',
97 top: 0,
98 width: '100%'
99 },
100 tabIndex: -1,
101 value: hintText
102 }));
103};
104
105var _default = Hint;
106exports["default"] = _default;
\No newline at end of file