@automattic/vip-design-system 0.24.0 → 0.25.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.
@@ -0,0 +1,206 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ exports.__esModule = true;
6
+ exports.FormAutocomplete = void 0;
7
+
8
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
+
10
+ var _react = _interopRequireWildcard(require("react"));
11
+
12
+ var _propTypes = _interopRequireDefault(require("prop-types"));
13
+
14
+ var _Label = require("../Form/Label");
15
+
16
+ var _react2 = _interopRequireDefault(require("accessible-autocomplete/react"));
17
+
18
+ var _FormAutocomplete = _interopRequireDefault(require("./FormAutocomplete.css"));
19
+
20
+ exports.css = _FormAutocomplete["default"];
21
+
22
+ var _FormSelectContent = require("./FormSelectContent");
23
+
24
+ var _FormSelectArrow = require("./FormSelectArrow");
25
+
26
+ var _jsxRuntime = require("theme-ui/jsx-runtime");
27
+
28
+ 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
+
30
+ 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
+
32
+ /** @jsxImportSource theme-ui */
33
+
34
+ /**
35
+ * External dependencies
36
+ */
37
+
38
+ /**
39
+ * Internal dependencies
40
+ */
41
+ var defaultStyles = {
42
+ width: '100%',
43
+ py: 0,
44
+ borderWidth: '1px',
45
+ borderStyle: 'solid',
46
+ borderColor: 'border',
47
+ borderRadius: 1,
48
+ backgroundColor: 'background',
49
+ color: 'text',
50
+ minHeight: '36px',
51
+ lineHeight: '36px',
52
+ '&:focus': function focus(theme) {
53
+ return theme.outline;
54
+ },
55
+ '&:focus-visible': function focusVisible(theme) {
56
+ return theme.outline;
57
+ },
58
+ '&:focus-within': function focusWithin(theme) {
59
+ return theme.outline;
60
+ },
61
+ '&.autocomplete__input--focused': function autocomplete__inputFocused(theme) {
62
+ return theme.outline;
63
+ },
64
+ '& .autocomplete__input': {
65
+ width: '100%',
66
+ paddingLeft: 3,
67
+ py: 0,
68
+ borderWidth: 0,
69
+ color: 'text',
70
+ minHeight: '36px',
71
+ lineHeight: '36px',
72
+ '&:focus': {
73
+ outlineWidth: 0,
74
+ boxShadow: 'none'
75
+ },
76
+ '&:focus-visible': {
77
+ outlineWidth: 0,
78
+ boxShadow: 'none'
79
+ },
80
+ '&:focus-within': {
81
+ outlineWidth: 0,
82
+ boxShadow: 'none'
83
+ },
84
+ '&.autocomplete__input--focused': {
85
+ outlineWidth: 0,
86
+ boxShadow: 'none'
87
+ }
88
+ },
89
+ '& .autocomplete__menu': {
90
+ borderWidth: '1px',
91
+ borderStyle: 'solid',
92
+ borderColor: 'border',
93
+ borderRadius: 1,
94
+ backgroundColor: 'background',
95
+ color: 'text'
96
+ },
97
+ '& .autocomplete__hint, & .autocomplete__input, & .autocomplete__option': {
98
+ fontSize: 'inherit'
99
+ },
100
+ '& .autocomplete__wrapper': {
101
+ width: '100%',
102
+ paddingRight: '40px'
103
+ },
104
+ '& .autocomplete__input--show-all-values': {
105
+ paddingRight: 0
106
+ }
107
+ };
108
+ var inlineStyles = {
109
+ borderWidth: 0
110
+ };
111
+
112
+ var FormAutocomplete = /*#__PURE__*/_react["default"].forwardRef(function (_ref, forwardRef) {
113
+ var isInline = _ref.isInline,
114
+ forLabel = _ref.forLabel,
115
+ options = _ref.options,
116
+ label = _ref.label,
117
+ getOptionValue = _ref.getOptionValue,
118
+ onChange = _ref.onChange,
119
+ value = _ref.value,
120
+ _ref$showAllValues = _ref.showAllValues,
121
+ showAllValues = _ref$showAllValues === void 0 ? true : _ref$showAllValues,
122
+ _ref$displayMenu = _ref.displayMenu,
123
+ displayMenu = _ref$displayMenu === void 0 ? 'overlay' : _ref$displayMenu,
124
+ _ref$id = _ref.id,
125
+ id = _ref$id === void 0 ? 'vip-autocomplete' : _ref$id;
126
+
127
+ var SelectLabel = function SelectLabel() {
128
+ return (0, _jsxRuntime.jsx)(_Label.Label, {
129
+ htmlFor: forLabel || id,
130
+ children: label
131
+ });
132
+ };
133
+
134
+ var inlineLabel = !!(isInline && label);
135
+ var optionValue = (0, _react.useCallback)(function (option) {
136
+ return getOptionValue ? getOptionValue(option) : option.value;
137
+ }, [getOptionValue]);
138
+ var getAllOptions = (0, _react.useMemo)(function () {
139
+ return [].concat(options.filter(function (option) {
140
+ return !option.options;
141
+ }), options.filter(function (option) {
142
+ return option.options;
143
+ }).map(function (option) {
144
+ return option.options;
145
+ })).reduce(function (a, b) {
146
+ return a.concat(b);
147
+ }, []);
148
+ }, [options]);
149
+ var getOptionByValue = (0, _react.useCallback)(function (inputValue) {
150
+ return getAllOptions.find(function (option) {
151
+ return "" + optionValue(option) === "" + inputValue;
152
+ });
153
+ }, [getAllOptions, optionValue]);
154
+ var onValueChange = (0, _react.useCallback)(function (inputValue) {
155
+ if (onChange) {
156
+ onChange(getOptionByValue(inputValue));
157
+ }
158
+ }, [onChange, getOptionByValue]);
159
+ var suggest = (0, _react.useCallback)(function (query, populateResults) {
160
+ var data = options.filter(function (option) {
161
+ return option.label.toLowerCase().indexOf(query.toLowerCase()) >= 0;
162
+ });
163
+ populateResults(data.map(function (option) {
164
+ return option.label;
165
+ }));
166
+ }, [options]);
167
+ (0, _react.useEffect)(function () {
168
+ global.document.querySelector('.autocomplete__input').setAttribute('aria-activedescendant', '');
169
+ }, []);
170
+ (0, _react.useEffect)(function () {
171
+ global.document.querySelector('.autocomplete__menu').setAttribute('aria-label', label + " list");
172
+ }, [label]);
173
+ return (0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
174
+ children: [label && !isInline && (0, _jsxRuntime.jsx)(SelectLabel, {}), (0, _jsxRuntime.jsx)("div", {
175
+ sx: (0, _extends2["default"])({}, defaultStyles, isInline && inlineStyles),
176
+ children: (0, _jsxRuntime.jsxs)(_FormSelectContent.FormSelectContent, {
177
+ isInline: inlineLabel,
178
+ label: inlineLabel ? (0, _jsxRuntime.jsx)(SelectLabel, {}) : null,
179
+ children: [(0, _jsxRuntime.jsx)(_react2["default"], {
180
+ id: id,
181
+ showAllValues: showAllValues,
182
+ ref: forwardRef,
183
+ source: suggest,
184
+ defaultValue: value,
185
+ displayMenu: displayMenu,
186
+ onConfirm: onValueChange
187
+ }), (0, _jsxRuntime.jsx)(_FormSelectArrow.FormSelectArrow, {})]
188
+ })
189
+ })]
190
+ });
191
+ });
192
+
193
+ exports.FormAutocomplete = FormAutocomplete;
194
+ FormAutocomplete.propTypes = {
195
+ id: _propTypes["default"].string,
196
+ showAllValues: _propTypes["default"].bool,
197
+ isInline: _propTypes["default"].bool,
198
+ forLabel: _propTypes["default"].string,
199
+ value: _propTypes["default"].string,
200
+ displayMenu: _propTypes["default"].string,
201
+ label: _propTypes["default"].string,
202
+ options: _propTypes["default"].array,
203
+ getOptionValue: _propTypes["default"].func,
204
+ onChange: _propTypes["default"].func
205
+ };
206
+ FormAutocomplete.displayName = 'FormAutocomplete';
@@ -0,0 +1,127 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ exports.__esModule = true;
6
+ exports["default"] = exports.Inline = exports.Default = void 0;
7
+
8
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
9
+
10
+ var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));
11
+
12
+ var _react = require("react");
13
+
14
+ var Form = _interopRequireWildcard(require("."));
15
+
16
+ var _jsxRuntime = require("theme-ui/jsx-runtime");
17
+
18
+ var _excluded = ["label", "width", "onChange"];
19
+
20
+ 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
+
22
+ 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; }
23
+
24
+ var _default = {
25
+ title: 'Form/Autocomplete',
26
+ argTypes: {
27
+ placeholder: {
28
+ type: {
29
+ name: 'string',
30
+ required: false
31
+ },
32
+ control: {
33
+ type: 'text'
34
+ }
35
+ },
36
+ label: {
37
+ type: {
38
+ name: 'string',
39
+ required: false
40
+ },
41
+ control: {
42
+ type: 'text'
43
+ }
44
+ }
45
+ }
46
+ };
47
+ exports["default"] = _default;
48
+ var defaultOptions = [{
49
+ value: 'chocolate',
50
+ label: 'Chocolate'
51
+ }, {
52
+ value: 'strawberry',
53
+ label: 'Strawberry Chocolate Vanilla Chocolate Vanilla'
54
+ }, {
55
+ value: 'vanilla',
56
+ label: 'Vanilla'
57
+ }]; // eslint-disable-next-line react/prop-types
58
+
59
+ var DefaultComponent = function DefaultComponent(_ref) {
60
+ var _ref$label = _ref.label,
61
+ label = _ref$label === void 0 ? 'Label' : _ref$label,
62
+ _ref$width = _ref.width,
63
+ width = _ref$width === void 0 ? 250 : _ref$width,
64
+ onChange = _ref.onChange,
65
+ rest = (0, _objectWithoutPropertiesLoose2["default"])(_ref, _excluded);
66
+ return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
67
+ children: (0, _jsxRuntime.jsx)(Form.Root, {
68
+ children: (0, _jsxRuntime.jsx)("div", {
69
+ sx: {
70
+ width: width
71
+ },
72
+ children: (0, _jsxRuntime.jsx)(Form.Autocomplete, (0, _extends2["default"])({
73
+ id: "form-autocomplete",
74
+ label: label,
75
+ onChange: onChange
76
+ }, rest))
77
+ })
78
+ })
79
+ });
80
+ };
81
+
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
+ return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
97
+ children: (0, _jsxRuntime.jsx)(DefaultComponent, (0, _extends2["default"])({
98
+ onChange: onChange
99
+ }, args))
100
+ });
101
+ };
102
+
103
+ exports.Default = Default;
104
+
105
+ var Inline = function Inline() {
106
+ var _useState2 = (0, _react.useState)(defaultOptions),
107
+ options = _useState2[0],
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
+ }));
114
+ });
115
+ var args = {
116
+ label: 'Label',
117
+ options: options
118
+ };
119
+ return (0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
120
+ children: (0, _jsxRuntime.jsx)(DefaultComponent, (0, _extends2["default"])({
121
+ isInline: true,
122
+ onChange: onChange
123
+ }, args))
124
+ });
125
+ };
126
+
127
+ exports.Inline = Inline;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
6
+
7
+ var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
8
+
9
+ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
10
+
11
+ var _react = require("@testing-library/react");
12
+
13
+ var _jestAxe = require("jest-axe");
14
+
15
+ var _FormAutocomplete = require("./FormAutocomplete");
16
+
17
+ var _jsxRuntime = require("theme-ui/jsx-runtime");
18
+
19
+ /**
20
+ * External dependencies
21
+ */
22
+
23
+ /**
24
+ * Internal dependencies
25
+ */
26
+ var options = [{
27
+ value: 'chocolate',
28
+ label: 'Chocolate'
29
+ }, {
30
+ value: 'strawberry',
31
+ label: 'Strawberry Chocolate Vanilla Chocolate Vanilla'
32
+ }, {
33
+ value: 'vanilla',
34
+ label: 'Vanilla'
35
+ }];
36
+ var defaultProps = {
37
+ label: 'This is a label',
38
+ options: options
39
+ };
40
+ describe('<FormAutocomplete />', function () {
41
+ it('renders the FormAutocomplete component', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
42
+ var _render, container;
43
+
44
+ return _regenerator["default"].wrap(function _callee$(_context) {
45
+ while (1) {
46
+ switch (_context.prev = _context.next) {
47
+ case 0:
48
+ _render = (0, _react.render)((0, _jsxRuntime.jsx)(_FormAutocomplete.FormAutocomplete, (0, _extends2["default"])({
49
+ id: "my_desert_list"
50
+ }, defaultProps))), container = _render.container;
51
+ expect(_react.screen.getByLabelText(defaultProps.label)).toBeInTheDocument(); // Check for accessibility issues
52
+
53
+ _context.t0 = expect;
54
+ _context.next = 5;
55
+ return (0, _jestAxe.axe)(container);
56
+
57
+ case 5:
58
+ _context.t1 = _context.sent;
59
+ _context.next = 8;
60
+ return (0, _context.t0)(_context.t1).toHaveNoViolations();
61
+
62
+ case 8:
63
+ case "end":
64
+ return _context.stop();
65
+ }
66
+ }
67
+ }, _callee);
68
+ })));
69
+ });
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports["default"] = exports.Select = exports.Root = void 0;
4
+ exports["default"] = exports.Select = exports.Root = exports.Autocomplete = void 0;
5
5
 
6
6
  var _FormSelect = require("./FormSelect");
7
7
 
8
+ var _FormAutocomplete = require("./FormAutocomplete");
9
+
8
10
  var _Form = require("./Form");
9
11
 
10
12
  /**
@@ -12,6 +14,8 @@ var _Form = require("./Form");
12
14
  */
13
15
  var Select = _FormSelect.FormSelect;
14
16
  exports.Select = Select;
17
+ var Autocomplete = _FormAutocomplete.FormAutocomplete;
18
+ exports.Autocomplete = Autocomplete;
15
19
  var Root = _Form.Form;
16
20
  exports.Root = Root;
17
21
  var _default = Root;
@@ -53,7 +53,7 @@ var StyledListItem = function StyledListItem(props) {
53
53
  sx: {
54
54
  py: 2,
55
55
  borderBottom: '1px solid',
56
- borderColor: 'border',
56
+ borderColor: 'borders.2',
57
57
  listStyleType: 'none',
58
58
  margin: 0,
59
59
  px: 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/vip-design-system",
3
- "version": "0.24.0",
3
+ "version": "0.25.0",
4
4
  "main": "build/system/index.js",
5
5
  "scripts": {
6
6
  "build-storybook": "build-storybook",
@@ -29,6 +29,7 @@
29
29
  "@radix-ui/react-tooltip": "^1.0.0",
30
30
  "@radix-ui/react-visually-hidden": "^1.0.0",
31
31
  "@storybook/addon-storysource": "^6.5.10",
32
+ "accessible-autocomplete": "^2.0.4",
32
33
  "babel-loader": "^8.2.2",
33
34
  "classnames": "^2.3.1",
34
35
  "framer-motion": "^3.9.1",
@@ -71,6 +72,9 @@
71
72
  ],
72
73
  "transform": {
73
74
  "\\.[jt]sx?$": "babel-jest"
75
+ },
76
+ "moduleNameMapper": {
77
+ "\\.(css|less)$": "<rootDir>/test/fileMock.js"
74
78
  }
75
79
  },
76
80
  "devDependencies": {
@@ -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
+ }
@@ -0,0 +1,182 @@
1
+ /** @jsxImportSource theme-ui */
2
+
3
+ /**
4
+ * External dependencies
5
+ */
6
+ import React, { useCallback, useEffect, useMemo } from 'react';
7
+ import PropTypes from 'prop-types';
8
+ import { Label } from '../Form/Label';
9
+ import Autocomplete from 'accessible-autocomplete/react';
10
+
11
+ /**
12
+ * Internal dependencies
13
+ */
14
+ import css from './FormAutocomplete.css';
15
+ import { FormSelectContent } from './FormSelectContent';
16
+ import { FormSelectArrow } from './FormSelectArrow';
17
+
18
+ const defaultStyles = {
19
+ width: '100%',
20
+ py: 0,
21
+ borderWidth: '1px',
22
+ borderStyle: 'solid',
23
+ borderColor: 'border',
24
+ borderRadius: 1,
25
+ backgroundColor: 'background',
26
+ color: 'text',
27
+ minHeight: '36px',
28
+ lineHeight: '36px',
29
+ '&:focus': theme => theme.outline,
30
+ '&:focus-visible': theme => theme.outline,
31
+ '&:focus-within': theme => theme.outline,
32
+ '&.autocomplete__input--focused': theme => theme.outline,
33
+ '& .autocomplete__input': {
34
+ width: '100%',
35
+ paddingLeft: 3,
36
+ py: 0,
37
+ borderWidth: 0,
38
+ color: 'text',
39
+ minHeight: '36px',
40
+ lineHeight: '36px',
41
+ '&:focus': { outlineWidth: 0, boxShadow: 'none' },
42
+ '&:focus-visible': { outlineWidth: 0, boxShadow: 'none' },
43
+ '&:focus-within': { outlineWidth: 0, boxShadow: 'none' },
44
+ '&.autocomplete__input--focused': { outlineWidth: 0, boxShadow: 'none' },
45
+ },
46
+ '& .autocomplete__menu': {
47
+ borderWidth: '1px',
48
+ borderStyle: 'solid',
49
+ borderColor: 'border',
50
+ borderRadius: 1,
51
+ backgroundColor: 'background',
52
+ color: 'text',
53
+ },
54
+ '& .autocomplete__hint, & .autocomplete__input, & .autocomplete__option': {
55
+ fontSize: 'inherit',
56
+ },
57
+ '& .autocomplete__wrapper': {
58
+ width: '100%',
59
+ paddingRight: '40px',
60
+ },
61
+ '& .autocomplete__input--show-all-values': {
62
+ paddingRight: 0,
63
+ },
64
+ };
65
+
66
+ const inlineStyles = {
67
+ borderWidth: 0,
68
+ };
69
+
70
+ const FormAutocomplete = React.forwardRef(
71
+ (
72
+ {
73
+ isInline,
74
+ forLabel,
75
+ options,
76
+ label,
77
+ getOptionValue,
78
+ onChange,
79
+ value,
80
+ showAllValues = true,
81
+ displayMenu = 'overlay',
82
+ id = 'vip-autocomplete',
83
+ },
84
+ forwardRef
85
+ ) => {
86
+ const SelectLabel = () => <Label htmlFor={ forLabel || id }>{ label }</Label>;
87
+
88
+ const inlineLabel = !! ( isInline && label );
89
+
90
+ const optionValue = useCallback(
91
+ option => ( getOptionValue ? getOptionValue( option ) : option.value ),
92
+ [ getOptionValue ]
93
+ );
94
+
95
+ const getAllOptions = useMemo(
96
+ () =>
97
+ [
98
+ ...options.filter( option => ! option.options ),
99
+ ...options.filter( option => option.options ).map( option => option.options ),
100
+ ].reduce( ( a, b ) => a.concat( b ), [] ),
101
+ [ options ]
102
+ );
103
+
104
+ const getOptionByValue = useCallback(
105
+ inputValue =>
106
+ getAllOptions.find( option => `${ optionValue( option ) }` === `${ inputValue }` ),
107
+ [ getAllOptions, optionValue ]
108
+ );
109
+
110
+ const onValueChange = useCallback(
111
+ inputValue => {
112
+ if ( onChange ) {
113
+ onChange( getOptionByValue( inputValue ) );
114
+ }
115
+ },
116
+ [ onChange, getOptionByValue ]
117
+ );
118
+
119
+ const suggest = useCallback(
120
+ ( query, populateResults ) => {
121
+ const data = options.filter(
122
+ option => option.label.toLowerCase().indexOf( query.toLowerCase() ) >= 0
123
+ );
124
+ populateResults( data.map( option => option.label ) );
125
+ },
126
+ [ options ]
127
+ );
128
+
129
+ useEffect( () => {
130
+ global.document
131
+ .querySelector( '.autocomplete__input' )
132
+ .setAttribute( 'aria-activedescendant', '' );
133
+ }, [] );
134
+
135
+ useEffect( () => {
136
+ global.document
137
+ .querySelector( '.autocomplete__menu' )
138
+ .setAttribute( 'aria-label', `${ label } list` );
139
+ }, [ label ] );
140
+
141
+ return (
142
+ <>
143
+ { label && ! isInline && <SelectLabel /> }
144
+
145
+ <div sx={ { ...defaultStyles, ...( isInline && inlineStyles ) } }>
146
+ <FormSelectContent
147
+ isInline={ inlineLabel }
148
+ label={ inlineLabel ? <SelectLabel /> : null }
149
+ >
150
+ <Autocomplete
151
+ id={ id }
152
+ showAllValues={ showAllValues }
153
+ ref={ forwardRef }
154
+ source={ suggest }
155
+ defaultValue={ value }
156
+ displayMenu={ displayMenu }
157
+ onConfirm={ onValueChange }
158
+ />
159
+ <FormSelectArrow />
160
+ </FormSelectContent>
161
+ </div>
162
+ </>
163
+ );
164
+ }
165
+ );
166
+
167
+ FormAutocomplete.propTypes = {
168
+ id: PropTypes.string,
169
+ showAllValues: PropTypes.bool,
170
+ isInline: PropTypes.bool,
171
+ forLabel: PropTypes.string,
172
+ value: PropTypes.string,
173
+ displayMenu: PropTypes.string,
174
+ label: PropTypes.string,
175
+ options: PropTypes.array,
176
+ getOptionValue: PropTypes.func,
177
+ onChange: PropTypes.func,
178
+ };
179
+
180
+ FormAutocomplete.displayName = 'FormAutocomplete';
181
+
182
+ export { FormAutocomplete, css };
@@ -0,0 +1,89 @@
1
+ /** @jsxImportSource theme-ui */
2
+
3
+ /**
4
+ * Internal dependencies
5
+ */
6
+ import { useCallback, useState } from 'react';
7
+ import * as Form from '.';
8
+
9
+ export default {
10
+ title: 'Form/Autocomplete',
11
+ argTypes: {
12
+ placeholder: {
13
+ type: { name: 'string', required: false },
14
+ control: { type: 'text' },
15
+ },
16
+ label: {
17
+ type: { name: 'string', required: false },
18
+ control: { type: 'text' },
19
+ },
20
+ },
21
+ };
22
+
23
+ const defaultOptions = [
24
+ { value: 'chocolate', label: 'Chocolate' },
25
+ { value: 'strawberry', label: 'Strawberry Chocolate Vanilla Chocolate Vanilla' },
26
+ { value: 'vanilla', label: 'Vanilla' },
27
+ ];
28
+
29
+ // eslint-disable-next-line react/prop-types
30
+ const DefaultComponent = ( { label = 'Label', width = 250, onChange, ...rest } ) => (
31
+ <>
32
+ <Form.Root>
33
+ <div sx={ { width } }>
34
+ <Form.Autocomplete
35
+ id="form-autocomplete"
36
+ label={ label }
37
+ onChange={ onChange }
38
+ { ...rest }
39
+ />
40
+ </div>
41
+ </Form.Root>
42
+ </>
43
+ );
44
+
45
+ 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
+ return (
62
+ <>
63
+ <DefaultComponent onChange={ onChange } { ...args } />
64
+ </>
65
+ );
66
+ };
67
+
68
+ export const Inline = () => {
69
+ const [ options, setOptions ] = useState( defaultOptions );
70
+
71
+ const onChange = useCallback( value => {
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,
82
+ };
83
+
84
+ return (
85
+ <>
86
+ <DefaultComponent isInline={ true } onChange={ onChange } { ...args } />
87
+ </>
88
+ );
89
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { render, screen } from '@testing-library/react';
5
+ import { axe } from 'jest-axe';
6
+
7
+ /**
8
+ * Internal dependencies
9
+ */
10
+ import { FormAutocomplete } from './FormAutocomplete';
11
+
12
+ const options = [
13
+ { value: 'chocolate', label: 'Chocolate' },
14
+ { value: 'strawberry', label: 'Strawberry Chocolate Vanilla Chocolate Vanilla' },
15
+ { value: 'vanilla', label: 'Vanilla' },
16
+ ];
17
+
18
+ const defaultProps = {
19
+ label: 'This is a label',
20
+ options,
21
+ };
22
+
23
+ describe( '<FormAutocomplete />', () => {
24
+ it( 'renders the FormAutocomplete component', async () => {
25
+ const { container } = render( <FormAutocomplete id="my_desert_list" { ...defaultProps } /> );
26
+
27
+ expect( screen.getByLabelText( defaultProps.label ) ).toBeInTheDocument();
28
+
29
+ // Check for accessibility issues
30
+ await expect( await axe( container ) ).toHaveNoViolations();
31
+ } );
32
+ } );
@@ -70,7 +70,7 @@ const DefaultComponent = ( { label = 'Label', width = 250, onChange, ...rest } )
70
70
  export const Default = DefaultComponent.bind( {} );
71
71
  Default.args = {
72
72
  placeholder: '- Select -',
73
- options: options,
73
+ options,
74
74
  };
75
75
 
76
76
  export const WithGroup = DefaultComponent.bind( {} );
@@ -3,11 +3,13 @@
3
3
  */
4
4
 
5
5
  import { FormSelect } from './FormSelect';
6
+ import { FormAutocomplete } from './FormAutocomplete';
6
7
  import { Form } from './Form';
7
8
 
8
9
  const Select = FormSelect;
10
+ const Autocomplete = FormAutocomplete;
9
11
  const Root = Form;
10
12
 
11
- export { Root, Select };
13
+ export { Root, Select, Autocomplete };
12
14
 
13
15
  export default Root;
@@ -0,0 +1 @@
1
+ module.exports = {};
@@ -1,38 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.MultiSelect = void 0;
5
-
6
- var _reactSelect = _interopRequireDefault(require("react-select"));
7
-
8
- var _jsxRuntime = require("theme-ui/jsx-runtime");
9
-
10
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
11
-
12
- function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
13
-
14
- var vipGold = '#c29c69'; // hardcoding for now
15
-
16
- var vipGrey2 = '#d7dee2';
17
- var customStyles = {
18
- control: function control(styles) {
19
- return _extends({}, styles, {
20
- border: "1px solid " + vipGrey2,
21
- boxShadow: 'none',
22
- '&:hover': {
23
- border: "1px solid " + vipGold
24
- },
25
- '&:focus': {
26
- border: "1px solid " + vipGold
27
- }
28
- });
29
- }
30
- };
31
-
32
- var MultiSelect = function MultiSelect(props) {
33
- return (0, _jsxRuntime.jsx)(_reactSelect["default"], _extends({}, props, {
34
- styles: customStyles
35
- }));
36
- };
37
-
38
- exports.MultiSelect = MultiSelect;
@@ -1,60 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.UsageChart = void 0;
5
-
6
- var _framerMotion = require("framer-motion");
7
-
8
- var _propTypes = _interopRequireDefault(require("prop-types"));
9
-
10
- var _jsxRuntime = require("theme-ui/jsx-runtime");
11
-
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
13
-
14
- /**
15
- * External dependencies
16
- */
17
- var UsageChart = function UsageChart(_ref) {
18
- var total = _ref.total,
19
- max = _ref.max,
20
- _ref$variant = _ref.variant,
21
- variant = _ref$variant === void 0 ? 'primary' : _ref$variant;
22
- var width = total / max * 100 + '%';
23
- var formattedTotal = total;
24
-
25
- if (total > 1000000) {
26
- formattedTotal = (total / 1000000).toFixed(2) + "M";
27
- } else if (total > 1000) {
28
- formattedTotal = (total / 1000).toFixed(2) + "K";
29
- }
30
-
31
- return (0, _jsxRuntime.jsx)("div", {
32
- sx: {
33
- height: variant === 'primary' ? 32 : 8,
34
- overflow: 'hidden',
35
- backgroundColor: variant === 'primary' ? 'border' : 'transparent'
36
- },
37
- children: (0, _jsxRuntime.jsx)(_framerMotion.motion.div, {
38
- initial: {
39
- width: 0
40
- },
41
- animate: {
42
- width: width
43
- },
44
- transition: {
45
- duration: 0.7
46
- },
47
- sx: {
48
- height: '100%',
49
- backgroundColor: variant === 'primary' ? 'primary' : 'grey.40'
50
- }
51
- })
52
- });
53
- };
54
-
55
- exports.UsageChart = UsageChart;
56
- UsageChart.propTypes = {
57
- total: _propTypes["default"].number,
58
- max: _propTypes["default"].number,
59
- variant: _propTypes["default"].string
60
- };
@@ -1,7 +0,0 @@
1
- "use strict";
2
-
3
- exports.__esModule = true;
4
-
5
- var _UsageChart = require("./UsageChart");
6
-
7
- exports.UsageChart = _UsageChart.UsageChart;