UNPKG

2.01 kBJavaScriptView Raw
1const _excluded = ["className", "children", "dataItem", "selected", "disabled", "onSelect", "searchTerm"];
2
3function _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); }
4
5function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
6
7import cn from 'classnames';
8import PropTypes from 'prop-types';
9import React from 'react';
10import { useListOption } from './FocusListContext';
11const propTypes = {
12 activeId: PropTypes.string,
13 dataItem: PropTypes.any,
14 selected: PropTypes.bool.isRequired,
15 onSelect: PropTypes.func.isRequired,
16 component: PropTypes.any
17};
18
19function ListOption(_ref) {
20 let {
21 className,
22 children,
23 dataItem,
24 selected,
25 disabled,
26 onSelect
27 } = _ref,
28 props = _objectWithoutPropertiesLoose(_ref, _excluded);
29
30 const [ref, focused, id] = useListOption(dataItem);
31
32 const handleSelect = event => {
33 if (onSelect && !disabled) onSelect(dataItem, event);
34 };
35
36 let classes = {
37 'rw-state-focus': focused,
38 'rw-state-selected': selected,
39 'rw-state-disabled': disabled
40 };
41 return /*#__PURE__*/React.createElement("div", _extends({
42 id: id,
43 ref: ref,
44 role: "option",
45 "data-rw-option": "",
46 "data-rw-focused": focused ? '' : undefined,
47 "data-rw-focusable": !disabled ? '' : undefined,
48 tabIndex: -1,
49 "aria-selected": !!selected,
50 className: cn('rw-list-option', className, classes),
51 onClick: handleSelect
52 }, props), children);
53} // @ts-ignore
54
55
56ListOption.propTypes = propTypes;
57export default ListOption;
\No newline at end of file