UNPKG

2.93 kBJavaScriptView Raw
1import _defineProperty from "@babel/runtime/helpers/defineProperty";
2import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3var _excluded = ["activeIndex", "id", "isFocused", "isMenuShown", "multiple", "onClick", "onFocus", "placeholder"];
4
5function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
6
7function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
8
9import cx from 'classnames';
10import getMenuItemId from './getMenuItemId';
11import hasOwnProperty from './hasOwnProperty';
12
13var getInputProps = function getInputProps(_ref) {
14 var activeIndex = _ref.activeIndex,
15 id = _ref.id,
16 isFocused = _ref.isFocused,
17 isMenuShown = _ref.isMenuShown,
18 multiple = _ref.multiple,
19 onClick = _ref.onClick,
20 onFocus = _ref.onFocus,
21 placeholder = _ref.placeholder,
22 rest = _objectWithoutProperties(_ref, _excluded);
23
24 return function () {
25 var _cx;
26
27 var inputProps = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
28 var className = hasOwnProperty(inputProps, 'className') ? inputProps.className : undefined;
29
30 var props = _objectSpread(_objectSpread(_objectSpread({
31 // These props can be overridden by values in `inputProps`.
32 autoComplete: 'off',
33 placeholder: placeholder,
34 type: 'text'
35 }, inputProps), rest), {}, {
36 'aria-activedescendant': activeIndex >= 0 ? getMenuItemId(id, activeIndex) : undefined,
37 'aria-autocomplete': 'both',
38 'aria-expanded': isMenuShown,
39 'aria-haspopup': 'listbox',
40 'aria-owns': isMenuShown ? id : undefined,
41 className: cx((_cx = {}, _defineProperty(_cx, className || '', !multiple), _defineProperty(_cx, "focus", isFocused), _cx)),
42 onClick: onClick,
43 onFocus: onFocus,
44 // Comboboxes are single-select by definition:
45 // https://www.w3.org/TR/wai-aria-practices-1.1/#combobox
46 role: 'combobox'
47 });
48
49 if (!multiple) {
50 return props;
51 }
52
53 return _objectSpread(_objectSpread({}, props), {}, {
54 'aria-autocomplete': 'list',
55 'aria-expanded': undefined,
56 inputClassName: className,
57 role: undefined
58 });
59 };
60};
61
62export default getInputProps;
\No newline at end of file