@automattic/vip-design-system 0.25.0 → 0.25.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/build/system/NewForm/FormAutocomplete.css +166 -0
- package/build/system/NewForm/FormAutocomplete.js +23 -24
- package/build/system/NewForm/FormAutocomplete.stories.js +12 -40
- package/package.json +2 -2
- package/src/system/NewForm/FormAutocomplete.js +16 -12
- package/src/system/NewForm/FormAutocomplete.stories.jsx +13 -39
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
.autocomplete__wrapper {
|
|
2
|
+
position: relative;
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
.autocomplete__hint,
|
|
6
|
+
.autocomplete__input {
|
|
7
|
+
-webkit-appearance: none;
|
|
8
|
+
border: 2px solid #0b0c0c;
|
|
9
|
+
border-radius: 0; /* Safari 10 on iOS adds implicit border rounding. */
|
|
10
|
+
box-sizing: border-box;
|
|
11
|
+
-moz-box-sizing: border-box;
|
|
12
|
+
-webkit-box-sizing: border-box;
|
|
13
|
+
margin-bottom: 0; /* BUG: Safari 10 on macOS seems to add an implicit margin. */
|
|
14
|
+
width: 100%;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.autocomplete__input {
|
|
18
|
+
background-color: transparent;
|
|
19
|
+
position: relative;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
.autocomplete__hint {
|
|
23
|
+
color: #b1b4b6;
|
|
24
|
+
position: absolute;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
.autocomplete__input--default {
|
|
28
|
+
padding: 5px;
|
|
29
|
+
}
|
|
30
|
+
.autocomplete__input--focused {
|
|
31
|
+
outline: 3px solid #fd0;
|
|
32
|
+
outline-offset: 0;
|
|
33
|
+
box-shadow: inset 0 0 0 2px;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
.autocomplete__input--show-all-values {
|
|
37
|
+
padding: 5px 34px 5px 5px; /* Space for arrow. Other padding should match .autocomplete__input--default. */
|
|
38
|
+
cursor: pointer;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
.autocomplete__dropdown-arrow-down {
|
|
42
|
+
z-index: -1;
|
|
43
|
+
display: inline-block;
|
|
44
|
+
position: absolute;
|
|
45
|
+
right: 8px;
|
|
46
|
+
width: 24px;
|
|
47
|
+
height: 24px;
|
|
48
|
+
top: 10px;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.autocomplete__menu {
|
|
52
|
+
background-color: #fff;
|
|
53
|
+
border: 2px solid #0B0C0C;
|
|
54
|
+
border-top: 0;
|
|
55
|
+
color: #0B0C0C;
|
|
56
|
+
margin: 0;
|
|
57
|
+
max-height: 342px;
|
|
58
|
+
overflow-x: hidden;
|
|
59
|
+
padding: 0;
|
|
60
|
+
width: 100%;
|
|
61
|
+
width: calc(100% - 4px);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
.autocomplete__menu--visible {
|
|
65
|
+
display: block;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.autocomplete__menu--hidden {
|
|
69
|
+
display: none;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
.autocomplete__menu--overlay {
|
|
73
|
+
box-shadow: rgba(0, 0, 0, 0.256863) 0px 2px 6px;
|
|
74
|
+
left: 0;
|
|
75
|
+
position: absolute;
|
|
76
|
+
top: 100%;
|
|
77
|
+
z-index: 100;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
.autocomplete__menu--inline {
|
|
81
|
+
position: relative;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.autocomplete__option {
|
|
85
|
+
border-bottom: solid #b1b4b6;
|
|
86
|
+
border-width: 1px 0;
|
|
87
|
+
cursor: pointer;
|
|
88
|
+
display: block;
|
|
89
|
+
position: relative;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.autocomplete__option > * {
|
|
93
|
+
pointer-events: none;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.autocomplete__option:first-of-type {
|
|
97
|
+
border-top-width: 0;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.autocomplete__option:last-of-type {
|
|
101
|
+
border-bottom-width: 0;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
.autocomplete__option--odd {
|
|
105
|
+
background-color: #FAFAFA;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
.autocomplete__option--focused,
|
|
109
|
+
.autocomplete__option:hover {
|
|
110
|
+
background-color: #1d70b8;
|
|
111
|
+
border-color: #1d70b8;
|
|
112
|
+
color: white;
|
|
113
|
+
outline: none;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
@media (-ms-high-contrast: active), (forced-colors: active) {
|
|
117
|
+
.autocomplete__menu {
|
|
118
|
+
border-color: FieldText;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
.autocomplete__option {
|
|
122
|
+
background-color: Field;
|
|
123
|
+
color: FieldText;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
.autocomplete__option--focused,
|
|
127
|
+
.autocomplete__option:hover {
|
|
128
|
+
forced-color-adjust: none; /* prevent backplate from obscuring text */
|
|
129
|
+
background-color: Highlight;
|
|
130
|
+
border-color: Highlight;
|
|
131
|
+
color: HighlightText;
|
|
132
|
+
|
|
133
|
+
/* Prefer SelectedItem / SelectedItemText in browsers that support it */
|
|
134
|
+
background-color: SelectedItem;
|
|
135
|
+
border-color: SelectedItem;
|
|
136
|
+
color: SelectedItemText;
|
|
137
|
+
outline-color: SelectedItemText;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
.autocomplete__option--no-results {
|
|
142
|
+
background-color: #FAFAFA;
|
|
143
|
+
color: #646b6f;
|
|
144
|
+
cursor: not-allowed;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
.autocomplete__hint,
|
|
148
|
+
.autocomplete__input,
|
|
149
|
+
.autocomplete__option {
|
|
150
|
+
font-size: 16px;
|
|
151
|
+
line-height: 1.25;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
.autocomplete__hint,
|
|
155
|
+
.autocomplete__option {
|
|
156
|
+
padding: 5px;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
@media (min-width: 641px) {
|
|
160
|
+
.autocomplete__hint,
|
|
161
|
+
.autocomplete__input,
|
|
162
|
+
.autocomplete__option {
|
|
163
|
+
font-size: 19px;
|
|
164
|
+
line-height: 1.31579;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
@@ -7,6 +7,8 @@ exports.FormAutocomplete = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
9
9
|
|
|
10
|
+
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
|
11
|
+
|
|
10
12
|
var _react = _interopRequireWildcard(require("react"));
|
|
11
13
|
|
|
12
14
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
@@ -25,19 +27,12 @@ var _FormSelectArrow = require("./FormSelectArrow");
|
|
|
25
27
|
|
|
26
28
|
var _jsxRuntime = require("theme-ui/jsx-runtime");
|
|
27
29
|
|
|
30
|
+
var _excluded = ["isInline", "forLabel", "options", "label", "getOptionLabel", "getOptionValue", "onChange", "value", "showAllValues", "displayMenu", "id"];
|
|
31
|
+
|
|
28
32
|
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); }
|
|
29
33
|
|
|
30
34
|
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; }
|
|
31
35
|
|
|
32
|
-
/** @jsxImportSource theme-ui */
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* External dependencies
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Internal dependencies
|
|
40
|
-
*/
|
|
41
36
|
var defaultStyles = {
|
|
42
37
|
width: '100%',
|
|
43
38
|
py: 0,
|
|
@@ -114,15 +109,18 @@ var FormAutocomplete = /*#__PURE__*/_react["default"].forwardRef(function (_ref,
|
|
|
114
109
|
forLabel = _ref.forLabel,
|
|
115
110
|
options = _ref.options,
|
|
116
111
|
label = _ref.label,
|
|
112
|
+
getOptionLabel = _ref.getOptionLabel,
|
|
117
113
|
getOptionValue = _ref.getOptionValue,
|
|
118
|
-
onChange = _ref.onChange,
|
|
114
|
+
_ref$onChange = _ref.onChange,
|
|
115
|
+
onChange = _ref$onChange === void 0 ? function () {} : _ref$onChange,
|
|
119
116
|
value = _ref.value,
|
|
120
117
|
_ref$showAllValues = _ref.showAllValues,
|
|
121
118
|
showAllValues = _ref$showAllValues === void 0 ? true : _ref$showAllValues,
|
|
122
119
|
_ref$displayMenu = _ref.displayMenu,
|
|
123
120
|
displayMenu = _ref$displayMenu === void 0 ? 'overlay' : _ref$displayMenu,
|
|
124
121
|
_ref$id = _ref.id,
|
|
125
|
-
id = _ref$id === void 0 ? 'vip-autocomplete' : _ref$id
|
|
122
|
+
id = _ref$id === void 0 ? 'vip-autocomplete' : _ref$id,
|
|
123
|
+
props = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
|
|
126
124
|
|
|
127
125
|
var SelectLabel = function SelectLabel() {
|
|
128
126
|
return (0, _jsxRuntime.jsx)(_Label.Label, {
|
|
@@ -132,9 +130,9 @@ var FormAutocomplete = /*#__PURE__*/_react["default"].forwardRef(function (_ref,
|
|
|
132
130
|
};
|
|
133
131
|
|
|
134
132
|
var inlineLabel = !!(isInline && label);
|
|
135
|
-
var
|
|
136
|
-
return
|
|
137
|
-
}, [
|
|
133
|
+
var optionLabel = (0, _react.useCallback)(function (option) {
|
|
134
|
+
return getOptionLabel ? getOptionLabel(option) : option.label;
|
|
135
|
+
}, [getOptionLabel]);
|
|
138
136
|
var getAllOptions = (0, _react.useMemo)(function () {
|
|
139
137
|
return [].concat(options.filter(function (option) {
|
|
140
138
|
return !option.options;
|
|
@@ -146,22 +144,22 @@ var FormAutocomplete = /*#__PURE__*/_react["default"].forwardRef(function (_ref,
|
|
|
146
144
|
return a.concat(b);
|
|
147
145
|
}, []);
|
|
148
146
|
}, [options]);
|
|
149
|
-
var
|
|
147
|
+
var getOptionByLabel = (0, _react.useCallback)(function (inputValue) {
|
|
150
148
|
return getAllOptions.find(function (option) {
|
|
151
|
-
return "" +
|
|
149
|
+
return "" + optionLabel(option) === "" + inputValue;
|
|
152
150
|
});
|
|
153
|
-
}, [getAllOptions,
|
|
151
|
+
}, [getAllOptions, optionLabel]);
|
|
154
152
|
var onValueChange = (0, _react.useCallback)(function (inputValue) {
|
|
155
|
-
if (
|
|
156
|
-
onChange(
|
|
153
|
+
if (inputValue) {
|
|
154
|
+
onChange(getOptionByLabel(inputValue), inputValue);
|
|
157
155
|
}
|
|
158
|
-
}, [onChange,
|
|
156
|
+
}, [onChange, getOptionByLabel]);
|
|
159
157
|
var suggest = (0, _react.useCallback)(function (query, populateResults) {
|
|
160
158
|
var data = options.filter(function (option) {
|
|
161
|
-
return option.
|
|
159
|
+
return optionLabel(option).toLowerCase().indexOf(query.toLowerCase()) >= 0;
|
|
162
160
|
});
|
|
163
161
|
populateResults(data.map(function (option) {
|
|
164
|
-
return option
|
|
162
|
+
return optionLabel(option);
|
|
165
163
|
}));
|
|
166
164
|
}, [options]);
|
|
167
165
|
(0, _react.useEffect)(function () {
|
|
@@ -176,7 +174,7 @@ var FormAutocomplete = /*#__PURE__*/_react["default"].forwardRef(function (_ref,
|
|
|
176
174
|
children: (0, _jsxRuntime.jsxs)(_FormSelectContent.FormSelectContent, {
|
|
177
175
|
isInline: inlineLabel,
|
|
178
176
|
label: inlineLabel ? (0, _jsxRuntime.jsx)(SelectLabel, {}) : null,
|
|
179
|
-
children: [(0, _jsxRuntime.jsx)(_react2["default"], {
|
|
177
|
+
children: [(0, _jsxRuntime.jsx)(_react2["default"], (0, _extends2["default"])({
|
|
180
178
|
id: id,
|
|
181
179
|
showAllValues: showAllValues,
|
|
182
180
|
ref: forwardRef,
|
|
@@ -184,7 +182,7 @@ var FormAutocomplete = /*#__PURE__*/_react["default"].forwardRef(function (_ref,
|
|
|
184
182
|
defaultValue: value,
|
|
185
183
|
displayMenu: displayMenu,
|
|
186
184
|
onConfirm: onValueChange
|
|
187
|
-
}), (0, _jsxRuntime.jsx)(_FormSelectArrow.FormSelectArrow, {})]
|
|
185
|
+
}, props)), (0, _jsxRuntime.jsx)(_FormSelectArrow.FormSelectArrow, {})]
|
|
188
186
|
})
|
|
189
187
|
})]
|
|
190
188
|
});
|
|
@@ -200,6 +198,7 @@ FormAutocomplete.propTypes = {
|
|
|
200
198
|
displayMenu: _propTypes["default"].string,
|
|
201
199
|
label: _propTypes["default"].string,
|
|
202
200
|
options: _propTypes["default"].array,
|
|
201
|
+
getOptionLabel: _propTypes["default"].func,
|
|
203
202
|
getOptionValue: _propTypes["default"].func,
|
|
204
203
|
onChange: _propTypes["default"].func
|
|
205
204
|
};
|
|
@@ -9,13 +9,11 @@ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")
|
|
|
9
9
|
|
|
10
10
|
var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
|
|
11
11
|
|
|
12
|
-
var _react = require("react");
|
|
13
|
-
|
|
14
12
|
var Form = _interopRequireWildcard(require("."));
|
|
15
13
|
|
|
16
14
|
var _jsxRuntime = require("theme-ui/jsx-runtime");
|
|
17
15
|
|
|
18
|
-
var _excluded = ["label", "width"
|
|
16
|
+
var _excluded = ["label", "width"];
|
|
19
17
|
|
|
20
18
|
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); }
|
|
21
19
|
|
|
@@ -45,7 +43,7 @@ var _default = {
|
|
|
45
43
|
}
|
|
46
44
|
};
|
|
47
45
|
exports["default"] = _default;
|
|
48
|
-
var
|
|
46
|
+
var options = [{
|
|
49
47
|
value: 'chocolate',
|
|
50
48
|
label: 'Chocolate'
|
|
51
49
|
}, {
|
|
@@ -54,14 +52,17 @@ var defaultOptions = [{
|
|
|
54
52
|
}, {
|
|
55
53
|
value: 'vanilla',
|
|
56
54
|
label: 'Vanilla'
|
|
57
|
-
}];
|
|
55
|
+
}];
|
|
56
|
+
var args = {
|
|
57
|
+
label: 'Label',
|
|
58
|
+
options: options
|
|
59
|
+
}; // eslint-disable-next-line react/prop-types
|
|
58
60
|
|
|
59
61
|
var DefaultComponent = function DefaultComponent(_ref) {
|
|
60
62
|
var _ref$label = _ref.label,
|
|
61
63
|
label = _ref$label === void 0 ? 'Label' : _ref$label,
|
|
62
64
|
_ref$width = _ref.width,
|
|
63
65
|
width = _ref$width === void 0 ? 250 : _ref$width,
|
|
64
|
-
onChange = _ref.onChange,
|
|
65
66
|
rest = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
|
|
66
67
|
return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
|
|
67
68
|
children: (0, _jsxRuntime.jsx)(Form.Root, {
|
|
@@ -71,8 +72,7 @@ var DefaultComponent = function DefaultComponent(_ref) {
|
|
|
71
72
|
},
|
|
72
73
|
children: (0, _jsxRuntime.jsx)(Form.Autocomplete, (0, _extends2["default"])({
|
|
73
74
|
id: "form-autocomplete",
|
|
74
|
-
label: label
|
|
75
|
-
onChange: onChange
|
|
75
|
+
label: label
|
|
76
76
|
}, rest))
|
|
77
77
|
})
|
|
78
78
|
})
|
|
@@ -80,47 +80,19 @@ var DefaultComponent = function DefaultComponent(_ref) {
|
|
|
80
80
|
};
|
|
81
81
|
|
|
82
82
|
var Default = function Default() {
|
|
83
|
-
var _useState = (0, _react.useState)(defaultOptions),
|
|
84
|
-
options = _useState[0],
|
|
85
|
-
setOptions = _useState[1];
|
|
86
|
-
|
|
87
|
-
var onChange = (0, _react.useCallback)(function (value) {
|
|
88
|
-
setOptions(defaultOptions.filter(function (option) {
|
|
89
|
-
return !value || option.label.toLowerCase().indexOf(value.toLowerCase()) >= 0;
|
|
90
|
-
}));
|
|
91
|
-
});
|
|
92
|
-
var args = {
|
|
93
|
-
label: 'Label',
|
|
94
|
-
options: options
|
|
95
|
-
};
|
|
96
83
|
return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
|
|
97
|
-
children: (0, _jsxRuntime.jsx)(DefaultComponent, (0, _extends2["default"])({
|
|
98
|
-
onChange: onChange
|
|
99
|
-
}, args))
|
|
84
|
+
children: (0, _jsxRuntime.jsx)(DefaultComponent, (0, _extends2["default"])({}, args))
|
|
100
85
|
});
|
|
101
86
|
};
|
|
102
87
|
|
|
103
88
|
exports.Default = Default;
|
|
104
89
|
|
|
105
90
|
var Inline = function Inline() {
|
|
106
|
-
var
|
|
107
|
-
|
|
108
|
-
setOptions = _useState2[1];
|
|
109
|
-
|
|
110
|
-
var onChange = (0, _react.useCallback)(function (value) {
|
|
111
|
-
setOptions(defaultOptions.filter(function (option) {
|
|
112
|
-
return !value || option.label.toLowerCase().indexOf(value.toLowerCase()) >= 0;
|
|
113
|
-
}));
|
|
91
|
+
var customArgs = (0, _extends2["default"])({}, args, {
|
|
92
|
+
isInline: true
|
|
114
93
|
});
|
|
115
|
-
var args = {
|
|
116
|
-
label: 'Label',
|
|
117
|
-
options: options
|
|
118
|
-
};
|
|
119
94
|
return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
|
|
120
|
-
children: (0, _jsxRuntime.jsx)(DefaultComponent, (0, _extends2["default"])({
|
|
121
|
-
isInline: true,
|
|
122
|
-
onChange: onChange
|
|
123
|
-
}, args))
|
|
95
|
+
children: (0, _jsxRuntime.jsx)(DefaultComponent, (0, _extends2["default"])({}, customArgs))
|
|
124
96
|
});
|
|
125
97
|
};
|
|
126
98
|
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@automattic/vip-design-system",
|
|
3
|
-
"version": "0.25.
|
|
3
|
+
"version": "0.25.1",
|
|
4
4
|
"main": "build/system/index.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build-storybook": "build-storybook",
|
|
7
7
|
"prepare": "npm run build",
|
|
8
|
-
"build": "npm run theme-update && cross-env NODE_ENV=production babel src --out-dir build && npm run theme-builder-copy",
|
|
8
|
+
"build": "npm run theme-update && cross-env NODE_ENV=production babel src --out-dir build --copy-files && npm run theme-builder-copy",
|
|
9
9
|
"format": "prettier --write \"src/**/*.{js,ts,tsx,md,mdx,json}\"",
|
|
10
10
|
"format:check": "prettier --list-different -- \"src/system/**/*.{js,ts,tsx,md,mdx,json}\"",
|
|
11
11
|
"jest": "NODE_ENV=test jest --detectOpenHandles --env=jsdom",
|
|
@@ -74,12 +74,14 @@ const FormAutocomplete = React.forwardRef(
|
|
|
74
74
|
forLabel,
|
|
75
75
|
options,
|
|
76
76
|
label,
|
|
77
|
+
getOptionLabel,
|
|
77
78
|
getOptionValue,
|
|
78
|
-
onChange,
|
|
79
|
+
onChange = () => {},
|
|
79
80
|
value,
|
|
80
81
|
showAllValues = true,
|
|
81
82
|
displayMenu = 'overlay',
|
|
82
83
|
id = 'vip-autocomplete',
|
|
84
|
+
...props
|
|
83
85
|
},
|
|
84
86
|
forwardRef
|
|
85
87
|
) => {
|
|
@@ -87,9 +89,9 @@ const FormAutocomplete = React.forwardRef(
|
|
|
87
89
|
|
|
88
90
|
const inlineLabel = !! ( isInline && label );
|
|
89
91
|
|
|
90
|
-
const
|
|
91
|
-
option => (
|
|
92
|
-
[
|
|
92
|
+
const optionLabel = useCallback(
|
|
93
|
+
option => ( getOptionLabel ? getOptionLabel( option ) : option.label ),
|
|
94
|
+
[ getOptionLabel ]
|
|
93
95
|
);
|
|
94
96
|
|
|
95
97
|
const getAllOptions = useMemo(
|
|
@@ -101,27 +103,27 @@ const FormAutocomplete = React.forwardRef(
|
|
|
101
103
|
[ options ]
|
|
102
104
|
);
|
|
103
105
|
|
|
104
|
-
const
|
|
106
|
+
const getOptionByLabel = useCallback(
|
|
105
107
|
inputValue =>
|
|
106
|
-
getAllOptions.find( option => `${
|
|
107
|
-
[ getAllOptions,
|
|
108
|
+
getAllOptions.find( option => `${ optionLabel( option ) }` === `${ inputValue }` ),
|
|
109
|
+
[ getAllOptions, optionLabel ]
|
|
108
110
|
);
|
|
109
111
|
|
|
110
112
|
const onValueChange = useCallback(
|
|
111
113
|
inputValue => {
|
|
112
|
-
if (
|
|
113
|
-
onChange(
|
|
114
|
+
if ( inputValue ) {
|
|
115
|
+
onChange( getOptionByLabel( inputValue ), inputValue );
|
|
114
116
|
}
|
|
115
117
|
},
|
|
116
|
-
[ onChange,
|
|
118
|
+
[ onChange, getOptionByLabel ]
|
|
117
119
|
);
|
|
118
120
|
|
|
119
121
|
const suggest = useCallback(
|
|
120
122
|
( query, populateResults ) => {
|
|
121
123
|
const data = options.filter(
|
|
122
|
-
option => option.
|
|
124
|
+
option => optionLabel( option ).toLowerCase().indexOf( query.toLowerCase() ) >= 0
|
|
123
125
|
);
|
|
124
|
-
populateResults( data.map( option => option
|
|
126
|
+
populateResults( data.map( option => optionLabel( option ) ) );
|
|
125
127
|
},
|
|
126
128
|
[ options ]
|
|
127
129
|
);
|
|
@@ -155,6 +157,7 @@ const FormAutocomplete = React.forwardRef(
|
|
|
155
157
|
defaultValue={ value }
|
|
156
158
|
displayMenu={ displayMenu }
|
|
157
159
|
onConfirm={ onValueChange }
|
|
160
|
+
{ ...props }
|
|
158
161
|
/>
|
|
159
162
|
<FormSelectArrow />
|
|
160
163
|
</FormSelectContent>
|
|
@@ -173,6 +176,7 @@ FormAutocomplete.propTypes = {
|
|
|
173
176
|
displayMenu: PropTypes.string,
|
|
174
177
|
label: PropTypes.string,
|
|
175
178
|
options: PropTypes.array,
|
|
179
|
+
getOptionLabel: PropTypes.func,
|
|
176
180
|
getOptionValue: PropTypes.func,
|
|
177
181
|
onChange: PropTypes.func,
|
|
178
182
|
};
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
/**
|
|
4
4
|
* Internal dependencies
|
|
5
5
|
*/
|
|
6
|
-
import { useCallback, useState } from 'react';
|
|
7
6
|
import * as Form from '.';
|
|
8
7
|
|
|
9
8
|
export default {
|
|
@@ -20,70 +19,45 @@ export default {
|
|
|
20
19
|
},
|
|
21
20
|
};
|
|
22
21
|
|
|
23
|
-
const
|
|
22
|
+
const options = [
|
|
24
23
|
{ value: 'chocolate', label: 'Chocolate' },
|
|
25
24
|
{ value: 'strawberry', label: 'Strawberry Chocolate Vanilla Chocolate Vanilla' },
|
|
26
25
|
{ value: 'vanilla', label: 'Vanilla' },
|
|
27
26
|
];
|
|
28
27
|
|
|
28
|
+
const args = {
|
|
29
|
+
label: 'Label',
|
|
30
|
+
options,
|
|
31
|
+
};
|
|
32
|
+
|
|
29
33
|
// eslint-disable-next-line react/prop-types
|
|
30
|
-
const DefaultComponent = ( { label = 'Label', width = 250,
|
|
34
|
+
const DefaultComponent = ( { label = 'Label', width = 250, ...rest } ) => (
|
|
31
35
|
<>
|
|
32
36
|
<Form.Root>
|
|
33
37
|
<div sx={ { width } }>
|
|
34
|
-
<Form.Autocomplete
|
|
35
|
-
id="form-autocomplete"
|
|
36
|
-
label={ label }
|
|
37
|
-
onChange={ onChange }
|
|
38
|
-
{ ...rest }
|
|
39
|
-
/>
|
|
38
|
+
<Form.Autocomplete id="form-autocomplete" label={ label } { ...rest } />
|
|
40
39
|
</div>
|
|
41
40
|
</Form.Root>
|
|
42
41
|
</>
|
|
43
42
|
);
|
|
44
43
|
|
|
45
44
|
export const Default = () => {
|
|
46
|
-
const [ options, setOptions ] = useState( defaultOptions );
|
|
47
|
-
|
|
48
|
-
const onChange = useCallback( value => {
|
|
49
|
-
setOptions(
|
|
50
|
-
defaultOptions.filter(
|
|
51
|
-
option => ! value || option.label.toLowerCase().indexOf( value.toLowerCase() ) >= 0
|
|
52
|
-
)
|
|
53
|
-
);
|
|
54
|
-
} );
|
|
55
|
-
|
|
56
|
-
const args = {
|
|
57
|
-
label: 'Label',
|
|
58
|
-
options,
|
|
59
|
-
};
|
|
60
|
-
|
|
61
45
|
return (
|
|
62
46
|
<>
|
|
63
|
-
<DefaultComponent
|
|
47
|
+
<DefaultComponent { ...args } />
|
|
64
48
|
</>
|
|
65
49
|
);
|
|
66
50
|
};
|
|
67
51
|
|
|
68
52
|
export const Inline = () => {
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
setOptions(
|
|
73
|
-
defaultOptions.filter(
|
|
74
|
-
option => ! value || option.label.toLowerCase().indexOf( value.toLowerCase() ) >= 0
|
|
75
|
-
)
|
|
76
|
-
);
|
|
77
|
-
} );
|
|
78
|
-
|
|
79
|
-
const args = {
|
|
80
|
-
label: 'Label',
|
|
81
|
-
options,
|
|
53
|
+
const customArgs = {
|
|
54
|
+
...args,
|
|
55
|
+
isInline: true,
|
|
82
56
|
};
|
|
83
57
|
|
|
84
58
|
return (
|
|
85
59
|
<>
|
|
86
|
-
<DefaultComponent
|
|
60
|
+
<DefaultComponent { ...customArgs } />
|
|
87
61
|
</>
|
|
88
62
|
);
|
|
89
63
|
};
|