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 />
<BfListItem
className={styles['list-item']}
label={item.age}
arrow
onClick={() => {
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}
hideRemoveIcon
creatorButton={({ add }) => (
<Button
onClick={() => {
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>
<BfListItem
className={styles['list-item']}
label={item.age}
arrow
onClick={() => {
setCustomList((pre) => {
pre[index].age = Math.random() + '';
return [...pre];
});
}}
/>
</>
)}
</BfEditableList>
</>
);
};

API

属性名描述类型默认值
min最少条目,删除时如果当前数据条目少于该数则无法删除。默认1number1
max最多条目,新增时如果当前数据条目多于该数则无法新增或复制number--
creatorRecord新建一行的默认值Partial<any>--
data列表数据。若传入此prop,则此组件变成受控any[]--
creatorButton自定义新建一行按钮string | ((action: TActionFunc<any>) => any)'添加'
onDataChange列表数据变化回调(data: Partial<any>[]) => void--
childrenitem 渲染函数(item: any, index: number, id: string, action: TActionFunc<any>) => any--
hideRemoveIcon是否隐藏默认右上角删除icon。默认falsebooleanfalse
dumi10:24