useDynamicList 封装为组件实现 管理动态列表

本文介绍了如何在React项目中使用`ahooks`库的`useDynamicList`hook创建一个可动态增删的表单,展示了如何结合AntDesign组件如Form、Input和自定义图标实现列表管理功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

useDynamicList官网

Demo

Dynamic

import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
import { useDynamicList } from 'ahooks';
import { Button, Form, Input } from 'antd';
import React from 'react';

const formItemLayout = {
    labelCol: {
        xs: { span: 24 },
        sm: { span: 4 },
    },
    wrapperCol: {
        xs: { span: 24 },
        sm: { span: 20 },
    },
};

const formItemLayoutWithOutLabel = {
    wrapperCol: {
        xs: { span: 24, offset: 0 },
        sm: { span: 20, offset: 4 },
    },
};

export default (props) => {
    const { initialValue } = props
    const { list, remove, getKey, push } = useDynamicList(initialValue ? initialValue : ['']);

    const Row = (index: number, item: any) => (
        <div key={getKey(index)} style={{ position: 'relative' }}>
            <Form.Item
                {...(index === 0 ? formItemLayout : formItemLayoutWithOutLabel)}
                name={['users', index]}
                label={index === 0 ? '名称' : ''}
                initialValue={item}
            >
                <Input
                    style={{ width: 300 }}
                    placeholder="Please enter name"
                // onChange={(e) => replace(index, e.target.value)}
                // value={item}
                />

            </Form.Item>
            {list.length > 1 && (
                <MinusCircleOutlined
                    style={{ marginLeft: 8, position: 'absolute', top: '10px', left: '70%' }}
                    onClick={() => {
                        remove(index);
                    }}
                />
            )}
        </div>
    );

    return (
        <>
            {list.map((ele, index) => Row(index, ele))}
            <Form.Item>
                <Button type='dashed'
                    onClick={() => {
                        push('')
                    }}
                    icon={<PlusOutlined />}>
                    添加一行
                </Button>
            </Form.Item>
        </>
    );
};

index

import { Button, Form, } from 'antd';
import React, { useEffect, useState } from 'react';
import Dynamic from './Dynamic';

const formItemLayoutWithOutLabel = {
    wrapperCol: {
        xs: { span: 24, offset: 0 },
        sm: { span: 20, offset: 4 },
    },
};


export default () => {

    return (
        <>
            <Form
                autoComplete='off'
                {...formItemLayoutWithOutLabel}
                style={{ maxWidth: 600 }}
                onFinish={(val) => {
                    console.log('val', val)
                }}>
                <Dynamic initialValue={['Jarry', 'John']} />
                <Form.Item>
                    <Button htmlType='submit' type='primary'>提交</Button>
                </Form.Item>
            </Form>
        </>
    );
};

效果图

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值