多个按钮点击切换

点击新的按钮 背景颜色才会发生改变

在这里插入图片描述
JS

import React, { useState } from 'react'
import styles from './style.less'
import { Button } from 'antd';
import { PauseOutlined, CaretRightOutlined } from '@ant-design/icons';
const Index = () =>
{
    const [buttonData, setButtonData] = useState([
        {
            id: '01', nameCurrent: '模拟暂停', Unselected: '动态模拟', done: false, changColor: false
        },
        {
            id: '02', nameCurrent: '模拟暂停', Unselected: '动态模拟', done: false, changColor: false
        },
        {
            id: '03', nameCurrent: '模拟暂停', Unselected: '动态模拟', done: false, changColor: false
        },
    ])
    // 重置按钮 关闭按钮 以及颜色
    const reset = () =>
    {
        let newBtn = buttonData.map(item =>
        {
            return { ...item, done: false, changColor: false }
        })
        setButtonData(newBtn)
    }
    const isShowClick = (data, done) =>
    {
        let newBtn = buttonData.map(item =>
        {
            if (item.id === data.id)
            {
                return { ...item, done: !done, changColor: true }
            } else
            {
                return { ...item, done: false, changColor: false }
            }
        })

        setButtonData(newBtn)
    }

    return (
        <div className={styles.overall}>
            <div className={styles.core}>
                <div className={styles.head}>
                    <Button onClick={reset} className={styles.reset} type="primary">重置</Button>
                </div>
            </div>
            {buttonData.map(data => (
                <Button key={data.id} onClick={() => { isShowClick(data, data.done) }} className={styles.btnCenter} type="primary" style={data.changColor ? { borderColor: 'rgba(111, 252, 51, 1)', color: 'rgba(111, 252, 51, 1)' } : { borderColor: 'rgba(54, 153, 255, 1)', color: 'rgba(54, 153, 255, 1)' }} ghost>
                    {data.done ? data.nameCurrent : data.Unselected} {data.done ? <PauseOutlined /> : <CaretRightOutlined />}
                </Button>
            ))}
        </div>
    )
}

export default Index
.overall {
  position: relative;
  width: 1000px;
  height: 880px;
  border-radius: 4px;
  box-sizing: border-box;
  background-color: pink;
  overflow: hidden;
}

.head {
  display: flex;
  align-items: center;
  width: 100%;
  height: 60px;
  //   background: linear-gradient(0deg, rgba(27, 71, 143, .9) 0%, rgba(21, 31, 45, .9) 100%);
  border-radius: 4px;
  position: absolute;
  top: 100px;

  .reset {
    display: flex;
    align-items: center;
    position: absolute;
    width: 90px;
    height: 38px;
    background: #3699FF;
    font-size: 22px;
    color: #fff;
    font-weight: 500;
    border-radius: 4px;
    right: 0;
    margin-right: 20px;
  }
}



.btnCenter {
  display: flex;
  align-items: center;
  margin: 238px 40px 0 auto;
  width: 124px;
  height: 28px;
  border-radius: 4px;
  font-weight: 400;
  font-size: 18px;
  cursor: pointer !important;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

臧小川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值