index.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import React from 'react';
  2. import cls from 'classnames';
  3. import PropTypes from 'prop-types';
  4. import { cssClasses, strings } from '@douyinfe/semi-foundation/list/constants';
  5. import { noop } from 'lodash';
  6. import '@douyinfe/semi-foundation/list/list.scss';
  7. import LocaleConsumer from '../locale/localeConsumer';
  8. import { Locale } from '../locale/interface';
  9. import ListItem from './item';
  10. import { Row } from '../grid';
  11. import Spin from '../spin';
  12. import ListContext, { Grid } from './list-context';
  13. import BaseComponent from '../_base/baseComponent';
  14. export { ListItemProps } from './item';
  15. export interface ListProps<T> {
  16. style?: React.CSSProperties;
  17. className?: string;
  18. children?: React.ReactNode;
  19. bordered?: boolean;
  20. footer?: React.ReactNode;
  21. header?: React.ReactNode;
  22. layout?: 'vertical' | 'horizontal';
  23. size?: 'small' | 'large' | 'default';
  24. split?: boolean;
  25. emptyContent?: React.ReactNode;
  26. dataSource?: T[];
  27. renderItem?: (item: T, ind: number) => React.ReactNode;
  28. grid?: Grid;
  29. loading?: boolean;
  30. loadMore?: React.ReactNode;
  31. onClick?: React.MouseEventHandler<HTMLLIElement>;
  32. onRightClick?: React.MouseEventHandler<HTMLLIElement>;
  33. }
  34. const prefixCls = cssClasses.PREFIX;
  35. class List<T = any> extends BaseComponent<ListProps<T>> {
  36. static Item = ListItem;
  37. static propTypes = {
  38. style: PropTypes.object,
  39. className: PropTypes.string,
  40. bordered: PropTypes.bool,
  41. footer: PropTypes.node,
  42. header: PropTypes.node,
  43. layout: PropTypes.oneOf(strings.LAYOUT),
  44. size: PropTypes.oneOf(strings.SIZE),
  45. split: PropTypes.bool,
  46. emptyContent: PropTypes.node,
  47. dataSource: PropTypes.array,
  48. renderItem: PropTypes.func,
  49. grid: PropTypes.object,
  50. loading: PropTypes.bool,
  51. loadMore: PropTypes.node,
  52. onRightClick: PropTypes.func,
  53. onClick: PropTypes.func,
  54. };
  55. static defaultProps = {
  56. bordered: false,
  57. split: true,
  58. loading: false,
  59. layout: 'vertical',
  60. size: 'default',
  61. onRightClick: noop,
  62. onClick: noop,
  63. };
  64. renderEmpty = () => {
  65. const { emptyContent } = this.props;
  66. if (emptyContent) {
  67. return (<div className={`${cssClasses.PREFIX}-empty`}>{emptyContent}</div>);
  68. } else {
  69. return (
  70. <LocaleConsumer componentName="List">
  71. {
  72. (locale: Locale['List']) => (
  73. <div className={`${cssClasses.PREFIX}-empty`}>{locale.emptyText}</div>
  74. )
  75. }
  76. </LocaleConsumer>
  77. );
  78. }
  79. };
  80. wrapChildren(childrenList: React.ReactNode, children: React.ReactNode) {
  81. const { grid } = this.props;
  82. if (grid) {
  83. const rowProps = {};
  84. ['align', 'gutter', 'justify', 'type'].forEach(key => {
  85. if (key in grid) {
  86. rowProps[key] = grid[key];
  87. }
  88. });
  89. return (
  90. <Row type="flex" {...rowProps}>
  91. {childrenList ? childrenList : null}
  92. {children}
  93. </Row>
  94. );
  95. }
  96. return (
  97. <ul className={`${prefixCls}-items`}>
  98. {childrenList ? childrenList : null}
  99. {children}
  100. </ul>
  101. );
  102. }
  103. render() {
  104. const {
  105. style,
  106. className,
  107. header,
  108. loading,
  109. onRightClick,
  110. onClick,
  111. footer,
  112. layout,
  113. grid,
  114. size,
  115. split,
  116. loadMore,
  117. bordered,
  118. dataSource,
  119. renderItem,
  120. children
  121. } = this.props;
  122. const wrapperCls = cls(prefixCls, className, {
  123. [`${prefixCls}-flex`]: layout === 'horizontal',
  124. [`${prefixCls}-${size}`]: size,
  125. [`${prefixCls}-grid`]: grid,
  126. [`${prefixCls}-split`]: split,
  127. [`${prefixCls}-bordered`]: bordered,
  128. });
  129. let childrenList;
  130. if (dataSource && dataSource.length) {
  131. childrenList = [];
  132. const items = renderItem ? dataSource.map((item, index) => renderItem(item, index)) : [];
  133. React.Children.forEach(items as any, (child, index) => {
  134. const itemKey = child.key || `list-item-${index}`;
  135. childrenList.push(
  136. React.cloneElement(child, {
  137. key: itemKey,
  138. })
  139. );
  140. });
  141. } else if (!children && !loading) {
  142. childrenList = this.renderEmpty();
  143. }
  144. return (
  145. <div className={wrapperCls} style={style}>
  146. {header ? <div className={`${cssClasses.PREFIX}-header`}>{header}</div> : null}
  147. <ListContext.Provider
  148. value={{
  149. grid,
  150. onRightClick,
  151. onClick
  152. }}
  153. >
  154. <Spin spinning={loading} size="large">
  155. {this.wrapChildren(childrenList, children)}
  156. </Spin>
  157. </ListContext.Provider>
  158. {footer ? <div className={`${cssClasses.PREFIX}-footer`}>{footer}</div> : null}
  159. {loadMore ? loadMore : null}
  160. </div>
  161. );
  162. }
  163. }
  164. export default List;