-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathListCard.jsx
170 lines (161 loc) · 4.8 KB
/
ListCard.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import React from 'react';
import {
InlineLoading,
StructuredListWrapper,
StructuredListBody,
StructuredListRow,
StructuredListCell,
Link,
} from '@carbon/react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
import { isEmpty } from 'lodash-es';
import { CARD_SIZES, CARD_CONTENT_PADDING } from '../../constants/LayoutConstants';
import { CardPropTypes } from '../../constants/CardPropTypes';
import Card from '../Card/Card';
import { getResizeHandles } from '../../utils/cardUtilityFunctions';
import deprecate from '../../internal/deprecate';
const ListCard = ({
id,
title,
size,
data,
isLoading,
isResizable,
loadData,
hasMoreData,
layout,
className,
children,
testID,
testId,
...others
}) => {
const handleScroll = (e) => {
const element = e.target;
// height of the elements content - height element’s content is scrolled vertically === height of the scrollable part of the element
if (
element.scrollHeight - element.scrollTop === element.clientHeight &&
hasMoreData &&
!isLoading
) {
loadData();
}
};
const resizeHandles = isResizable ? getResizeHandles(children) : [];
return (
<Card
id={id}
title={title}
size={size}
onScroll={handleScroll}
isEmpty={isEmpty(data)}
resizeHandles={resizeHandles}
// TODO: remove deprecated 'testID' in v3.
testId={testID || testId}
{...others}
>
<div
className={classnames('list-card', className)}
style={{
paddingTop: 0,
paddingRight: CARD_CONTENT_PADDING,
paddingBottom: 0,
paddingLeft: CARD_CONTENT_PADDING,
}}
>
<StructuredListWrapper>
<StructuredListBody
// TODO: remove deprecated 'testID' in v3.
data-testid={`${testID || testId}-list-body`}
>
{data
? data.map((item) => {
return (
<StructuredListRow key={item.id}>
<StructuredListCell className="list-card--item" key={`${item.id}-cell`}>
<div className="list-card--item--value">
{item.link ? (
<Link style={{ display: 'inherit' }} target="_blank" href={item.link}>
{item.value}
</Link>
) : (
item.value
)}
</div>
{item.extraContent ? (
<div className="list-card--item--extra-content">{item.extraContent}</div>
) : null}
</StructuredListCell>
</StructuredListRow>
);
})
: null}
{isLoading ? (
<InlineLoading
// TODO: remove deprecated 'testID' in v3.
data-testid={`${testID || testId}-loading`}
description="Loading data..."
status="active"
/>
) : null}
</StructuredListBody>
</StructuredListWrapper>
</div>
</Card>
);
};
ListCard.propTypes = {
...CardPropTypes,
data: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
value: PropTypes.string.isRequired,
link: PropTypes.string,
extraContent: PropTypes.element,
})
),
isLoading: PropTypes.bool,
hasMoreData: PropTypes.bool,
loadData: PropTypes.func.isRequired,
layout: PropTypes.string,
size: (props, propName, componentName) => {
let error;
if (!Object.keys(CARD_SIZES).includes(props[propName])) {
error = new Error(
`\`${componentName}\` prop \`${propName}\` must be one of ${Object.keys(CARD_SIZES).join(
','
)}.`
);
}
// If the size
if (
props[propName] === CARD_SIZES.SMALL ||
props[propName] === CARD_SIZES.SMALLWIDE ||
props[propName] === CARD_SIZES.SMALLFULL
) {
error = new Error(
`Deprecation notice: \`${componentName}\` prop \`${propName}\` cannot be \`${props[propName]}\` as the lists will not render correctly. Minimum size is \`MEDIUM\``
);
}
return error;
},
// TODO: remove deprecated 'testID' in v3
// eslint-disable-next-line react/require-default-props
testID: deprecate(
PropTypes.string,
`The 'testID' prop has been deprecated. Please use 'testId' instead.`
),
testId: PropTypes.string,
};
ListCard.defaultProps = {
isLoading: false,
hasMoreData: false,
layout: '',
data: [],
size: CARD_SIZES.MEDIUM,
// TODO: replace with 'list-card' in v3. to set better defaults instead of inheriting from Card
testId: 'Card',
};
ListCard.displayName = 'ListCard';
export default ListCard;