List列表

通用列表。

何时使用

最基础的列表展示,可承载文字、列表、图片、段落,常用于后台数据展示页面。

代码演示

List 列表 - 图1

简单列表

列表拥有大、中、小三种尺寸。

通过设置 sizelarge small 分别把按钮设为大、小尺寸。若不设置 size,则尺寸为中。

可通过设置 headerfooter,来自定义列表头部和尾部。

  1. import { List } from 'choerodon-ui';
  2. const data = [
  3. 'Racing car sprays burning fuel into crowd.',
  4. 'Japanese princess to wed commoner.',
  5. 'Australian walks 100km after outback crash.',
  6. 'Man charged over missing wedding girl.',
  7. 'Los Angeles battles huge wildfires.',
  8. ];
  9. ReactDOM.render(
  10. <div>
  11. <h3 style={{ marginBottom: 16 }}>Default Size</h3>
  12. <List
  13. header={<div>Header</div>}
  14. footer={<div>Footer</div>}
  15. bordered
  16. dataSource={data}
  17. renderItem={item => <List.Item>{item}</List.Item>}
  18. />
  19. <h3 style={{ margin: '16px 0' }}>Small Size</h3>
  20. <List
  21. size="small"
  22. header={<div>Header</div>}
  23. footer={<div>Footer</div>}
  24. bordered
  25. dataSource={data}
  26. renderItem={item => <List.Item>{item}</List.Item>}
  27. />
  28. <h3 style={{ margin: '16px 0' }}>Large Size</h3>
  29. <List
  30. size="large"
  31. header={<div>Header</div>}
  32. footer={<div>Footer</div>}
  33. bordered
  34. dataSource={data}
  35. renderItem={item => <List.Item>{item}</List.Item>}
  36. />
  37. </div>,
  38. mountNode,
  39. );

List 列表 - 图2

基础列表

基础列表。

  1. import { List, Avatar } from 'choerodon-ui';
  2. const data = [
  3. {
  4. title: 'Choerodon UI Title 1',
  5. },
  6. {
  7. title: 'Choerodon UI Title 2',
  8. },
  9. {
  10. title: 'Choerodon UI Title 3',
  11. },
  12. {
  13. title: 'Choerodon UI Title 4',
  14. },
  15. ];
  16. ReactDOM.render(
  17. <List
  18. itemLayout="horizontal"
  19. dataSource={data}
  20. renderItem={item => (
  21. <List.Item>
  22. <List.Item.Meta
  23. avatar={<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />}
  24. title={<a href="https://choerodon.github.io/choerodon-ui/">{item.title}</a>}
  25. description="Choerodon"
  26. />
  27. </List.Item>
  28. )}
  29. />,
  30. mountNode,
  31. );

List 列表 - 图3

加载更多

可通过 loadMore 属性实现加载更多功能。

  1. import { List, Avatar, Button, Spin } from 'choerodon-ui';
  2. import reqwest from 'reqwest';
  3. const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo';
  4. class LoadMoreList extends React.Component {
  5. state = {
  6. loading: true,
  7. loadingMore: false,
  8. showLoadingMore: true,
  9. data: [],
  10. };
  11. componentDidMount() {
  12. this.getData(res => {
  13. this.setState({
  14. loading: false,
  15. data: res.results,
  16. });
  17. });
  18. }
  19. getData = callback => {
  20. reqwest({
  21. url: fakeDataUrl,
  22. type: 'json',
  23. method: 'get',
  24. contentType: 'application/json',
  25. success: res => {
  26. callback(res);
  27. },
  28. });
  29. };
  30. onLoadMore = () => {
  31. this.setState({
  32. loadingMore: true,
  33. });
  34. this.getData(res => {
  35. const data = this.state.data.concat(res.results);
  36. this.setState(
  37. {
  38. data,
  39. loadingMore: false,
  40. },
  41. () => {
  42. // Resetting window's offsetTop so as to display react-virtualized demo underfloor.
  43. // In real scene, you can using public method of react-virtualized:
  44. // https://stackoverflow.com/questions/46700726/how-to-use-public-method-updateposition-of-react-virtualized
  45. window.dispatchEvent(new Event('resize'));
  46. },
  47. );
  48. });
  49. };
  50. render() {
  51. const { loading, loadingMore, showLoadingMore, data } = this.state;
  52. const loadMore = showLoadingMore ? (
  53. <div style={{ textAlign: 'center', marginTop: 12, height: 32, lineHeight: '32px' }}>
  54. {loadingMore && <Spin />}
  55. {!loadingMore && <Button onClick={this.onLoadMore}>loading more</Button>}
  56. </div>
  57. ) : null;
  58. return (
  59. <List
  60. className="demo-loadmore-list"
  61. loading={loading}
  62. itemLayout="horizontal"
  63. loadMore={loadMore}
  64. dataSource={data}
  65. renderItem={item => (
  66. <List.Item
  67. key={item.name.last}
  68. actions={[<a key="edit">edit</a>, <a key="more">more</a>]}
  69. >
  70. <List.Item.Meta
  71. avatar={
  72. <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
  73. }
  74. title={<a href="https://choerodon.github.io/choerodon-ui/">{item.name.last}</a>}
  75. description="Choerodon"
  76. />
  77. <div>content</div>
  78. </List.Item>
  79. )}
  80. />
  81. );
  82. }
  83. }
  84. ReactDOM.render(<LoadMoreList />, mountNode);
  1. .demo-loadmore-list {
  2. min-height: 350px;
  3. }

List 列表 - 图4

竖排列表样式

通过设置 itemLayout 属性为 vertical 可实现竖排列表样式。

  1. import { List, Avatar, Icon } from 'choerodon-ui';
  2. const listData = [];
  3. for (let i = 0; i < 5; i++) {
  4. listData.push({
  5. href: 'https://choerodon.github.io/choerodon-ui/',
  6. title: `choerodon ui part ${i}`,
  7. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
  8. description:
  9. 'Choerodon UI, An enterprise-class UI design language and React-based implementation.',
  10. content:
  11. 'We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
  12. });
  13. }
  14. const pagination = {
  15. pageSize: 10,
  16. current: 1,
  17. total: listData.length,
  18. onChange: () => {},
  19. };
  20. const IconText = ({ type, text }) => (
  21. <span>
  22. <Icon type={type} style={{ marginRight: 8 }} />
  23. {text}
  24. </span>
  25. );
  26. ReactDOM.render(
  27. <List
  28. itemLayout="vertical"
  29. size="large"
  30. pagination={pagination}
  31. dataSource={listData}
  32. renderItem={item => (
  33. <List.Item
  34. key={item.title}
  35. actions={[
  36. <IconText key="star-o" type="star-o" text="156" />,
  37. <IconText key="like-o" type="like-o" text="156" />,
  38. <IconText key="message" type="message" text="2" />,
  39. ]}
  40. extra={
  41. <img
  42. width={272}
  43. alt="logo"
  44. src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png"
  45. />
  46. }
  47. >
  48. <List.Item.Meta
  49. avatar={<Avatar src={item.avatar} />}
  50. title={<a href={item.href}>{item.title}</a>}
  51. description={item.description}
  52. />
  53. {item.content}
  54. </List.Item>
  55. )}
  56. />,
  57. mountNode,
  58. );

List 列表 - 图5

栅格列表

可以通过设置 Listgrid 属性来实现栅格列表,column 可设置期望显示的列数。

  1. import { List, Card } from 'choerodon-ui';
  2. const data = [
  3. {
  4. title: 'Title 1',
  5. },
  6. {
  7. title: 'Title 2',
  8. },
  9. {
  10. title: 'Title 3',
  11. },
  12. {
  13. title: 'Title 4',
  14. },
  15. ];
  16. ReactDOM.render(
  17. <List
  18. grid={{ gutter: 16, column: 4 }}
  19. dataSource={data}
  20. renderItem={item => (
  21. <List.Item>
  22. <Card title={item.title}>Card content</Card>
  23. </List.Item>
  24. )}
  25. />,
  26. mountNode);

List 列表 - 图6

响应式的栅格列表

响应式的栅格列表。尺寸与 Layout Grid 保持一致。

  1. import { List, Card } from 'choerodon-ui';
  2. const data = [
  3. {
  4. title: 'Title 1',
  5. },
  6. {
  7. title: 'Title 2',
  8. },
  9. {
  10. title: 'Title 3',
  11. },
  12. {
  13. title: 'Title 4',
  14. },
  15. {
  16. title: 'Title 5',
  17. },
  18. {
  19. title: 'Title 6',
  20. },
  21. ];
  22. ReactDOM.render(
  23. <List
  24. grid={{ gutter: 16, xs: 1, sm: 2, md: 4, lg: 4, xl: 6, xxl: 3 }}
  25. dataSource={data}
  26. renderItem={item => (
  27. <List.Item>
  28. <Card title={item.title}>Card content</Card>
  29. </List.Item>
  30. )}
  31. />,
  32. mountNode);

List 列表 - 图7

滚动加载

结合 react-infinite-scroller 实现滚动自动加载列表。

  1. import { List, message, Avatar, Spin } from 'choerodon-ui';
  2. import reqwest from 'reqwest';
  3. import InfiniteScroll from 'react-infinite-scroller';
  4. const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo';
  5. class InfiniteListExample extends React.Component {
  6. state = {
  7. data: [],
  8. loading: false,
  9. hasMore: true,
  10. }
  11. getData = (callback) => {
  12. reqwest({
  13. url: fakeDataUrl,
  14. type: 'json',
  15. method: 'get',
  16. contentType: 'application/json',
  17. success: (res) => {
  18. callback(res);
  19. },
  20. });
  21. }
  22. componentWillMount() {
  23. this.getData((res) => {
  24. this.setState({
  25. data: res.results,
  26. });
  27. });
  28. }
  29. handleInfiniteOnLoad = () => {
  30. let data = this.state.data;
  31. this.setState({
  32. loading: true,
  33. });
  34. if (data.length > 14) {
  35. message.warning('Infinite List loaded all');
  36. this.setState({
  37. hasMore: false,
  38. loading: false,
  39. });
  40. return;
  41. }
  42. this.getData((res) => {
  43. data = data.concat(res.results);
  44. this.setState({
  45. data,
  46. loading: false,
  47. });
  48. });
  49. }
  50. render() {
  51. return (
  52. <div className="demo-infinite-container">
  53. <InfiniteScroll
  54. initialLoad={false}
  55. pageStart={0}
  56. loadMore={this.handleInfiniteOnLoad}
  57. hasMore={!this.state.loading && this.state.hasMore}
  58. useWindow={false}
  59. >
  60. <List
  61. dataSource={this.state.data}
  62. renderItem={item => (
  63. <List.Item key={item.id}>
  64. <List.Item.Meta
  65. avatar={<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />}
  66. title={<a href="https://choerodon.github.io/choerodon-ui/">{item.name.last}</a>}
  67. description={item.email}
  68. />
  69. <div>Content</div>
  70. </List.Item>
  71. )}
  72. >
  73. {this.state.loading && this.state.hasMore && <Spin className="demo-loading" />}
  74. </List>
  75. </InfiniteScroll>
  76. </div>
  77. );
  78. }
  79. }
  80. ReactDOM.render(<InfiniteListExample />, mountNode);
  1. .demo-infinite-container {
  2. border: 1px solid #e8e8e8;
  3. border-radius: 4px;
  4. overflow: auto;
  5. padding: 8px 24px;
  6. height: 300px;
  7. }
  8. .demo-loading {
  9. position: absolute;
  10. bottom: -40px;
  11. left: 50%;
  12. }

List 列表 - 图8

滚动加载无限长列表

结合 react-virtualized 实现滚动加载无限长列表,带有虚拟化(virtualization)功能,能够提高数据量大时候长列表的性能。

virtualized 是在大数据列表中应用的一种技术,主要是为了减少不可见区域不必要的渲染从而提高性能,特别是数据量在成千上万条效果尤为明显。了解更多

  1. import { List, message, Avatar, Spin } from 'choerodon-ui';
  2. import reqwest from 'reqwest';
  3. import WindowScroller from 'react-virtualized/dist/commonjs/WindowScroller';
  4. import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
  5. import VList from 'react-virtualized/dist/commonjs/List';
  6. import InfiniteLoader from 'react-virtualized/dist/commonjs/InfiniteLoader';
  7. const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo';
  8. class VirtualizedExample extends React.Component {
  9. state = {
  10. data: [],
  11. loading: false,
  12. }
  13. loadedRowsMap = {}
  14. getData = (callback) => {
  15. reqwest({
  16. url: fakeDataUrl,
  17. type: 'json',
  18. method: 'get',
  19. contentType: 'application/json',
  20. success: (res) => {
  21. callback(res);
  22. },
  23. });
  24. }
  25. componentWillMount() {
  26. this.getData((res) => {
  27. this.setState({
  28. data: res.results,
  29. });
  30. });
  31. }
  32. handleInfiniteOnLoad = ({ startIndex, stopIndex }) => {
  33. let data = this.state.data;
  34. this.setState({
  35. loading: true,
  36. });
  37. for (let i = startIndex; i <= stopIndex; i++) {
  38. // 1 means loading
  39. this.loadedRowsMap[i] = 1;
  40. }
  41. if (data.length > 19) {
  42. message.warning('Virtualized List loaded all');
  43. this.setState({
  44. loading: false,
  45. });
  46. return;
  47. }
  48. this.getData((res) => {
  49. data = data.concat(res.results);
  50. this.setState({
  51. data,
  52. loading: false,
  53. });
  54. });
  55. }
  56. isRowLoaded = ({ index }) => {
  57. return !!this.loadedRowsMap[index];
  58. }
  59. renderItem = ({ index, key, style }) => {
  60. const { data } = this.state;
  61. const item = data[index];
  62. return (
  63. <List.Item key={key} style={style}>
  64. <List.Item.Meta
  65. avatar={<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />}
  66. title={<a href="https://choerodon.github.io/choerodon-ui/">{item.name.last}</a>}
  67. description={item.email}
  68. />
  69. <div>Content</div>
  70. </List.Item>
  71. );
  72. }
  73. render() {
  74. const { data } = this.state;
  75. const vlist = ({ height, isScrolling, onChildScroll, scrollTop, onRowsRendered, width }) => (
  76. <VList
  77. autoHeight
  78. height={height}
  79. isScrolling={isScrolling}
  80. onScroll={onChildScroll}
  81. overscanRowCount={2}
  82. rowCount={data.length}
  83. rowHeight={73}
  84. rowRenderer={this.renderItem}
  85. onRowsRendered={onRowsRendered}
  86. scrollTop={scrollTop}
  87. width={width}
  88. />
  89. );
  90. const autoSize = ({ height, isScrolling, onChildScroll, scrollTop, onRowsRendered }) => (
  91. <AutoSizer disableHeight>
  92. {({ width }) => vlist({ height, isScrolling, onChildScroll, scrollTop, onRowsRendered, width })}
  93. </AutoSizer>
  94. );
  95. const infiniteLoader = ({ height, isScrolling, onChildScroll, scrollTop }) => (
  96. <InfiniteLoader
  97. isRowLoaded={this.isRowLoaded}
  98. loadMoreRows={this.handleInfiniteOnLoad}
  99. rowCount={data.length}
  100. >
  101. {({ onRowsRendered }) => autoSize({ height, isScrolling, onChildScroll, scrollTop, onRowsRendered })}
  102. </InfiniteLoader>
  103. );
  104. return (
  105. <List>
  106. {
  107. data.length > 0 && (
  108. <WindowScroller scrollElement={null}>
  109. {infiniteLoader}
  110. </WindowScroller>
  111. )
  112. }
  113. {this.state.loading && <Spin className="demo-loading" />}
  114. </List>
  115. );
  116. }
  117. }
  118. ReactDOM.render(<VirtualizedExample />, mountNode);
  1. .demo-loading {
  2. position: absolute;
  3. bottom: -40px;
  4. left: 50%;
  5. }

API

List

参数说明类型默认值
bordered是否展示边框booleanfalse
footer列表底部string|ReactNode-
empty当数据源为空时显示的内容string|ReactNode-
grid列表栅格配置object-
header列表头部string|ReactNode-
itemLayout设置 List.Item 布局, 设置成 vertical 则竖直样式显示, 默认横排string-
loading当卡片内容还在加载中时,可以用 loading 展示一个占位boolean|objectfalse
loadMore加载更多string|ReactNode-
pagination对应的 pagination 配置, 设置 false 不显示boolean|objectfalse
sizelist 的尺寸default | large | smalldefault
split是否展示分割线booleantrue

List grid props

参数说明类型默认值
column列数number-
gutter栅格间隔number0
xs<576px 展示的列数number-
sm≥576px 展示的列数number-
md≥768px 展示的列数number-
lg≥992px 展示的列数number-
xl≥1200px 展示的列数number-
xxl≥1600px 展示的列数number-

List.Item

参数说明类型默认值
actions列表操作组,根据 itemLayout 的不同, 位置在卡片底部或者最右侧Array<ReactNode>-
extra额外内容, 通常用在 itemLayoutvertical 的情况下, 展示右侧内容; horizontal 展示在列表元素最右侧string|ReactNode-

List.Item.Meta

参数说明类型默认值
avatar列表元素的图标ReactNode-
description列表元素的描述内容string|ReactNode-
title列表元素的标题string|ReactNode-