React Ant Design 简单实现如何选中图片

效果:

代码:

定义的初始值和方法

javascript 复制代码
    const [selected, setSelected] = useState(0); // 表示当前选中的图片索引

    const handleClick = (index) => {
        if (selected === index) {
            setSelected(null); // 如果点击的是已选中的图片,则取消选中状态
        } else {
            setSelected(index); // 否则设置当前选中的图片索引为点击的图片索引
        }
    };

样式组件

javascript 复制代码
   <Form.Item
                                label="模板名称"
                                colon={false}
                                name="template"
                                rules={[
                                    {
                                        required: true,
                                        message: '此项为必填项,请填写后提交',
                                    },
                                ]}
                                labelAlign="top"
                                labelCol={{ span: 24 }}
                            >
                                <div style={{ display: 'flex' }}>
                                    {[0, 1].map((index) => (
                                        <div key={index} style={{ marginRight: '15px', position: 'relative' }}>
                                            <img
                                                src={template}
                                                alt="--"
                                                style={{ maxWidth: '100px', maxHeight: '100px', cursor: 'pointer' }}
                                                onClick={() => handleClick(index)}
                                            />
                                            {selected === index && ( // 如果当前图片被选中,则显示小对勾图标
                                                <CheckCircleFilled
                                                    style={{
                                                        position: 'absolute',
                                                        right: '5px',
                                                        bottom: '5px',
                                                        fontSize: '20px',
                                                        color: 'green',
                                                    }}
                                                />
                                            )}
                                        </div>
                                    ))}
                                </div>
                            </Form.Item>
相关推荐
漂流瓶jz25 分钟前
快速定位源码问题:SourceMap的生成/使用/文件格式与历史
前端·javascript·前端工程化
samroom26 分钟前
iframe实战:跨域通信与安全隔离
前端·安全
fury_12344 分钟前
vue3:数组的.includes方法怎么使用
前端·javascript·vue.js
weixin_405023371 小时前
包资源管理器NPM 使用
前端·npm·node.js
宁&沉沦1 小时前
Cursor 科技感的登录页面提示词
前端·javascript·vue.js
Dragonir1 小时前
React+Three.js 实现 Apple 2025 热成像 logo
前端·javascript·html·three.js·页面特效
peachSoda72 小时前
封装一个不同跳转方式的通用方法(跳转外部链接,跳转其他小程序,跳转半屏小程序)
前端·javascript·微信小程序·小程序
@PHARAOH2 小时前
HOW - 浏览器兼容(含 Safari)
前端·safari
undefined在掘金390413 小时前
flutter 仿商场_首页
前端