@happeouikit/form-elements 2.0.1 → 2.3.0
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/CHANGELOG.md +20 -0
- package/dist/Checkbox.js +31 -6
- package/dist/CheckboxGroup.js +150 -0
- package/dist/DatePicker.js +65 -48
- package/dist/DateTimePicker.js +81 -56
- package/dist/Typeahead/Typeahead.js +234 -0
- package/dist/Typeahead/TypeaheadDropdown.js +27 -0
- package/dist/Typeahead/TypeaheadError.js +16 -0
- package/dist/Typeahead/TypeaheadList.js +37 -0
- package/dist/Typeahead/TypeaheadLoading.js +26 -0
- package/dist/Typeahead/TypeaheadMessage.js +41 -0
- package/dist/Typeahead/TypeaheadNoResults.js +19 -0
- package/dist/Typeahead/messages.js +29 -0
- package/dist/index.js +3 -1
- package/dist/utils.js +21 -0
- package/package.json +6 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 2.3.0
|
|
4
|
+
|
|
5
|
+
- [Added] Typeahead component
|
|
6
|
+
|
|
7
|
+
## 2.2.1
|
|
8
|
+
|
|
9
|
+
- [Fixed] build dist
|
|
10
|
+
|
|
11
|
+
## 2.2.0
|
|
12
|
+
|
|
13
|
+
- [Fixed] DatePicker. inability to control component by value prop
|
|
14
|
+
- [Fixed] DatePicker. focus loss on edit
|
|
15
|
+
- [Fixed] DatePicker. selected date highlight in the UI
|
|
16
|
+
- [Fixed] DatePicker. PropTypes
|
|
17
|
+
- [Fixed] DatePicker. disabled `react/jsx-props-no-spreading eslint rule`
|
|
18
|
+
|
|
19
|
+
## 2.1.0
|
|
20
|
+
|
|
21
|
+
- [Added] CheckboxGroup component
|
|
22
|
+
|
|
3
23
|
## 2.0.1
|
|
4
24
|
|
|
5
25
|
- [Fixed] InputNumber will only accept numerical value
|
package/dist/Checkbox.js
CHANGED
|
@@ -5,7 +5,7 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
|
|
|
5
5
|
|
|
6
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; }
|
|
7
7
|
|
|
8
|
-
import React from "react";
|
|
8
|
+
import React, { useRef } from "react";
|
|
9
9
|
import styled from "styled-components";
|
|
10
10
|
import PropTypes from "prop-types";
|
|
11
11
|
import { white, active, gray01, gray06, gray08 } from "@happeouikit/colors";
|
|
@@ -19,12 +19,30 @@ var Checkbox = function Checkbox(_ref) {
|
|
|
19
19
|
background = _ref.background,
|
|
20
20
|
props = _objectWithoutProperties(_ref, ["label", "background"]);
|
|
21
21
|
|
|
22
|
+
var checkboxRef = useRef();
|
|
23
|
+
|
|
24
|
+
var onKeyDown = function onKeyDown(event) {
|
|
25
|
+
if (event.key.match(/enter|spacebar|\W{1}/i)) {
|
|
26
|
+
event.preventDefault();
|
|
27
|
+
checkboxRef.current.checked = !checkboxRef.current.checked;
|
|
28
|
+
|
|
29
|
+
if (typeof props.onChange === "function") {
|
|
30
|
+
props.onChange(event);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
22
35
|
return /*#__PURE__*/_jsxs(CheckboxWrapper, {
|
|
23
36
|
background: background,
|
|
24
37
|
disabled: props.disabled,
|
|
25
38
|
children: [/*#__PURE__*/_jsx("input", _objectSpread({
|
|
26
|
-
|
|
27
|
-
|
|
39
|
+
ref: checkboxRef,
|
|
40
|
+
type: "checkbox",
|
|
41
|
+
tabIndex: "-1"
|
|
42
|
+
}, props)), /*#__PURE__*/_jsx(StyledCheckbox, _objectSpread({
|
|
43
|
+
onKeyDown: onKeyDown,
|
|
44
|
+
tabIndex: 0
|
|
45
|
+
}, props, {
|
|
28
46
|
children: /*#__PURE__*/_jsx(Checkmark, {})
|
|
29
47
|
})), label && /*#__PURE__*/_jsx(Label, {
|
|
30
48
|
children: label
|
|
@@ -33,12 +51,19 @@ var Checkbox = function Checkbox(_ref) {
|
|
|
33
51
|
};
|
|
34
52
|
|
|
35
53
|
Checkbox.propTypes = {
|
|
36
|
-
label: PropTypes.node
|
|
54
|
+
label: PropTypes.node,
|
|
55
|
+
value: PropTypes.string,
|
|
56
|
+
background: PropTypes.string
|
|
57
|
+
};
|
|
58
|
+
Checkbox.defaultProps = {
|
|
59
|
+
label: null,
|
|
60
|
+
value: undefined,
|
|
61
|
+
background: null
|
|
37
62
|
};
|
|
38
63
|
var StyledCheckbox = styled.span.withConfig({
|
|
39
64
|
displayName: "Checkbox__StyledCheckbox",
|
|
40
65
|
componentId: "tc08iy-0"
|
|
41
|
-
})(["cursor:pointer;position:absolute;height:18px;width:18px;border-radius:3px;background-color:", ";border:2px solid ", ";box-sizing:content-box !important;"], white, gray06);
|
|
66
|
+
})(["cursor:pointer;position:absolute;height:18px;width:18px;border-radius:3px;background-color:", ";border:2px solid ", ";box-sizing:content-box !important;&:focus-visible{outline:0;box-shadow:0 0 0 1px ", ";}"], white, gray06, active);
|
|
42
67
|
var Checkmark = styled(IconCheck).withConfig({
|
|
43
68
|
displayName: "Checkbox__Checkmark",
|
|
44
69
|
componentId: "tc08iy-1"
|
|
@@ -46,7 +71,7 @@ var Checkmark = styled(IconCheck).withConfig({
|
|
|
46
71
|
var CheckboxWrapper = styled.label.withConfig({
|
|
47
72
|
displayName: "Checkbox__CheckboxWrapper",
|
|
48
73
|
componentId: "tc08iy-2"
|
|
49
|
-
})(["display:flex;align-items:center;position:relative;cursor:", ";user-select:none;padding:4px 0;input{
|
|
74
|
+
})(["display:flex;align-items:center;position:relative;cursor:", ";user-select:none;padding:4px 0;input{opacity:0;width:0;height:0;}input:checked + ", "{background:", ";border:", ";}input:checked + ", " > svg{display:block;}input:disabled + ", "{cursor:not-allowed;background:", ";border:2px solid ", ";}input:disabled ~ p{color:", ";}"], function (_ref2) {
|
|
50
75
|
var disabled = _ref2.disabled;
|
|
51
76
|
return disabled ? "not-allowed" : " pointer";
|
|
52
77
|
}, StyledCheckbox, function (_ref3) {
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
|
|
2
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
3
|
+
import React, { useEffect, useMemo, useState } from "react";
|
|
4
|
+
import PropTypes from "prop-types";
|
|
5
|
+
import Checkbox from "./Checkbox";
|
|
6
|
+
import { devWarning, getValidChildrenArray, isString, useDevWarningOnce } from "./utils";
|
|
7
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
8
|
+
|
|
9
|
+
var getOptions = function getOptions(options) {
|
|
10
|
+
return options.map(function (option) {
|
|
11
|
+
return isString(option) ? {
|
|
12
|
+
label: option,
|
|
13
|
+
value: option
|
|
14
|
+
} : option;
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
var sortValuesByOrderInOptions = function sortValuesByOrderInOptions(options) {
|
|
19
|
+
return function (a, b) {
|
|
20
|
+
var indexA = options.findIndex(function (opt) {
|
|
21
|
+
return opt.value === a;
|
|
22
|
+
});
|
|
23
|
+
var indexB = options.findIndex(function (opt) {
|
|
24
|
+
return opt.value === b;
|
|
25
|
+
});
|
|
26
|
+
return indexA - indexB;
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
var getOptionsFromChildren = function getOptionsFromChildren(children) {
|
|
31
|
+
return getValidChildrenArray(children).map(function (_ref) {
|
|
32
|
+
var option = _ref.props;
|
|
33
|
+
return option;
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
var getChildrenDefaultValue = function getChildrenDefaultValue(options) {
|
|
38
|
+
return options.filter(function (_ref2) {
|
|
39
|
+
var defaultChecked = _ref2.defaultChecked;
|
|
40
|
+
return defaultChecked;
|
|
41
|
+
}).map(function (_ref3) {
|
|
42
|
+
var value = _ref3.value;
|
|
43
|
+
return value;
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
var optionsWarning = "[CheckboxGroup] Both the `children` and `options` props have been used.\n The `options` prop will be ignored. To get rid of this warning, remove one of these props.";
|
|
48
|
+
var defaultValueWarning = "[CheckboxGroup] Both the `defaultChecked` and `defaultValue` props have been used.\n The `defaultValue` prop will be ignored. To get rid of this warning, remove one of these props.";
|
|
49
|
+
|
|
50
|
+
var CheckboxGroup = function CheckboxGroup(_ref4) {
|
|
51
|
+
var value = _ref4.value,
|
|
52
|
+
disabled = _ref4.disabled,
|
|
53
|
+
name = _ref4.name,
|
|
54
|
+
children = _ref4.children,
|
|
55
|
+
className = _ref4.className,
|
|
56
|
+
rawOptions = _ref4.options,
|
|
57
|
+
onGroupChange = _ref4.onChange,
|
|
58
|
+
groupDefaultValue = _ref4.defaultValue;
|
|
59
|
+
useDevWarningOnce(children && rawOptions, optionsWarning);
|
|
60
|
+
var options = useMemo(function () {
|
|
61
|
+
return children ? getOptionsFromChildren(children) : getOptions(rawOptions);
|
|
62
|
+
}, [children, rawOptions]);
|
|
63
|
+
var defaultValue = useMemo(function () {
|
|
64
|
+
var childrenDefaultValue = getChildrenDefaultValue(options);
|
|
65
|
+
var hasChildrenDefaultValue = Boolean(childrenDefaultValue.length);
|
|
66
|
+
|
|
67
|
+
if (hasChildrenDefaultValue && groupDefaultValue) {
|
|
68
|
+
devWarning(defaultValueWarning);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return hasChildrenDefaultValue ? childrenDefaultValue : groupDefaultValue;
|
|
72
|
+
}, [options, groupDefaultValue]);
|
|
73
|
+
|
|
74
|
+
var _useState = useState(value || defaultValue || []),
|
|
75
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
76
|
+
groupValue = _useState2[0],
|
|
77
|
+
setGroupValue = _useState2[1];
|
|
78
|
+
|
|
79
|
+
useEffect(function () {
|
|
80
|
+
if (value) {
|
|
81
|
+
setGroupValue(value);
|
|
82
|
+
}
|
|
83
|
+
}, [value]);
|
|
84
|
+
|
|
85
|
+
var isChecked = function isChecked(optionValue) {
|
|
86
|
+
return groupValue.includes(optionValue);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
var handleChange = function handleChange(option, event) {
|
|
90
|
+
if (option.onChange) {
|
|
91
|
+
option.onChange(event);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
var newValue = event.target.checked ? [].concat(_toConsumableArray(groupValue), [option.value]) : groupValue.filter(function (val) {
|
|
95
|
+
return val !== option.value;
|
|
96
|
+
});
|
|
97
|
+
var newSortedValue = newValue.sort(sortValuesByOrderInOptions(options));
|
|
98
|
+
setGroupValue(newSortedValue);
|
|
99
|
+
|
|
100
|
+
if (onGroupChange) {
|
|
101
|
+
onGroupChange(newSortedValue);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
return /*#__PURE__*/_jsx("div", {
|
|
106
|
+
className: className,
|
|
107
|
+
children: options.map(function (option) {
|
|
108
|
+
return /*#__PURE__*/_jsx(Checkbox, {
|
|
109
|
+
label: option.label,
|
|
110
|
+
name: name,
|
|
111
|
+
disabled: option.disabled || disabled,
|
|
112
|
+
checked: isChecked(option.value),
|
|
113
|
+
onChange: function onChange(event) {
|
|
114
|
+
return handleChange(option, event);
|
|
115
|
+
}
|
|
116
|
+
}, option.value);
|
|
117
|
+
})
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
var optionPropType = PropTypes.oneOfType([PropTypes.shape({
|
|
122
|
+
label: PropTypes.string,
|
|
123
|
+
value: PropTypes.string.isRequired,
|
|
124
|
+
onChange: PropTypes.func
|
|
125
|
+
}), PropTypes.string]);
|
|
126
|
+
CheckboxGroup.propTypes = {
|
|
127
|
+
className: PropTypes.string,
|
|
128
|
+
options: PropTypes.arrayOf(optionPropType),
|
|
129
|
+
onChange: PropTypes.func,
|
|
130
|
+
value: PropTypes.arrayOf(PropTypes.string),
|
|
131
|
+
defaultValue: PropTypes.arrayOf(PropTypes.string),
|
|
132
|
+
disabled: PropTypes.bool,
|
|
133
|
+
name: PropTypes.string,
|
|
134
|
+
children: PropTypes.oneOfType([PropTypes.shape({
|
|
135
|
+
type: PropTypes.oneOf([Checkbox])
|
|
136
|
+
}), PropTypes.arrayOf(PropTypes.shape({
|
|
137
|
+
type: PropTypes.oneOf([Checkbox])
|
|
138
|
+
}))])
|
|
139
|
+
};
|
|
140
|
+
CheckboxGroup.defaultProps = {
|
|
141
|
+
className: "",
|
|
142
|
+
options: null,
|
|
143
|
+
onChange: null,
|
|
144
|
+
value: null,
|
|
145
|
+
defaultValue: null,
|
|
146
|
+
disabled: false,
|
|
147
|
+
name: "",
|
|
148
|
+
children: null
|
|
149
|
+
};
|
|
150
|
+
export default CheckboxGroup;
|
package/dist/DatePicker.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
1
|
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
3
2
|
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
|
|
3
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
4
4
|
|
|
5
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; }
|
|
6
6
|
|
|
@@ -21,15 +21,43 @@ import { margin200, padding300 } from "@happeouikit/layout";
|
|
|
21
21
|
import { IconButton, ButtonSecondary } from "@happeouikit/buttons";
|
|
22
22
|
import { IconChevronLeft, IconChevronRight, IconCalendar, IconCheck } from "@happeouikit/icons";
|
|
23
23
|
import { radius500, shadow500 } from "@happeouikit/theme";
|
|
24
|
+
import { components } from "react-select";
|
|
24
25
|
import Dropdown from "./Dropdown"; // CSS
|
|
25
26
|
|
|
26
27
|
import "react-day-picker/lib/style.css";
|
|
27
28
|
import messages from "./messages";
|
|
28
|
-
import {
|
|
29
|
+
import { useDevWarningOnce } from "./utils";
|
|
29
30
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
30
31
|
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
31
32
|
import { Fragment as _Fragment } from "react/jsx-runtime";
|
|
32
33
|
|
|
34
|
+
var formatValue = function formatValue(value, locale) {
|
|
35
|
+
if (!value) return null;
|
|
36
|
+
return moment(value).locale(locale).format("L");
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
var CustomInput = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
40
|
+
return /*#__PURE__*/_jsxs("div", {
|
|
41
|
+
style: {
|
|
42
|
+
display: "flex",
|
|
43
|
+
alignItems: "center"
|
|
44
|
+
},
|
|
45
|
+
children: [/*#__PURE__*/_jsx(IconCalendar, {
|
|
46
|
+
style: {
|
|
47
|
+
position: "absolute",
|
|
48
|
+
paddingLeft: "10px",
|
|
49
|
+
fill: gray03,
|
|
50
|
+
height: "20px",
|
|
51
|
+
width: "20px",
|
|
52
|
+
boxSizing: "content-box"
|
|
53
|
+
}
|
|
54
|
+
}), /*#__PURE__*/_jsx("input", _objectSpread({
|
|
55
|
+
"data-testid": "datepicker-input",
|
|
56
|
+
ref: ref
|
|
57
|
+
}, props))]
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
33
61
|
var DatePicker = function DatePicker(_ref) {
|
|
34
62
|
var label = _ref.label,
|
|
35
63
|
locale = _ref.locale,
|
|
@@ -47,21 +75,21 @@ var DatePicker = function DatePicker(_ref) {
|
|
|
47
75
|
month = _useState2[0],
|
|
48
76
|
setMonth = _useState2[1];
|
|
49
77
|
|
|
78
|
+
var from = date.from,
|
|
79
|
+
to = date.to;
|
|
80
|
+
var formattedValue = formatValue(value || date[type], locale);
|
|
81
|
+
var isRangeMode = Boolean(from) && Boolean(to);
|
|
82
|
+
useDevWarningOnce(value && date[type], "[DatePicker] Both the `value` and `date` props have been used.\n The `date` prop will be ignored. To get rid of this warning, remove one of these props.");
|
|
83
|
+
|
|
50
84
|
var onChangeMonthYear = function onChangeMonthYear(_ref2) {
|
|
51
85
|
var month = _ref2.month,
|
|
52
86
|
year = _ref2.year;
|
|
53
87
|
setMonth(new Date(year, month));
|
|
54
88
|
};
|
|
55
89
|
|
|
56
|
-
if (!date.to || date.to === "") {
|
|
57
|
-
date.to = value || "";
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
var to = date.to,
|
|
61
|
-
from = date.from;
|
|
62
90
|
return /*#__PURE__*/_jsxs(Wrapper, {
|
|
63
91
|
width: width,
|
|
64
|
-
hasRange:
|
|
92
|
+
hasRange: isRangeMode,
|
|
65
93
|
children: [label && /*#__PURE__*/_jsx("div", {
|
|
66
94
|
style: {
|
|
67
95
|
margin: "4px 0"
|
|
@@ -71,30 +99,26 @@ var DatePicker = function DatePicker(_ref) {
|
|
|
71
99
|
children: label
|
|
72
100
|
})
|
|
73
101
|
}), /*#__PURE__*/_jsx(DayPickerInput, _objectSpread({
|
|
74
|
-
component:
|
|
75
|
-
|
|
76
|
-
ref: ref
|
|
77
|
-
}, props));
|
|
78
|
-
}),
|
|
79
|
-
value: date[type] && moment(date[type]).locale(locale).format("L"),
|
|
102
|
+
component: CustomInput,
|
|
103
|
+
value: formattedValue,
|
|
80
104
|
formatDate: formatDate,
|
|
81
105
|
parseDate: parseDate,
|
|
82
|
-
placeholder:
|
|
106
|
+
placeholder: formatValue(new Date(), locale),
|
|
83
107
|
dayPickerProps: {
|
|
84
108
|
locale: locale,
|
|
85
109
|
localeUtils: MomentLocaleUtils,
|
|
86
110
|
className: "Range",
|
|
87
|
-
disabledDays:
|
|
111
|
+
disabledDays: isRangeMode ? {
|
|
88
112
|
before: from
|
|
89
113
|
} : disabledDays,
|
|
90
114
|
modifiers: {
|
|
91
115
|
before: from,
|
|
92
116
|
after: to
|
|
93
117
|
},
|
|
94
|
-
selectedDays:
|
|
118
|
+
selectedDays: isRangeMode ? {
|
|
95
119
|
from: from,
|
|
96
120
|
to: to
|
|
97
|
-
}
|
|
121
|
+
} : undefined,
|
|
98
122
|
navbarElement: /*#__PURE__*/_jsx(CustomNavbar, {
|
|
99
123
|
onChangeMonthYear: onChangeMonthYear,
|
|
100
124
|
disabledDays: disabledDays
|
|
@@ -114,9 +138,15 @@ DatePicker.defaultProps = {
|
|
|
114
138
|
from: "",
|
|
115
139
|
to: ""
|
|
116
140
|
},
|
|
117
|
-
locale: "en-gb"
|
|
141
|
+
locale: "en-gb",
|
|
142
|
+
label: "",
|
|
143
|
+
width: undefined,
|
|
144
|
+
value: undefined,
|
|
145
|
+
pickerRef: undefined,
|
|
146
|
+
disabledDays: []
|
|
118
147
|
};
|
|
119
148
|
DatePicker.propTypes = {
|
|
149
|
+
fromMonth: PropTypes.instanceOf(Date),
|
|
120
150
|
label: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
|
|
121
151
|
locale: PropTypes.string,
|
|
122
152
|
width: PropTypes.string,
|
|
@@ -136,13 +166,18 @@ DatePicker.propTypes = {
|
|
|
136
166
|
if (typeof from === "undefined" || typeof to === "undefined") {
|
|
137
167
|
return new Error("Date prop required. Format: { from: Date|number|string, to: Date|number|string }. ");
|
|
138
168
|
}
|
|
169
|
+
|
|
170
|
+
return null;
|
|
139
171
|
},
|
|
140
172
|
|
|
141
173
|
/** Can be used to disable range of dates. Also restricts the year selector. Example: { after: new Date(2025, 0, 0), before: new Date(2020, 0, 0) } */
|
|
142
|
-
disabledDays: PropTypes.object,
|
|
174
|
+
disabledDays: PropTypes.oneOfType([PropTypes.object, PropTypes.arrayOf(PropTypes.object)]),
|
|
143
175
|
|
|
144
176
|
/** Can be used to tap a ref to the picker. See Range select code example how to utilise. */
|
|
145
|
-
pickerRef: PropTypes.
|
|
177
|
+
pickerRef: PropTypes.oneOfType([PropTypes.func, // eslint-disable-next-line react/forbid-prop-types
|
|
178
|
+
PropTypes.shape({
|
|
179
|
+
current: PropTypes.any
|
|
180
|
+
})])
|
|
146
181
|
};
|
|
147
182
|
|
|
148
183
|
var CustomNavbar = function CustomNavbar(_ref3) {
|
|
@@ -207,27 +242,6 @@ var CustomNavbar = function CustomNavbar(_ref3) {
|
|
|
207
242
|
});
|
|
208
243
|
};
|
|
209
244
|
|
|
210
|
-
var CustomInput = /*#__PURE__*/React.forwardRef(function (props, ref) {
|
|
211
|
-
return /*#__PURE__*/_jsxs("div", {
|
|
212
|
-
style: {
|
|
213
|
-
display: "flex",
|
|
214
|
-
alignItems: "center"
|
|
215
|
-
},
|
|
216
|
-
children: [/*#__PURE__*/_jsx(IconCalendar, {
|
|
217
|
-
style: {
|
|
218
|
-
position: "absolute",
|
|
219
|
-
paddingLeft: "10px",
|
|
220
|
-
fill: gray03,
|
|
221
|
-
height: "20px",
|
|
222
|
-
width: "20px",
|
|
223
|
-
boxSizing: "content-box"
|
|
224
|
-
}
|
|
225
|
-
}), /*#__PURE__*/_jsx("input", _objectSpread({
|
|
226
|
-
ref: ref
|
|
227
|
-
}, props))]
|
|
228
|
-
});
|
|
229
|
-
});
|
|
230
|
-
|
|
231
245
|
var MonthPicker = function MonthPicker(_ref4) {
|
|
232
246
|
var date = _ref4.date,
|
|
233
247
|
disabledDays = _ref4.disabledDays,
|
|
@@ -253,7 +267,7 @@ var MonthPicker = function MonthPicker(_ref4) {
|
|
|
253
267
|
|
|
254
268
|
var fromYear = before ? new Date(before).getFullYear() + 1 : 1939;
|
|
255
269
|
var toYear = after ? new Date(after).getFullYear() : new Date(currentYear + 10, 11).getFullYear();
|
|
256
|
-
var years = [];
|
|
270
|
+
var years = []; // eslint-disable-next-line no-plusplus
|
|
257
271
|
|
|
258
272
|
for (var i = toYear; i >= fromYear; i--) {
|
|
259
273
|
years.push({
|
|
@@ -275,6 +289,9 @@ var MonthPicker = function MonthPicker(_ref4) {
|
|
|
275
289
|
onMouseDown: function onMouseDown(e) {
|
|
276
290
|
return e.stopPropagation();
|
|
277
291
|
},
|
|
292
|
+
role: "option",
|
|
293
|
+
"aria-selected": true,
|
|
294
|
+
tabIndex: "0",
|
|
278
295
|
children: /*#__PURE__*/_jsx(components.Option, _objectSpread({}, props, {
|
|
279
296
|
children: children
|
|
280
297
|
}))
|
|
@@ -364,16 +381,16 @@ var Wrapper = styled.div.withConfig({
|
|
|
364
381
|
return width || "200px";
|
|
365
382
|
}, shadow500, radius500, function (_ref9) {
|
|
366
383
|
var hasRange = _ref9.hasRange;
|
|
367
|
-
return hasRange && "
|
|
384
|
+
return hasRange && css(["background-color:", ";border-radius:0;&:not(.DayPicker-Day--outside){color:", ";}"], toBgLight(active), gray01);
|
|
368
385
|
}, active, white, function (_ref10) {
|
|
369
386
|
var hasRange = _ref10.hasRange;
|
|
370
|
-
return hasRange && "
|
|
387
|
+
return hasRange && css(["::before{content:\"\";position:absolute;background-color:", ";width:10px;height:35px;top:0;right:0;z-index:-1;}"], toBgLight(active));
|
|
371
388
|
}, function (_ref11) {
|
|
372
389
|
var hasRange = _ref11.hasRange;
|
|
373
|
-
return hasRange && "
|
|
390
|
+
return hasRange && css(["::before{content:\"\";position:absolute;background-color:", ";width:10px;height:35px;top:0;left:0;z-index:-1;}"], toBgLight(active));
|
|
374
391
|
}, active, white, sansFamily, gray06, black, gray06, active, active, sansFamily, gray01, function (_ref12) {
|
|
375
392
|
var hasRange = _ref12.hasRange;
|
|
376
|
-
return hasRange && "
|
|
393
|
+
return hasRange && css([".DayPicker-Day--selected:not(.DayPicker-Day--before):not(.DayPicker-Day--after){&:first-child{border-top-left-radius:", " !important;border-bottom-left-radius:", " !important;}&:last-child{border-top-right-radius:", " !important;border-bottom-right-radius:", " !important;}}"], borderRadius, borderRadius, borderRadius, borderRadius);
|
|
377
394
|
});
|
|
378
395
|
var NavBarContainer = styled.div.withConfig({
|
|
379
396
|
displayName: "DatePicker__NavBarContainer",
|
package/dist/DateTimePicker.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
/* eslint-disable radix */
|
|
4
|
+
import React, { useState, useCallback } from "react";
|
|
3
5
|
import styled from "styled-components";
|
|
4
6
|
import moment from "moment";
|
|
5
7
|
import debounce from "lodash.debounce";
|
|
@@ -33,8 +35,8 @@ var DateTimePicker = function DateTimePicker(_ref) {
|
|
|
33
35
|
|
|
34
36
|
var _useState5 = useState(inputMode),
|
|
35
37
|
_useState6 = _slicedToArray(_useState5, 2),
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
selectedMode = _useState6[0],
|
|
39
|
+
setSelectedMode = _useState6[1];
|
|
38
40
|
|
|
39
41
|
var _useState7 = useState(DEFAULT_INPUT_STATE),
|
|
40
42
|
_useState8 = _slicedToArray(_useState7, 2),
|
|
@@ -51,25 +53,11 @@ var DateTimePicker = function DateTimePicker(_ref) {
|
|
|
51
53
|
timeValue = _useState12[0],
|
|
52
54
|
setTimeValue = _useState12[1];
|
|
53
55
|
|
|
54
|
-
var
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
var handleTimeSelected = function handleTimeSelected(time) {
|
|
60
|
-
if (time.length !== 2) {
|
|
61
|
-
setTimeInputState("error");
|
|
62
|
-
setTimeInputErrorMessage(intl.formatMessage(messages.wrongFormat));
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
setSelectedTime(time);
|
|
67
|
-
setDateTime();
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
var validateTime = function validateTime(time) {
|
|
56
|
+
var validateTime = useCallback(function (_ref2) {
|
|
57
|
+
var time = _ref2.time,
|
|
58
|
+
mode = _ref2.mode;
|
|
71
59
|
var hour = parseInt(time[0]);
|
|
72
|
-
var minute = parseInt(time[1]);
|
|
60
|
+
var minute = parseInt(time[1]); // eslint-disable-next-line no-restricted-globals
|
|
73
61
|
|
|
74
62
|
if (isNaN(hour) || isNaN(minute)) {
|
|
75
63
|
setTimeInputState("error");
|
|
@@ -77,7 +65,10 @@ var DateTimePicker = function DateTimePicker(_ref) {
|
|
|
77
65
|
return false;
|
|
78
66
|
}
|
|
79
67
|
|
|
80
|
-
|
|
68
|
+
var regex = new RegExp(mode === "24h" ? "([0-1][0-9]{1}|[2][0-3]{1}):([0-5]{1}[0-9]{1})" : "([0][1-9]|[1][0-2]{1}):([0-5]{1}[0-9]{1})");
|
|
69
|
+
var timeValue = hour < 10 ? "0".concat(hour, ":").concat(time[1]) : "".concat(hour, ":").concat(time[1]);
|
|
70
|
+
|
|
71
|
+
if (!regex.test(timeValue)) {
|
|
81
72
|
setTimeInputState("error");
|
|
82
73
|
setTimeInputErrorMessage(intl.formatMessage(messages.invalidTime));
|
|
83
74
|
return false;
|
|
@@ -85,36 +76,20 @@ var DateTimePicker = function DateTimePicker(_ref) {
|
|
|
85
76
|
|
|
86
77
|
setTimeInputState(DEFAULT_INPUT_STATE);
|
|
87
78
|
return true;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
var
|
|
100
|
-
var
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (timeString.match(TIME_REGEX)) {
|
|
104
|
-
newTime = "".concat(timeString.slice(0, 2), ":").concat(timeString.slice(2));
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
setTimeValue(newTime);
|
|
108
|
-
handleTimeSelectedDebounced(newTime.split(":"));
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
var setDateTime = function setDateTime() {
|
|
112
|
-
if (!selectedTime || !mode) return;
|
|
113
|
-
if (!validateTime(selectedTime)) return;
|
|
114
|
-
if (!selectedDate) return;
|
|
115
|
-
var hour = parseInt(selectedTime[0]);
|
|
116
|
-
var minute = parseInt(selectedTime[1]);
|
|
117
|
-
var selectedDateTime = moment(selectedDate);
|
|
79
|
+
}, [intl]);
|
|
80
|
+
var setDateTime = useCallback(function (_ref3) {
|
|
81
|
+
var date = _ref3.date,
|
|
82
|
+
time = _ref3.time,
|
|
83
|
+
mode = _ref3.mode;
|
|
84
|
+
if (!time || !mode) return;
|
|
85
|
+
if (!validateTime({
|
|
86
|
+
time: time,
|
|
87
|
+
mode: mode
|
|
88
|
+
})) return;
|
|
89
|
+
if (!date) return;
|
|
90
|
+
var hour = parseInt(time[0]);
|
|
91
|
+
var minute = parseInt(time[1]);
|
|
92
|
+
var selectedDateTime = moment(date);
|
|
118
93
|
|
|
119
94
|
if (mode === "am" && hour === 12) {
|
|
120
95
|
selectedDateTime.hour(0);
|
|
@@ -128,8 +103,56 @@ var DateTimePicker = function DateTimePicker(_ref) {
|
|
|
128
103
|
|
|
129
104
|
selectedDateTime.minute(minute);
|
|
130
105
|
if (onChange) onChange(selectedDateTime.toDate());
|
|
131
|
-
};
|
|
106
|
+
}, [onChange, validateTime]);
|
|
107
|
+
var handleDateSelected = useCallback(function (date) {
|
|
108
|
+
setSelectedDate(date);
|
|
109
|
+
setDateTime({
|
|
110
|
+
date: date,
|
|
111
|
+
time: selectedTime,
|
|
112
|
+
mode: selectedMode
|
|
113
|
+
});
|
|
114
|
+
}, [selectedMode, selectedTime, setDateTime]);
|
|
115
|
+
var handleOnKeyPress = useCallback(function (event) {
|
|
116
|
+
if (event.charCode < 48 || event.charCode > 58) {
|
|
117
|
+
event.preventDefault();
|
|
118
|
+
}
|
|
119
|
+
}, []);
|
|
120
|
+
var handleTimeSelected = useCallback(function (time) {
|
|
121
|
+
if (time.length !== 2) {
|
|
122
|
+
setTimeInputState("error");
|
|
123
|
+
setTimeInputErrorMessage(intl.formatMessage(messages.wrongFormat));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
132
126
|
|
|
127
|
+
setSelectedTime(time);
|
|
128
|
+
setDateTime({
|
|
129
|
+
date: selectedDate,
|
|
130
|
+
time: time,
|
|
131
|
+
mode: selectedMode
|
|
132
|
+
});
|
|
133
|
+
}, [intl, selectedDate, selectedMode, setDateTime]);
|
|
134
|
+
var onChangeMode = useCallback(function (_ref4) {
|
|
135
|
+
var value = _ref4.value;
|
|
136
|
+
setSelectedMode(value);
|
|
137
|
+
setDateTime({
|
|
138
|
+
date: selectedDate,
|
|
139
|
+
time: selectedTime,
|
|
140
|
+
mode: value
|
|
141
|
+
});
|
|
142
|
+
}, [selectedDate, selectedTime, setDateTime]);
|
|
143
|
+
var handleTimeSelectedDebounced = debounce(handleTimeSelected, 500);
|
|
144
|
+
var onChangeTime = useCallback(function (event) {
|
|
145
|
+
var timeString = event.target.value;
|
|
146
|
+
var newTime = timeString;
|
|
147
|
+
setTimeInputState(DEFAULT_INPUT_STATE);
|
|
148
|
+
|
|
149
|
+
if (timeString.match(TIME_REGEX)) {
|
|
150
|
+
newTime = "".concat(timeString.slice(0, 2), ":").concat(timeString.slice(2));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
setTimeValue(newTime);
|
|
154
|
+
handleTimeSelectedDebounced(newTime.split(":"));
|
|
155
|
+
}, [handleTimeSelectedDebounced]);
|
|
133
156
|
return /*#__PURE__*/_jsxs(Wrapper, {
|
|
134
157
|
children: [/*#__PURE__*/_jsx(DatePicker, {
|
|
135
158
|
width: "100%",
|
|
@@ -142,7 +165,8 @@ var DateTimePicker = function DateTimePicker(_ref) {
|
|
|
142
165
|
onChange: onChangeTime,
|
|
143
166
|
state: timeInputState,
|
|
144
167
|
errorMessage: timeInputErrorMessage,
|
|
145
|
-
value: timeValue
|
|
168
|
+
value: timeValue,
|
|
169
|
+
onKeyPress: handleOnKeyPress
|
|
146
170
|
}), /*#__PURE__*/_jsx("div", {
|
|
147
171
|
style: {
|
|
148
172
|
paddingTop: "28px"
|
|
@@ -175,9 +199,10 @@ var Wrapper = styled.div.withConfig({
|
|
|
175
199
|
DateTimePicker.propTypes = {
|
|
176
200
|
onChange: PropTypes.func.isRequired,
|
|
177
201
|
label: PropTypes.string,
|
|
178
|
-
|
|
202
|
+
inputMode: PropTypes.oneOf(["am", "pm", "24h"])
|
|
179
203
|
};
|
|
180
204
|
DateTimePicker.defaultProps = {
|
|
181
|
-
inputMode: "am"
|
|
205
|
+
inputMode: "am",
|
|
206
|
+
label: undefined
|
|
182
207
|
};
|
|
183
208
|
export default DateTimePicker;
|
|
@@ -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
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -3,10 +3,12 @@ export { default as Dropdown, dropdownStyles } from "./Dropdown";
|
|
|
3
3
|
export * from "./Link";
|
|
4
4
|
export { default as Switch } from "./Switch";
|
|
5
5
|
export { default as Checkbox } from "./Checkbox";
|
|
6
|
+
export { default as CheckboxGroup } from "./CheckboxGroup";
|
|
6
7
|
export { default as RadioButton } from "./RadioButton";
|
|
7
8
|
export { default as Textarea } from "./Textarea";
|
|
8
9
|
export { default as Tag } from "./Tag";
|
|
9
10
|
export { default as Badge } from "./Badge";
|
|
10
11
|
export { default as DatePicker } from "./DatePicker";
|
|
11
12
|
export * from "./utils";
|
|
12
|
-
export { default as DateTimePicker } from "./DateTimePicker";
|
|
13
|
+
export { default as DateTimePicker } from "./DateTimePicker";
|
|
14
|
+
export { default as Typeahead } from "./Typeahead/Typeahead";
|
package/dist/utils.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import React, { useEffect } from "react";
|
|
1
2
|
export var toCamelCase = function toCamelCase(str) {
|
|
2
3
|
return str.replace(/(_|-|,|\.)(.)/g, function (match) {
|
|
3
4
|
return match[1].toUpperCase();
|
|
@@ -24,4 +25,24 @@ export var sanitiseHashtag = function sanitiseHashtag(hashtag) {
|
|
|
24
25
|
export var addHashSign = function addHashSign(input) {
|
|
25
26
|
if (input.startsWith("#")) return input;
|
|
26
27
|
return "#" + input;
|
|
28
|
+
};
|
|
29
|
+
export var isString = function isString(value) {
|
|
30
|
+
return typeof value === "string";
|
|
31
|
+
};
|
|
32
|
+
export var getValidChildrenArray = function getValidChildrenArray(children) {
|
|
33
|
+
return React.Children.toArray(children).filter(React.isValidElement);
|
|
34
|
+
};
|
|
35
|
+
export var devWarning = function devWarning(message) {
|
|
36
|
+
if (process.env.NODE_ENV !== "production") {
|
|
37
|
+
// eslint-disable-next-line no-console
|
|
38
|
+
console.warn("HappeoUiKit/form-elements: ".concat(message));
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
export var useDevWarningOnce = function useDevWarningOnce(shouldWarn, message) {
|
|
42
|
+
useEffect(function () {
|
|
43
|
+
if (shouldWarn) {
|
|
44
|
+
devWarning(message);
|
|
45
|
+
} // eslint-disable-next-line
|
|
46
|
+
|
|
47
|
+
}, []);
|
|
27
48
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@happeouikit/form-elements",
|
|
3
|
-
"version": "2.0
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "src/index.js",
|
|
@@ -15,10 +15,12 @@
|
|
|
15
15
|
"author": "Happeo Oy",
|
|
16
16
|
"license": "MIT",
|
|
17
17
|
"dependencies": {
|
|
18
|
+
"@happeo/react-core": "^0.9.0",
|
|
18
19
|
"@happeouikit/buttons": "^1.0.15",
|
|
19
20
|
"@happeouikit/colors": "^1.0.4",
|
|
20
|
-
"@happeouikit/icons": "^2.0.
|
|
21
|
+
"@happeouikit/icons": "^2.0.5",
|
|
21
22
|
"@happeouikit/layout": "^1.1.11",
|
|
23
|
+
"@happeouikit/loaders": "^1.0.4",
|
|
22
24
|
"@happeouikit/theme": "^1.0.4",
|
|
23
25
|
"@happeouikit/tooltip": "^1.0.11",
|
|
24
26
|
"@happeouikit/typography": "^1.0.10",
|
|
@@ -34,6 +36,8 @@
|
|
|
34
36
|
"@babel/plugin-proposal-unicode-property-regex": "^7.16.0"
|
|
35
37
|
},
|
|
36
38
|
"peerDependencies": {
|
|
39
|
+
"@testing-library/jest-dom": "^5.14.1",
|
|
40
|
+
"@testing-library/react": "^10.2.1",
|
|
37
41
|
"prop-types": "^15.7.2",
|
|
38
42
|
"react": "^17.0.2",
|
|
39
43
|
"styled-components": "^5.3.3"
|