React实现点击切换组件

实现如上组件

组件代码:

TypeScript 复制代码
import { SwapOutlined } from "@ant-design/icons"
import React, { useState } from "react"
import './index.less'

interface ISwitchTypeProps {
  onChange?: (val) => boolean
  activeKey?: string
  left: { key: string, text: string }
  right: { key: string, text: string }
}
const SwitchType = ({ onChange, left, right, activeKey }: ISwitchTypeProps) => {
  const [data, setData] = useState({
    left,
    right,
    activeKey:activeKey||left.key
  })
  const changeActiveData = () => {
    const activeKey = data.activeKey === left.key ? right.key : left.key
    const changeData = () => {
      setData({
        left: { ...data.right },
        right: { ...data.left },
        activeKey
      })
    }
    if (onChange&&onChange(activeKey)) {
      changeData()
    }
    if (!onChange) {
      changeData()
    }
  }

  const changeActive = () => {
    const activeKey = data.activeKey === left.key ? right.key : left.key
    const changeData = () => {
      setData({
        ...data,
        activeKey
      })
    }
    if (onChange&&onChange(activeKey)) {
      changeData()
    }
    if (!onChange) {
      changeData()
    }
  }
  return <div className="switch-type">
    <div className={data.activeKey === data.left.key ? 'type-active' : 'type-data'} onClick={changeActive} key={data.left.key}>{data.left.text}</div>
    <div className="change-icon" onClick={changeActiveData}><SwapOutlined /></div>
    <div className={data.activeKey === data.right.key ? 'type-active' : 'type-data'} onClick={changeActive} key={data.right.key}>{data.right.text}</div>
  </div>
}

export default SwitchType

index.less样式文件

TypeScript 复制代码
.switch-type {
  display: flex;
  align-items: center;
  color: #B9BCC1;

  .change-icon {
    border-radius: 2px;
    background-color: #F1F3F5;
    width: 24px;
    height: 24px;
    line-height: 24px;
    text-align: center;
    flex-shrink: 0;
    margin: 0 8px;
    color: #555961;
    cursor: pointer;
  }

  .type-data {
    cursor: pointer;
  }

  .type-active {
    color: #555961;
    .type-data
  }
}

若想要实现如上效果,点击不切换左右顺序,只切换选中项,把onClick事件统一成changeActive就可以了

TypeScript 复制代码
import { SwapOutlined } from "@ant-design/icons"
import React, { useState } from "react"
import './index.less'

interface ISwitchTypeProps {
  onChange?: (val) => boolean
  activeKey?: string
  left: { key: string, text: string }
  right: { key: string, text: string }
}
const SwitchType = ({ onChange, left, right, activeKey }: ISwitchTypeProps) => {
  const [data, setData] = useState({
    left,
    right,
    activeKey: activeKey || left.key
  })

  const changeActive = () => {
    const activeKey = data.activeKey === left.key ? right.key : left.key
    const changeData = () => {
      setData({
        ...data,
        activeKey
      })
    }
    if (onChange && onChange(activeKey)) {
      changeData()
    }
    if (!onChange) {
      changeData()
    }
  }
  return <div className="switch-type">
    <div className={data.activeKey === data.left.key ? 'type-active' : 'type-data'} onClick={changeActive} key={data.left.key}>{data.left.text}</div>
    <div className="change-icon" onClick={changeActive}><SwapOutlined /></div>
    <div className={data.activeKey === data.right.key ? 'type-active' : 'type-data'} onClick={changeActive} key={data.right.key}>{data.right.text}</div>
  </div>
}

export default SwitchType
相关推荐
cnsxjean12 分钟前
SpringBoot集成Minio实现上传凭证、分片上传、秒传和断点续传
java·前端·spring boot·分布式·后端·中间件·架构
ZL_56729 分钟前
uniapp中使用uni-forms实现表单管理,验证表单
前端·javascript·uni-app
沉浮yu大海1 小时前
Vue.js 组件开发:构建可重用且高效的 UI 块
前端·vue.js·ui
代码欢乐豆1 小时前
软件工程第13章小测
服务器·前端·数据库·软件工程
莘薪2 小时前
JQuery -- 第九课
前端·javascript·jquery
好青崧2 小时前
CSS 样式入门:属性全知晓
前端·css·tensorflow
光头程序员2 小时前
工程化开发谷歌插件到底有爽
前端·react·工程化·前端工程化·谷歌插件
蒜蓉大猩猩2 小时前
Vue.js --- Vue3中其他组合式API
前端·javascript·vue.js·前端框架·node.js·html
铅华尽2 小时前
前端---HTML(一)
前端
无限大.2 小时前
0基础学前端系列 -- 深入理解 HTML 布局
前端·html