1 | import { __rest } from "tslib";
|
2 | import * as React from 'react';
|
3 | import { css } from '@patternfly/react-styles';
|
4 | import styles from '@patternfly/react-styles/css/components/DataList/data-list';
|
5 | import { DataListContext } from './DataList';
|
6 | import { KeyTypes } from '../../helpers/constants';
|
7 | import { DataListDragButton } from './DataListDragButton';
|
8 | function findDataListDragButton(node) {
|
9 | if (!React.isValidElement(node)) {
|
10 | return null;
|
11 | }
|
12 | if (node.type === DataListDragButton) {
|
13 | return node;
|
14 | }
|
15 | if (node.props.children) {
|
16 | for (const child of React.Children.toArray(node.props.children)) {
|
17 | const button = findDataListDragButton(child);
|
18 | if (button) {
|
19 | return button;
|
20 | }
|
21 | }
|
22 | }
|
23 | return null;
|
24 | }
|
25 | export class DataListItem extends React.Component {
|
26 | render() {
|
27 | const _a = this.props, { children, isExpanded, className, id, 'aria-labelledby': ariaLabelledBy } = _a, props = __rest(_a, ["children", "isExpanded", "className", "id", 'aria-labelledby']);
|
28 | return (React.createElement(DataListContext.Consumer, null, ({ isSelectable, selectedDataListItemId, updateSelectedDataListItem, isDraggable, dragStart, dragEnd, drop }) => {
|
29 | const selectDataListItem = (event) => {
|
30 | let target = event.target;
|
31 | while (event.currentTarget !== target) {
|
32 | if (('onclick' in target && target.onclick) ||
|
33 | target.parentNode.classList.contains(styles.dataListItemAction) ||
|
34 | target.parentNode.classList.contains(styles.dataListItemControl)) {
|
35 |
|
36 | return;
|
37 | }
|
38 | else {
|
39 | target = target.parentNode;
|
40 | }
|
41 | }
|
42 | updateSelectedDataListItem(id);
|
43 | };
|
44 | const onKeyDown = (event) => {
|
45 | if (event.key === KeyTypes.Enter) {
|
46 | updateSelectedDataListItem(id);
|
47 | }
|
48 | };
|
49 |
|
50 |
|
51 |
|
52 | const dragButton = findDataListDragButton(children);
|
53 | const dragProps = isDraggable && {
|
54 | draggable: dragButton ? !dragButton.props.isDisabled : true,
|
55 | onDrop: drop,
|
56 | onDragEnd: dragEnd,
|
57 | onDragStart: dragStart
|
58 | };
|
59 | return (React.createElement("li", Object.assign({ id: id, className: css(styles.dataListItem, isExpanded && styles.modifiers.expanded, isSelectable && styles.modifiers.selectable, selectedDataListItemId && selectedDataListItemId === id && styles.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) &&
|
60 | React.cloneElement(child, {
|
61 | rowid: ariaLabelledBy
|
62 | }))));
|
63 | }));
|
64 | }
|
65 | }
|
66 | DataListItem.displayName = 'DataListItem';
|
67 | DataListItem.defaultProps = {
|
68 | isExpanded: false,
|
69 | className: '',
|
70 | id: '',
|
71 | children: null,
|
72 | 'aria-labelledby': ''
|
73 | };
|
74 |
|
\ | No newline at end of file |