EditableList
EditableList 可新增、删除的动态列表
代码示例
import { BfEditableList, BfListItem } from '@bud-fe/react-taro-ui';import { Button, Text, View } from '@tarojs/components';import React, { useState } from 'react';import IconFont from '../../components/icon-font';import styles from './index.module.less';type TItem = {name: string;age: string;};export default () => {const [list, setList] = useState<TItem[]>([{ name: '萨卡', age: '22' },{ name: '史密斯罗', age: '22' },]);const [customList, setCustomList] = useState<TItem[]>([{ name: '厄德高', age: '23' },{ name: '热苏斯', age: '25' },]);return (<><View className="group-title">基础用法</View><BfEditableList<TItem>creatorRecord={{ name: '张三', age: '30' }}max={3}creatorButton="添加人员"data={list}onDataChange={(data) => setList(data)}>{(item: TItem, index: number) => (<><BfListItem className={styles['list-item']} label={item.name} arrow /><BfListItemclassName={styles['list-item']}label={item.age}arrowonClick={() => {setList((pre) => {pre[index].age = Math.random() + '';return [...pre];});}}/></>)}</BfEditableList><View className="group-title">自定义删除、添加按钮</View><BfEditableList<TItem>creatorRecord={{ name: '李四', age: '20' }}max={4}creatorButton="添加人员"data={customList}hideRemoveIconcreatorButton={({ add }) => (<ButtononClick={() => {add({ name: 'customName', age: 100 });}}>custom add</Button>)}onDataChange={(data) => setCustomList(data)}>{(item: TItem, index: number, _id: string, { remove }) => (<><View className={styles['custom-list-item']}><IconFont type="icon-shanshu" className={styles['icon-delete']} onClick={() => remove(_id)} /><Text>{item.name}</Text></View><BfListItemclassName={styles['list-item']}label={item.age}arrowonClick={() => {setCustomList((pre) => {pre[index].age = Math.random() + '';return [...pre];});}}/></>)}</BfEditableList></>);};
API
属性名 | 描述 | 类型 | 默认值 |
---|---|---|---|
min | 最少条目,删除时如果当前数据条目少于该数则无法删除。默认1 | number | 1 |
max | 最多条目,新增时如果当前数据条目多于该数则无法新增或复制 | number | -- |
creatorRecord | 新建一行的默认值 | Partial<any> | -- |
data | 列表数据。若传入此prop,则此组件变成受控 | any[] | -- |
creatorButton | 自定义新建一行按钮 | string | ((action: TActionFunc<any>) => any) | '添加' |
onDataChange | 列表数据变化回调 | (data: Partial<any>[]) => void | -- |
children | item 渲染函数 | (item: any, index: number, id: string, action: TActionFunc<any>) => any | -- |
hideRemoveIcon | 是否隐藏默认右上角删除icon。默认false | boolean | false |