@happeouikit/form-elements 2.1.0 → 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Input.js CHANGED
@@ -1,9 +1,7 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
- var _excluded = ["label", "icon", "iconProps", "state", "errorMessage"],
4
- _excluded2 = ["label"];
5
3
 
6
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
4
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
7
5
 
8
6
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
9
7
 
@@ -12,6 +10,7 @@ import styled from "styled-components";
12
10
  import { placeholder } from "polished";
13
11
  import { gray01, gray04, gray08, gray05, gray06, active, black, success, alert } from "@happeouikit/colors";
14
12
  import { TinyText, sansFamily, BodyUI } from "@happeouikit/typography";
13
+ import { uuidv4 } from "./utils";
15
14
  /**
16
15
  * Map input states to colors
17
16
  * @type {{default: string, success: string, error: string}}
@@ -30,18 +29,24 @@ export var Input = function Input(_ref) {
30
29
  iconProps = _ref.iconProps,
31
30
  state = _ref.state,
32
31
  errorMessage = _ref.errorMessage,
33
- rest = _objectWithoutProperties(_ref, _excluded);
32
+ rest = _objectWithoutProperties(_ref, ["label", "icon", "iconProps", "state", "errorMessage"]);
34
33
 
34
+ var id = React.useMemo(uuidv4, []);
35
+ var accessibility = label ? {
36
+ "aria-labelledby": id
37
+ } : {};
35
38
  return /*#__PURE__*/_jsxs(Container, {
36
39
  children: [!!label && /*#__PURE__*/_jsx(InputLabel, {
40
+ id: id,
37
41
  children: /*#__PURE__*/_jsx(BodyUI, {
38
42
  color: gray01,
39
43
  children: label
40
44
  })
41
- }), icon && new iconWrapper(icon, iconProps)(), /*#__PURE__*/_jsx(StyledInput, _objectSpread({
45
+ }), icon && new IconWrapper(icon, iconProps)(), /*#__PURE__*/_jsx(StyledInput, _objectSpread({
46
+ title: label || "Styled input",
42
47
  hasIcon: (icon || "").toString(),
43
48
  state: state || "default"
44
- }, rest)), state === "error" && errorMessage && /*#__PURE__*/_jsx(ErrorMessage, {
49
+ }, accessibility, {}, rest)), state === "error" && errorMessage && /*#__PURE__*/_jsx(ErrorMessage, {
45
50
  color: alert,
46
51
  children: errorMessage
47
52
  })]
@@ -54,7 +59,7 @@ export var Input = function Input(_ref) {
54
59
  * @returns {function(): *}
55
60
  */
56
61
 
57
- function iconWrapper(IconComponent, iconProps) {
62
+ var IconWrapper = function IconWrapper(IconComponent, iconProps) {
58
63
  var iconDefaults = {
59
64
  width: "20px",
60
65
  height: "20px",
@@ -66,15 +71,17 @@ function iconWrapper(IconComponent, iconProps) {
66
71
  left: "10px",
67
72
  fontSize: "18px"
68
73
  };
69
- var props = Object.assign({}, iconDefaults, iconProps);
74
+
75
+ var props = _objectSpread({}, iconDefaults, {}, iconProps);
76
+
70
77
  return function () {
71
78
  return /*#__PURE__*/_jsx(InputIconWrapper, {
72
- children: /*#__PURE__*/_jsx(IconComponent, _objectSpread(_objectSpread({}, props), {}, {
79
+ children: /*#__PURE__*/_jsx(IconComponent, _objectSpread({}, props, {
73
80
  style: style
74
81
  }))
75
82
  });
76
83
  };
77
- }
84
+ };
78
85
 
79
86
  var ErrorMessage = styled(TinyText).withConfig({
80
87
  displayName: "Input__ErrorMessage",
@@ -134,10 +141,10 @@ var InputIconWrapper = styled.span.withConfig({
134
141
 
135
142
  export var InputNumber = function InputNumber(_ref8) {
136
143
  var label = _ref8.label,
137
- props = _objectWithoutProperties(_ref8, _excluded2);
144
+ props = _objectWithoutProperties(_ref8, ["label"]);
138
145
 
139
146
  return /*#__PURE__*/_jsxs(InputNumberContainer, {
140
- children: [/*#__PURE__*/_jsx(StyledInputNumber, _objectSpread(_objectSpread({}, props), {}, {
147
+ children: [/*#__PURE__*/_jsx(StyledInputNumber, _objectSpread({}, props, {
141
148
  type: "number"
142
149
  })), label && /*#__PURE__*/_jsx(InputNumberLabel, {
143
150
  children: label
package/dist/Link.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
 
3
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
4
4
 
5
5
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
6
6
 
@@ -11,26 +11,26 @@ import { BodyUI } from "@happeouikit/typography";
11
11
  import { active } from "@happeouikit/colors";
12
12
  import { jsx as _jsx } from "react/jsx-runtime";
13
13
  export var Link = /*#__PURE__*/React.forwardRef(function (props, ref) {
14
- return /*#__PURE__*/_jsx(StyledLink, _objectSpread(_objectSpread({
14
+ return /*#__PURE__*/_jsx(StyledLink, _objectSpread({
15
15
  ref: ref,
16
16
  as: "a"
17
- }, props), {}, {
17
+ }, props, {
18
18
  children: props.children
19
19
  }));
20
20
  });
21
21
  export var LinkExternal = /*#__PURE__*/React.forwardRef(function (props, ref) {
22
- return /*#__PURE__*/_jsx(StyledLink, _objectSpread(_objectSpread({
22
+ return /*#__PURE__*/_jsx(StyledLink, _objectSpread({
23
23
  ref: ref,
24
24
  as: "a",
25
25
  target: "_blank",
26
26
  rel: "nofollow noopener"
27
- }, props), {}, {
27
+ }, props, {
28
28
  children: props.children
29
29
  }));
30
30
  });
31
31
  var StyledLink = styled(BodyUI).withConfig({
32
32
  displayName: "Link__StyledLink",
33
- componentId: "sc-jezurh-0"
33
+ componentId: "jezurh-0"
34
34
  })(["text-decoration:", ";color:", ";:hover{color:", ";text-decoration:underline;}"], function (_ref) {
35
35
  var underline = _ref.underline;
36
36
  return underline ? "underline" : "none";
@@ -1,8 +1,7 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
- var _excluded = ["label"];
4
3
 
5
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
4
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
6
5
 
7
6
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
8
7
 
@@ -16,7 +15,7 @@ import { jsxs as _jsxs } from "react/jsx-runtime";
16
15
 
17
16
  var RadioButton = function RadioButton(_ref) {
18
17
  var label = _ref.label,
19
- props = _objectWithoutProperties(_ref, _excluded);
18
+ props = _objectWithoutProperties(_ref, ["label"]);
20
19
 
21
20
  return /*#__PURE__*/_jsxs(RadioButtonWrapper, {
22
21
  children: [/*#__PURE__*/_jsx("input", _objectSpread({
package/dist/Switch.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
- var _excluded = ["label", "tabIndex"];
4
3
 
5
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
4
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
6
5
 
7
6
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
8
7
 
@@ -17,7 +16,7 @@ import { jsxs as _jsxs } from "react/jsx-runtime";
17
16
  var Switch = function Switch(_ref) {
18
17
  var label = _ref.label,
19
18
  tabIndex = _ref.tabIndex,
20
- props = _objectWithoutProperties(_ref, _excluded);
19
+ props = _objectWithoutProperties(_ref, ["label", "tabIndex"]);
21
20
 
22
21
  var checkboxRef = useRef();
23
22
 
@@ -30,10 +29,10 @@ var Switch = function Switch(_ref) {
30
29
  };
31
30
 
32
31
  return /*#__PURE__*/_jsxs(ToggleSwitchWrapper, {
33
- children: [/*#__PURE__*/_jsx("input", _objectSpread(_objectSpread({
32
+ children: [/*#__PURE__*/_jsx("input", _objectSpread({
34
33
  ref: checkboxRef,
35
34
  type: "checkbox"
36
- }, props), {}, {
35
+ }, props, {
37
36
  tabIndex: -1
38
37
  })), /*#__PURE__*/_jsx(StyledSwitch, _objectSpread({
39
38
  onKeyDown: onKeyDown,
package/dist/Tag.js CHANGED
@@ -1,9 +1,8 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
3
  import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
4
- var _excluded = ["text", "closeCallback", "isUppercase"];
5
4
 
6
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
5
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
7
6
 
8
7
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
9
8
 
@@ -20,7 +19,7 @@ var Tag = function Tag(_ref) {
20
19
  var text = _ref.text,
21
20
  closeCallback = _ref.closeCallback,
22
21
  isUppercase = _ref.isUppercase,
23
- props = _objectWithoutProperties(_ref, _excluded);
22
+ props = _objectWithoutProperties(_ref, ["text", "closeCallback", "isUppercase"]);
24
23
 
25
24
  var _useState = useState(true),
26
25
  _useState2 = _slicedToArray(_useState, 2),
@@ -32,7 +31,7 @@ var Tag = function Tag(_ref) {
32
31
  closeCallback && closeCallback();
33
32
  };
34
33
 
35
- return show && /*#__PURE__*/_jsxs(TagWrapper, _objectSpread(_objectSpread({}, props), {}, {
34
+ return show && /*#__PURE__*/_jsxs(TagWrapper, _objectSpread({}, props, {
36
35
  children: [/*#__PURE__*/_jsx(Text, {
37
36
  bold: true,
38
37
  uppercase: isUppercase,
package/dist/Textarea.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
2
  import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3
- var _excluded = ["cols", "rows", "label"];
4
3
 
5
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
4
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
6
5
 
7
6
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
8
7
 
@@ -18,7 +17,7 @@ var Textarea = function Textarea(_ref) {
18
17
  var cols = _ref.cols,
19
18
  rows = _ref.rows,
20
19
  label = _ref.label,
21
- props = _objectWithoutProperties(_ref, _excluded);
20
+ props = _objectWithoutProperties(_ref, ["cols", "rows", "label"]);
22
21
 
23
22
  return /*#__PURE__*/_jsxs(Fragment, {
24
23
  children: [!!label && /*#__PURE__*/_jsx(Label, {
@@ -0,0 +1,234 @@
1
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
2
+ import { useState, useEffect, useRef } from "react";
3
+ import PropTypes from "prop-types";
4
+ import { IconSearch } from "@happeouikit/icons";
5
+ import styled from "styled-components";
6
+ import { hooks } from "@happeo/react-core";
7
+ import { Input } from "../Input";
8
+ import TypeaheadList from "./TypeaheadList";
9
+ import TypeaheadDropdown from "./TypeaheadDropdown";
10
+ import TypeaheadError from "./TypeaheadError";
11
+ import TypeaheadNoResults from "./TypeaheadNoResults";
12
+ import TypeaheadLoading from "./TypeaheadLoading";
13
+ import { jsx as _jsx } from "react/jsx-runtime";
14
+ import { jsxs as _jsxs } from "react/jsx-runtime";
15
+
16
+ var Typeahead = function Typeahead(_ref) {
17
+ var onQuery = _ref.onQuery,
18
+ loading = _ref.loading,
19
+ results = _ref.results,
20
+ afterSelect = _ref.afterSelect,
21
+ keepResult = _ref.keepResult,
22
+ placeholder = _ref.placeholder,
23
+ errorMessage = _ref.errorMessage,
24
+ queryError = _ref.queryError,
25
+ dropdownMaxHeight = _ref.dropdownMaxHeight;
26
+
27
+ // Need to store query as object to support change detection when debouncing from query to the same query
28
+ var _useState = useState({
29
+ q: ""
30
+ }),
31
+ _useState2 = _slicedToArray(_useState, 2),
32
+ query = _useState2[0],
33
+ setQuery = _useState2[1];
34
+
35
+ var debouncedQuery = hooks.useDebounce(query, 300);
36
+
37
+ var _useState3 = useState(null),
38
+ _useState4 = _slicedToArray(_useState3, 2),
39
+ focusedResultIndex = _useState4[0],
40
+ setFocusedResultIndex = _useState4[1];
41
+
42
+ var _useState5 = useState(true),
43
+ _useState6 = _slicedToArray(_useState5, 2),
44
+ allowDropdown = _useState6[0],
45
+ setAllowDropdown = _useState6[1];
46
+
47
+ var _useState7 = useState(null),
48
+ _useState8 = _slicedToArray(_useState7, 2),
49
+ selectedResult = _useState8[0],
50
+ setSelectedResult = _useState8[1];
51
+
52
+ var selfRef = useRef(null);
53
+ var selectedRef = useRef(null);
54
+ var userIsTyping = query !== debouncedQuery;
55
+ var showDropDown = query.q.length > 1 && !selectedResult && allowDropdown; // The parent may want to keep the result visible until some further user interaction
56
+ // Then keepResult may be toggled OFF to clear the input for next query and set back ON at onQuery
57
+
58
+ useEffect(function () {
59
+ if (!keepResult) setQuery({
60
+ q: ""
61
+ });
62
+ }, [keepResult]);
63
+ useEffect(function () {
64
+ // Debouncing timer might still be running after result is selected, so ignore false triggers
65
+ if (selectedResult) return;
66
+ var q = debouncedQuery.q;
67
+ if (q.length < 2) return;
68
+ onQuery(q);
69
+ }, [onQuery, debouncedQuery, selectedResult]);
70
+ useEffect(function () {
71
+ if (results.length === 0) {
72
+ setFocusedResultIndex(null);
73
+ }
74
+
75
+ if (results.length > 0) {
76
+ setFocusedResultIndex(0);
77
+ }
78
+ }, [results]);
79
+
80
+ var focusNextResult = function focusNextResult() {
81
+ return focusedResultIndex < results.length - 1 ? focusedResultIndex + 1 : focusedResultIndex;
82
+ };
83
+
84
+ var focusPrevResult = function focusPrevResult() {
85
+ return focusedResultIndex > 0 ? focusedResultIndex - 1 : focusedResultIndex;
86
+ };
87
+
88
+ var selectResult = function selectResult(result) {
89
+ if (keepResult && result.text) {
90
+ setQuery({
91
+ q: result.text
92
+ });
93
+ } else {
94
+ setQuery({
95
+ q: ""
96
+ });
97
+ }
98
+
99
+ setSelectedResult(result);
100
+ afterSelect(result);
101
+ setFocusedResultIndex(null);
102
+ };
103
+
104
+ var valueChangeHandler = function valueChangeHandler(event) {
105
+ var value = event.target.value;
106
+ setQuery({
107
+ q: value
108
+ });
109
+ setAllowDropdown(true);
110
+ setSelectedResult(null);
111
+ setFocusedResultIndex(null); // notify parent that selection was erased
112
+
113
+ if (selectedResult) {
114
+ afterSelect(null);
115
+ }
116
+ };
117
+
118
+ useEffect(function () {
119
+ if (!selectedRef.current) return;
120
+ selectedRef.current.scrollIntoView({
121
+ block: "nearest"
122
+ });
123
+ }, [focusedResultIndex]);
124
+
125
+ var keyDownHandler = function keyDownHandler(event) {
126
+ if (!showDropDown) return;
127
+ if (queryError || userIsTyping || loading) return;
128
+
129
+ if (event.key === "ArrowUp") {
130
+ setFocusedResultIndex(focusPrevResult());
131
+ event.preventDefault();
132
+ } else if (event.key === "ArrowDown") {
133
+ setFocusedResultIndex(focusNextResult());
134
+ event.preventDefault();
135
+ } else if (event.key === "Enter") {
136
+ if (focusedResultIndex != null) {
137
+ selectResult(results[focusedResultIndex]);
138
+ event.preventDefault();
139
+ }
140
+ }
141
+ };
142
+
143
+ var resultClickHandler = function resultClickHandler(result) {
144
+ selectResult(result);
145
+ };
146
+
147
+ useEffect(function () {
148
+ var handleWindowClick = function handleWindowClick(event) {
149
+ if (selfRef.current && !selfRef.current.contains(event.target)) {
150
+ setAllowDropdown(false);
151
+ } else {
152
+ setAllowDropdown(true);
153
+ }
154
+ };
155
+
156
+ document.addEventListener("pointerdown", handleWindowClick);
157
+ return function cleanup() {
158
+ document.removeEventListener("pointerdown", handleWindowClick);
159
+ };
160
+ }, [selfRef]);
161
+
162
+ var renderDropdownContent = function renderDropdownContent() {
163
+ if (queryError) {
164
+ return /*#__PURE__*/_jsx(TypeaheadError, {});
165
+ }
166
+
167
+ if (userIsTyping || loading) {
168
+ return /*#__PURE__*/_jsx(TypeaheadLoading, {
169
+ query: query.q
170
+ });
171
+ }
172
+
173
+ if (results.length > 0) {
174
+ return /*#__PURE__*/_jsx(TypeaheadList, {
175
+ results: results,
176
+ focusedResultIndex: focusedResultIndex,
177
+ onResultClick: resultClickHandler,
178
+ ref: selectedRef
179
+ });
180
+ }
181
+
182
+ return /*#__PURE__*/_jsx(TypeaheadNoResults, {
183
+ query: query.q
184
+ });
185
+ };
186
+
187
+ return /*#__PURE__*/_jsxs(Wrapper, {
188
+ ref: selfRef,
189
+ children: [/*#__PURE__*/_jsx(Input, {
190
+ placeholder: placeholder,
191
+ icon: IconSearch,
192
+ value: query.q,
193
+ name: "inputValue",
194
+ onChange: valueChangeHandler,
195
+ onKeyDown: keyDownHandler,
196
+ state: errorMessage ? "error" : "default",
197
+ errorMessage: errorMessage,
198
+ autoComplete: "off"
199
+ }), showDropDown && /*#__PURE__*/_jsx(TypeaheadDropdown, {
200
+ maxHeight: dropdownMaxHeight,
201
+ children: renderDropdownContent()
202
+ })]
203
+ });
204
+ };
205
+
206
+ var Wrapper = styled.div.withConfig({
207
+ displayName: "Typeahead__Wrapper",
208
+ componentId: "sc-16j2d25-0"
209
+ })(["width:100%;"]);
210
+ Typeahead.propTypes = {
211
+ onQuery: PropTypes.func.isRequired,
212
+ loading: PropTypes.bool.isRequired,
213
+ results: PropTypes.arrayOf(PropTypes.shape({
214
+ text: PropTypes.string,
215
+ // This is used in association with keepResult
216
+ component: PropTypes.element.isRequired,
217
+ data: PropTypes.shape // You may add any data related to the result, so you can use it on afterSelect
218
+
219
+ })).isRequired,
220
+ afterSelect: PropTypes.func.isRequired,
221
+ keepResult: PropTypes.bool,
222
+ placeholder: PropTypes.string,
223
+ errorMessage: PropTypes.string,
224
+ queryError: PropTypes.bool,
225
+ dropdownMaxHeight: PropTypes.string
226
+ };
227
+ Typeahead.defaultProps = {
228
+ keepResult: false,
229
+ placeholder: "",
230
+ errorMessage: null,
231
+ queryError: false,
232
+ dropdownMaxHeight: "350px"
233
+ };
234
+ export default Typeahead;
@@ -0,0 +1,27 @@
1
+ import styled from "styled-components";
2
+ import { shadow400 } from "@happeouikit/theme";
3
+ import { white } from "@happeouikit/colors";
4
+ import { jsx as _jsx } from "react/jsx-runtime";
5
+
6
+ var TypeaheadDropdown = function TypeaheadDropdown(_ref) {
7
+ var maxHeight = _ref.maxHeight,
8
+ children = _ref.children;
9
+ return /*#__PURE__*/_jsx(Dropdown, {
10
+ children: /*#__PURE__*/_jsx(DropdownContent, {
11
+ maxHeight: maxHeight,
12
+ children: children
13
+ })
14
+ });
15
+ };
16
+
17
+ var Dropdown = styled.div.withConfig({
18
+ displayName: "TypeaheadDropdown__Dropdown",
19
+ componentId: "c2clla-0"
20
+ })(["position:relative;display:block;width:100%;z-index:1000;"]);
21
+ var DropdownContent = styled.div.withConfig({
22
+ displayName: "TypeaheadDropdown__DropdownContent",
23
+ componentId: "c2clla-1"
24
+ })(["position:absolute;display:block;width:100%;background:", ";margin-top:8px;padding-top:8px;padding-bottom:8px;box-shadow:", ";border-radius:4px;max-height:", ";overflow-y:auto;"], white, shadow400, function (props) {
25
+ return props.maxHeight;
26
+ });
27
+ export default TypeaheadDropdown;
@@ -0,0 +1,16 @@
1
+ import { IconError } from "@happeouikit/icons";
2
+ import { useIntl } from "react-intl";
3
+ import messages from "./messages";
4
+ import TypeaheadMessage from "./TypeaheadMessage";
5
+ import { jsx as _jsx } from "react/jsx-runtime";
6
+
7
+ var TypeaheadError = function TypeaheadError() {
8
+ var intl = useIntl();
9
+ return /*#__PURE__*/_jsx(TypeaheadMessage, {
10
+ icon: IconError,
11
+ title: intl.formatMessage(messages.typeaheadServerError),
12
+ details: intl.formatMessage(messages.typeaheadServerErrorDetails)
13
+ });
14
+ };
15
+
16
+ export default TypeaheadError;
@@ -0,0 +1,37 @@
1
+ import React, { Fragment } from "react";
2
+ import styled from "styled-components";
3
+ import { active, toBgLight, lighten40 } from "@happeouikit/colors";
4
+ import { radius400 } from "@happeouikit/theme";
5
+ import { jsx as _jsx } from "react/jsx-runtime";
6
+ var FocusStyle = {
7
+ border: "solid 1px ".concat(lighten40(active)),
8
+ borderRadius: radius400
9
+ };
10
+ var TypeaheadList = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
11
+ var results = _ref.results,
12
+ focusedResultIndex = _ref.focusedResultIndex,
13
+ onResultClick = _ref.onResultClick;
14
+ return /*#__PURE__*/_jsx(Container, {
15
+ children: results.map(function (result, index) {
16
+ return /*#__PURE__*/_jsx(Fragment, {
17
+ children: /*#__PURE__*/_jsx(Row, {
18
+ onClick: function onClick() {
19
+ return onResultClick(result);
20
+ },
21
+ style: index === focusedResultIndex ? FocusStyle : {},
22
+ ref: index === focusedResultIndex ? ref : null,
23
+ children: result.component
24
+ })
25
+ }, result.text || index);
26
+ })
27
+ });
28
+ });
29
+ var Container = styled.ul.withConfig({
30
+ displayName: "TypeaheadList__Container",
31
+ componentId: "ce1llk-0"
32
+ })(["padding:0px;margin:0px;"]);
33
+ var Row = styled.li.withConfig({
34
+ displayName: "TypeaheadList__Row",
35
+ componentId: "ce1llk-1"
36
+ })(["padding:9px 15px 9px 15px;border:solid 1px transparent;list-style-type:none;&:hover{background:", ";}"], toBgLight(active));
37
+ export default TypeaheadList;
@@ -0,0 +1,26 @@
1
+ import { Loader } from "@happeouikit/loaders";
2
+ import { useIntl } from "react-intl";
3
+ import messages from "./messages";
4
+ import TypeaheadMessage, { ICON_SIZE } from "./TypeaheadMessage";
5
+ import { jsx as _jsx } from "react/jsx-runtime";
6
+
7
+ var FixedSizeLoader = function FixedSizeLoader() {
8
+ return /*#__PURE__*/_jsx(Loader, {
9
+ containerWidth: ICON_SIZE,
10
+ containerHeight: ICON_SIZE
11
+ });
12
+ };
13
+
14
+ var TypeaheadLoading = function TypeaheadLoading(_ref) {
15
+ var query = _ref.query;
16
+ var intl = useIntl();
17
+ return /*#__PURE__*/_jsx(TypeaheadMessage, {
18
+ icon: FixedSizeLoader,
19
+ title: intl.formatMessage(messages.typeaheadSearching),
20
+ details: intl.formatMessage(messages.typeaheadSearchingDetails, {
21
+ query: query
22
+ })
23
+ });
24
+ };
25
+
26
+ export default TypeaheadLoading;
@@ -0,0 +1,41 @@
1
+ import styled from "styled-components";
2
+ import { TextEpsilon, BodyUI } from "@happeouikit/typography";
3
+ import { gray07, gray04 } from "@happeouikit/colors";
4
+ import { jsx as _jsx } from "react/jsx-runtime";
5
+ import { jsxs as _jsxs } from "react/jsx-runtime";
6
+ export var ICON_SIZE = "48px";
7
+ var IconStyle = {
8
+ width: ICON_SIZE,
9
+ height: ICON_SIZE,
10
+ fill: gray07
11
+ };
12
+
13
+ var TypeaheadMessage = function TypeaheadMessage(_ref) {
14
+ var icon = _ref.icon,
15
+ title = _ref.title,
16
+ details = _ref.details;
17
+ var Icon = icon;
18
+ return /*#__PURE__*/_jsxs(Flexbox, {
19
+ children: [/*#__PURE__*/_jsx(Icon, {
20
+ style: IconStyle
21
+ }), /*#__PURE__*/_jsx(Title, {
22
+ children: title
23
+ }), /*#__PURE__*/_jsx(Details, {
24
+ children: details
25
+ })]
26
+ });
27
+ };
28
+
29
+ var Title = styled(TextEpsilon).withConfig({
30
+ displayName: "TypeaheadMessage__Title",
31
+ componentId: "sc-1yxn5eq-0"
32
+ })(["text-align:center;margin-bottom:8px;"]);
33
+ var Details = styled(BodyUI).withConfig({
34
+ displayName: "TypeaheadMessage__Details",
35
+ componentId: "sc-1yxn5eq-1"
36
+ })(["text-align:center;color:", ";"], gray04);
37
+ var Flexbox = styled.div.withConfig({
38
+ displayName: "TypeaheadMessage__Flexbox",
39
+ componentId: "sc-1yxn5eq-2"
40
+ })(["display:flex;flex-direction:column;align-items:center;padding:24px;"]);
41
+ export default TypeaheadMessage;
@@ -0,0 +1,19 @@
1
+ import { useIntl } from "react-intl";
2
+ import { IconSearch } from "@happeouikit/icons";
3
+ import messages from "./messages";
4
+ import TypeaheadMessage from "./TypeaheadMessage";
5
+ import { jsx as _jsx } from "react/jsx-runtime";
6
+
7
+ var TypeaheadNoResults = function TypeaheadNoResults(_ref) {
8
+ var query = _ref.query;
9
+ var intl = useIntl();
10
+ return /*#__PURE__*/_jsx(TypeaheadMessage, {
11
+ icon: IconSearch,
12
+ title: intl.formatMessage(messages.typeaheadNoResults),
13
+ details: intl.formatMessage(messages.typeaheadNoResultsDetails, {
14
+ query: query
15
+ })
16
+ });
17
+ };
18
+
19
+ export default TypeaheadNoResults;
@@ -0,0 +1,29 @@
1
+ import { defineMessages } from "react-intl";
2
+ export default defineMessages({
3
+ typeaheadNoResults: {
4
+ id: "typeahead.noResults",
5
+ defaultMessage: "No results found"
6
+ },
7
+ typeaheadNoResultsDetails: {
8
+ id: "typeahead.noResultsDetails",
9
+ // eslint-disable-next-line quotes
10
+ defaultMessage: 'We found no results for "{query}"'
11
+ },
12
+ typeaheadServerError: {
13
+ id: "typeahead.serverError",
14
+ defaultMessage: "Server Error"
15
+ },
16
+ typeaheadServerErrorDetails: {
17
+ id: "typeahead.serverErrorDetails",
18
+ defaultMessage: "Oh no, there seems to be an issue with our servers. Please try again later"
19
+ },
20
+ typeaheadSearching: {
21
+ id: "typeahead.searching",
22
+ defaultMessage: "Searching..."
23
+ },
24
+ typeaheadSearchingDetails: {
25
+ id: "typeahead.searchingDetails",
26
+ // eslint-disable-next-line quotes
27
+ defaultMessage: 'Please wait as we try to find "{query}"'
28
+ }
29
+ });