List 列表
单个连续模块垂直排列,显示当前的内容、状态和可进行的操作。eg:联系人列表。 当你需要一个 infinite scroll 列表时,请使用 ListView。规则
一般由主要信息、主要操作、次要信息、次要操作组成。
主要信息和主要操作放在列表的左边,次要信息和次要操作放在列表的右边。
代码演示
import { List } from 'antd-mobile';
const Item = List.Item;
const Brief = Item.Brief;
class ListExample extends React.Component {
state = {
disabled: false,
}
render() {
return (<div>
<List renderHeader={() => 'Basic Style'} className="my-list">
<Item extra={'extra content'}>Title</Item>
</List>
<List renderHeader={() => 'Subtitle'} className="my-list">
<Item arrow="horizontal" multipleLine onClick={() => {}}>
Title <Brief>subtitle</Brief>
</Item>
<Item
arrow="horizontal"
multipleLine
onClick={() => {}}
platform="android"
>
ListItem (Android)<Brief>There may have water ripple effect of <br /> material if you set the click event.</Brief>
</Item>
<Item
arrow="horizontal"
thumb="https://zos.alipayobjects.com/rmsportal/dNuvNrtqUztHCwM.png"
multipleLine
onClick={() => {}}
>
Title <Brief>subtitle</Brief>
</Item>
</List>
<List renderHeader={() => 'Customized Right Side(Empty Content / Text / Image)'} className="my-list">
<Item>Title</Item>
<Item arrow="horizontal" onClick={() => {}}>Title</Item>
<Item extra="extra content" arrow="horizontal" onClick={() => {}}>Title</Item>
<Item extra="10:30" align="top" thumb="https://zos.alipayobjects.com/rmsportal/dNuvNrtqUztHCwM.png" multipleLine>
Title <Brief>subtitle</Brief>
</Item>
</List>
<List renderHeader={() => 'Align Vertical Center'} className="my-list">
<Item multipleLine extra="extra content">
Title <Brief>subtitle</Brief>
</Item>
</List>
<List renderHeader={() => 'Icon in the left'}>
<Item
thumb="https://zos.alipayobjects.com/rmsportal/dNuvNrtqUztHCwM.png"
arrow="horizontal"
onClick={() => {}}
>My wallet</Item>
<Item
thumb="https://zos.alipayobjects.com/rmsportal/UmbJMbWOejVOpxe.png"
onClick={() => {}}
arrow="horizontal"
>
My Cost Ratio
</Item>
</List>
<List renderHeader={() => 'Text Wrapping'} className="my-list">
<Item data-seed="logId">Single line,long text will be hidden with ellipsis;</Item>
<Item wrap>Multiple line,long text will wrap;Long Text Long Text Long Text Long Text Long Text Long Text</Item>
<Item extra="extra content" multipleLine align="top" wrap>
Multiple line and long text will wrap. Long Text Long Text Long Text
</Item>
<Item extra="no arrow" arrow="empty" className="spe" wrap>
In rare cases, the text of right side will wrap in the single line with long text. long text long text long text
</Item>
</List>
<List renderHeader={() => 'Other'} className="my-list">
<Item disabled={this.state.disabled} extra="" onClick={() => { console.log('click', this.state.disabled); this.setState({ disabled: true }); }}>Click to disable</Item>
<Item>
<select defaultValue="1">
<option value="1">Html select element</option>
<option value="2" disabled>Unable to select</option>
<option value="3">option 3</option>
</select>
</Item>
</List>
</div>);
}
}
ReactDOM.render(<ListExample />, mountNode);
Form Collection. (Use rc-form to validate form fields)
.my-list .spe .am-list-extra {
flex-basis: initial;
}
import { List, InputItem, Switch, Stepper, Range, Button } from 'antd-mobile';
import { createForm } from 'rc-form';
const Item = List.Item;
class BasicInput extends React.Component {
state = {
value: 1,
}
onSubmit = () => {
this.props.form.validateFields({ force: true }, (error) => {
if (!error) {
console.log(this.props.form.getFieldsValue());
} else {
alert('Validation failed');
}
});
}
onReset = () => {
this.props.form.resetFields();
}
validateAccount = (rule, value, callback) => {
if (value && value.length > 4) {
callback();
} else {
callback(new Error('At least four characters for account'));
}
}
render() {
const { getFieldProps, getFieldError } = this.props.form;
return (<form>
<List
renderHeader={() => 'Form Validation'}
renderFooter={() => getFieldError('account') && getFieldError('account').join(',')}
>
<InputItem
{...getFieldProps('account', {
// initialValue: 'little ant',
rules: [
{ required: true, message: 'Please input account' },
{ validator: this.validateAccount },
],
})}
clear
error={!!getFieldError('account')}
onErrorClick={() => {
alert(getFieldError('account').join('、'));
}}
placeholder="please input account"
>Account</InputItem>
<InputItem {...getFieldProps('password')} placeholder="please input password" type="password">
Password
</InputItem>
<Item
extra={<Switch {...getFieldProps('1', { initialValue: true, valuePropName: 'checked' })} />}
>Confirm Infomation</Item>
<Item><div style={{ padding: 7 }}><Range defaultValue={[20, 80]} /></div></Item>
<Item extra={<Stepper style={{ width: '100%', minWidth: '100px' }} showNumber size="small" defaultValue={20} />}>Number of Subscribers</Item>
<Item>
<Button type="primary" size="small" inline onClick={this.onSubmit}>Submit</Button>
<Button size="small" inline style={{ marginLeft: '2.5px' }} onClick={this.onReset}>Reset</Button>
</Item>
</List>
</form>);
}
}
const BasicInputWrapper = createForm()(BasicInput);
ReactDOM.render(<BasicInputWrapper />, mountNode);
API
List
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
renderHeader | list heder | (): void | 无 |
renderFooter | list 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 ,bottom | String | middle |
onClick | 点击事件的回调函数 | (): void | 无 |
error | 报错样式,右侧文字颜色变成橙色 | Boolean | false |
multipleLine | 多行 | Boolean | false |
wrap | 是否换行,默认情况下,文字超长会被隐藏, | Boolean | false |
activeStyle | 自定义active的样式 | Object | |
platform | 设定组件的平台特有样式, 可选值为 android , ios , 默认为 cross , 即组件会自动检测设备 UA 应用不同平台的样式 | String | 'cross' |