List 列表

单个连续模块垂直排列,显示当前的内容、状态和可进行的操作。eg:联系人列表。 当你需要一个 infinite scroll 列表时,请使用 ListView

规则

  • 一般由主要信息、主要操作、次要信息、次要操作组成。

  • 主要信息和主要操作放在列表的左边,次要信息和次要操作放在列表的右边。

代码演示

基本

  1. import { List } from 'antd-mobile';
  2. const Item = List.Item;
  3. const Brief = Item.Brief;
  4. class ListExample extends React.Component {
  5. state = {
  6. disabled: false,
  7. }
  8. render() {
  9. return (<div>
  10. <List renderHeader={() => 'Basic Style'} className="my-list">
  11. <Item extra={'extra content'}>Title</Item>
  12. </List>
  13. <List renderHeader={() => 'Subtitle'} className="my-list">
  14. <Item arrow="horizontal" multipleLine onClick={() => {}}>
  15. Title <Brief>subtitle</Brief>
  16. </Item>
  17. <Item
  18. arrow="horizontal"
  19. multipleLine
  20. onClick={() => {}}
  21. platform="android"
  22. >
  23. ListItem Android)<Brief>There may have water ripple effect of <br /> material if you set the click event.</Brief>
  24. </Item>
  25. <Item
  26. arrow="horizontal"
  27. thumb="https://zos.alipayobjects.com/rmsportal/dNuvNrtqUztHCwM.png"
  28. multipleLine
  29. onClick={() => {}}
  30. >
  31. Title <Brief>subtitle</Brief>
  32. </Item>
  33. </List>
  34. <List renderHeader={() => 'Customized Right Side(Empty Content / Text / Image)'} className="my-list">
  35. <Item>Title</Item>
  36. <Item arrow="horizontal" onClick={() => {}}>Title</Item>
  37. <Item extra="extra content" arrow="horizontal" onClick={() => {}}>Title</Item>
  38. <Item extra="10:30" align="top" thumb="https://zos.alipayobjects.com/rmsportal/dNuvNrtqUztHCwM.png" multipleLine>
  39. Title <Brief>subtitle</Brief>
  40. </Item>
  41. </List>
  42. <List renderHeader={() => 'Align Vertical Center'} className="my-list">
  43. <Item multipleLine extra="extra content">
  44. Title <Brief>subtitle</Brief>
  45. </Item>
  46. </List>
  47. <List renderHeader={() => 'Icon in the left'}>
  48. <Item
  49. thumb="https://zos.alipayobjects.com/rmsportal/dNuvNrtqUztHCwM.png"
  50. arrow="horizontal"
  51. onClick={() => {}}
  52. >My wallet</Item>
  53. <Item
  54. thumb="https://zos.alipayobjects.com/rmsportal/UmbJMbWOejVOpxe.png"
  55. onClick={() => {}}
  56. arrow="horizontal"
  57. >
  58. My Cost Ratio
  59. </Item>
  60. </List>
  61. <List renderHeader={() => 'Text Wrapping'} className="my-list">
  62. <Item data-seed="logId">Single linelong text will be hidden with ellipsis;</Item>
  63. <Item wrap>Multiple linelong text will wrapLong Text Long Text Long Text Long Text Long Text Long Text</Item>
  64. <Item extra="extra content" multipleLine align="top" wrap>
  65. Multiple line and long text will wrap. Long Text Long Text Long Text
  66. </Item>
  67. <Item extra="no arrow" arrow="empty" className="spe" wrap>
  68. In rare cases, the text of right side will wrap in the single line with long text. long text long text long text
  69. </Item>
  70. </List>
  71. <List renderHeader={() => 'Other'} className="my-list">
  72. <Item disabled={this.state.disabled} extra="" onClick={() => { console.log('click', this.state.disabled); this.setState({ disabled: true }); }}>Click to disable</Item>
  73. <Item>
  74. <select defaultValue="1">
  75. <option value="1">Html select element</option>
  76. <option value="2" disabled>Unable to select</option>
  77. <option value="3">option 3</option>
  78. </select>
  79. </Item>
  80. </List>
  81. </div>);
  82. }
  83. }
  84. ReactDOM.render(<ListExample />, mountNode);
  1. .my-list .spe .am-list-extra {
  2. flex-basis: initial;
  3. }

可输入列表

Form Collection. (Use rc-form to validate form fields)

  1. import { List, InputItem, Switch, Stepper, Range, Button } from 'antd-mobile';
  2. import { createForm } from 'rc-form';
  3. const Item = List.Item;
  4. class BasicInput extends React.Component {
  5. state = {
  6. value: 1,
  7. }
  8. onSubmit = () => {
  9. this.props.form.validateFields({ force: true }, (error) => {
  10. if (!error) {
  11. console.log(this.props.form.getFieldsValue());
  12. } else {
  13. alert('Validation failed');
  14. }
  15. });
  16. }
  17. onReset = () => {
  18. this.props.form.resetFields();
  19. }
  20. validateAccount = (rule, value, callback) => {
  21. if (value && value.length > 4) {
  22. callback();
  23. } else {
  24. callback(new Error('At least four characters for account'));
  25. }
  26. }
  27. render() {
  28. const { getFieldProps, getFieldError } = this.props.form;
  29. return (<form>
  30. <List
  31. renderHeader={() => 'Form Validation'}
  32. renderFooter={() => getFieldError('account') && getFieldError('account').join(',')}
  33. >
  34. <InputItem
  35. {...getFieldProps('account', {
  36. // initialValue: 'little ant',
  37. rules: [
  38. { required: true, message: 'Please input account' },
  39. { validator: this.validateAccount },
  40. ],
  41. })}
  42. clear
  43. error={!!getFieldError('account')}
  44. onErrorClick={() => {
  45. alert(getFieldError('account').join('、'));
  46. }}
  47. placeholder="please input account"
  48. >Account</InputItem>
  49. <InputItem {...getFieldProps('password')} placeholder="please input password" type="password">
  50. Password
  51. </InputItem>
  52. <Item
  53. extra={<Switch {...getFieldProps('1', { initialValue: true, valuePropName: 'checked' })} />}
  54. >Confirm Infomation</Item>
  55. <Item><div style={{ padding: 7 }}><Range defaultValue={[20, 80]} /></div></Item>
  56. <Item extra={<Stepper style={{ width: '100%', minWidth: '100px' }} showNumber size="small" defaultValue={20} />}>Number of Subscribers</Item>
  57. <Item>
  58. <Button type="primary" size="small" inline onClick={this.onSubmit}>Submit</Button>
  59. <Button size="small" inline style={{ marginLeft: '2.5px' }} onClick={this.onReset}>Reset</Button>
  60. </Item>
  61. </List>
  62. </form>);
  63. }
  64. }
  65. const BasicInputWrapper = createForm()(BasicInput);
  66. ReactDOM.render(<BasicInputWrapper />, mountNode);

List列表 - 图1

API

List

属性说明类型默认值
renderHeaderlist heder(): void
renderFooterlist footer(): void

List.Item

属性说明类型默认值
thumb缩略图(当为 string 类型时作为 img src)String/React.Element
extra右边内容String/React.Element
arrow箭头方向(右,上,下), 可选horizontal,up,down,empty,如果是empty则存在对应的dom,但是不显示String
align子元素垂直对齐,可选top,middle,bottomStringmiddle
onClick点击事件的回调函数(): void
error报错样式,右侧文字颜色变成橙色Booleanfalse
multipleLine多行Booleanfalse
wrap是否换行,默认情况下,文字超长会被隐藏,Booleanfalse
activeStyle自定义active的样式Object
platform设定组件的平台特有样式, 可选值为 android, ios, 默认为 cross, 即组件会自动检测设备 UA 应用不同平台的样式String'cross'

List.Item.Brief

辅助说明