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
相关推荐
菌菇汤12 分钟前
uni-app实现单选,多选也能搜索,勾选,选择,回显
前端·javascript·vue.js·微信小程序·uni-app·app
Ramos丶20 分钟前
【ABAP】 从无到有 新建一个Webdynpro程序
java·前端·javascript
摸鱼仙人~30 分钟前
如何创建基于 TypeScript 的 React 项目
javascript·react.js·typescript
qq_4116719839 分钟前
vue3 的模板引用ref和$parent
前端·javascript·vue.js
清幽竹客2 小时前
vue-37(模拟依赖项进行隔离测试)
前端·vue.js
vvilkim2 小时前
Nuxt.js 页面与布局系统深度解析:构建高效 Vue 应用的关键
前端·javascript·vue.js
滿2 小时前
Vue3 父子组件表单滚动到校验错误的位置实现方法
前端·javascript·vue.js
夏梦春蝉3 小时前
ES6从入门到精通:模块化
前端·ecmascript·es6
拓端研究室4 小时前
视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
前端·算法
工一木子5 小时前
URL时间戳参数深度解析:缓存破坏与前端优化的前世今生
前端·缓存