UNPKG

3.96 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.DataListItem = void 0;
4const tslib_1 = require("tslib");
5const React = tslib_1.__importStar(require("react"));
6const react_styles_1 = require("@patternfly/react-styles");
7const data_list_1 = tslib_1.__importDefault(require("@patternfly/react-styles/css/components/DataList/data-list"));
8const DataList_1 = require("./DataList");
9const constants_1 = require("../../helpers/constants");
10const DataListDragButton_1 = require("./DataListDragButton");
11function findDataListDragButton(node) {
12 if (!React.isValidElement(node)) {
13 return null;
14 }
15 if (node.type === DataListDragButton_1.DataListDragButton) {
16 return node;
17 }
18 if (node.props.children) {
19 for (const child of React.Children.toArray(node.props.children)) {
20 const button = findDataListDragButton(child);
21 if (button) {
22 return button;
23 }
24 }
25 }
26 return null;
27}
28class DataListItem extends React.Component {
29 render() {
30 const _a = this.props, { children, isExpanded, className, id, 'aria-labelledby': ariaLabelledBy } = _a, props = tslib_1.__rest(_a, ["children", "isExpanded", "className", "id", 'aria-labelledby']);
31 return (React.createElement(DataList_1.DataListContext.Consumer, null, ({ isSelectable, selectedDataListItemId, updateSelectedDataListItem, isDraggable, dragStart, dragEnd, drop }) => {
32 const selectDataListItem = (event) => {
33 let target = event.target;
34 while (event.currentTarget !== target) {
35 if (('onclick' in target && target.onclick) ||
36 target.parentNode.classList.contains(data_list_1.default.dataListItemAction) ||
37 target.parentNode.classList.contains(data_list_1.default.dataListItemControl)) {
38 // check other event handlers are not present.
39 return;
40 }
41 else {
42 target = target.parentNode;
43 }
44 }
45 updateSelectedDataListItem(id);
46 };
47 const onKeyDown = (event) => {
48 if (event.key === constants_1.KeyTypes.Enter) {
49 updateSelectedDataListItem(id);
50 }
51 };
52 // We made the DataListDragButton determine if the entire item is draggable instead of
53 // DataListItem like we should have.
54 // Recursively search children for the DataListDragButton and see if it's disabled...
55 const dragButton = findDataListDragButton(children);
56 const dragProps = isDraggable && {
57 draggable: dragButton ? !dragButton.props.isDisabled : true,
58 onDrop: drop,
59 onDragEnd: dragEnd,
60 onDragStart: dragStart
61 };
62 return (React.createElement("li", Object.assign({ id: id, className: react_styles_1.css(data_list_1.default.dataListItem, isExpanded && data_list_1.default.modifiers.expanded, isSelectable && data_list_1.default.modifiers.selectable, selectedDataListItemId && selectedDataListItemId === id && data_list_1.default.modifiers.selected, className), "aria-labelledby": ariaLabelledBy }, (isSelectable && { tabIndex: 0, onClick: selectDataListItem, onKeyDown }), (isSelectable && selectedDataListItemId === id && { 'aria-selected': true }), props, dragProps), React.Children.map(children, child => React.isValidElement(child) &&
63 React.cloneElement(child, {
64 rowid: ariaLabelledBy
65 }))));
66 }));
67 }
68}
69exports.DataListItem = DataListItem;
70DataListItem.displayName = 'DataListItem';
71DataListItem.defaultProps = {
72 isExpanded: false,
73 className: '',
74 id: '',
75 children: null,
76 'aria-labelledby': ''
77};
78//# sourceMappingURL=DataListItem.js.map
\No newline at end of file