-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathList.js
60 lines (54 loc) · 1.3 KB
/
List.js
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
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { camelCase } from 'lodash';
import css from './List.css';
const propTypes = {
id: PropTypes.string,
isEmptyMessage: PropTypes.oneOfType([
PropTypes.string,
PropTypes.node,
PropTypes.arrayOf(PropTypes.node),
]),
itemFormatter: PropTypes.func,
items: PropTypes.oneOfType([
PropTypes.array,
PropTypes.object,
]),
listClass: PropTypes.string,
listStyle: PropTypes.oneOf(['default', 'bullets']),
marginBottom0: PropTypes.bool,
};
const List = ({
id,
itemFormatter = (item, i) => (<li key={i}>{item}</li>),
listClass,
marginBottom0 = false,
listStyle = 'default',
items,
isEmptyMessage
}) => {
/**
* Get list classes
*/
const getListClass = () => classNames(
css.list,
listClass,
{ [css.marginBottom0]: marginBottom0 },
{ [css[camelCase(`list style ${listStyle}`)]]: listStyle }
);
/**
* Return empty message if we have one
* and there is no visible items
*/
if (!items.length && isEmptyMessage) {
return (<p className={css.isEmptyMessage}>{isEmptyMessage}</p>);
}
return (
<ul className={getListClass()} id={id}>
{items.map(itemFormatter, this)}
</ul>
);
};
List.propTypes = propTypes;
export default List;