React实现点击切换组件

实现如上组件

组件代码:

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样式文件

.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就可以了

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React中,你可以通过改变组件的属性来切换组件。这可以通过以下步骤完成: 1. 在父组件中定义一个状态,用来保存要传递给子组件的属性值。 2. 在父组件中定义一个函数,该函数将根据需要更改状态中的属性值。 3. 将该函数作为属性传递给子组件。 4. 在子组件中,使用父组件传递的属性来渲染组件。 5. 当需要更改属性时,调用父组件传递的函数来更新状态中的属性值。 以下是一个示例代码: ```javascript // 父组件 import React, { useState } from 'react'; import ChildComponent from './ChildComponent'; function ParentComponent() { const [text, setText] = useState("Hello World"); const changeText = () => { if (text === "Hello World") { setText("Hello React"); } else { setText("Hello World"); } } return ( <div> <ChildComponent text={text} onChange={changeText} /> </div> ); } // 子组件 import React from 'react'; function ChildComponent(props) { return ( <div> <p>{props.text}</p> <button onClick={props.onChange}>Change Text</button> </div> ); } ``` 在这个示例中,父组件 `ParentComponent` 保存了一个状态 `text`,并定义了一个函数 `changeText`,该函数将根据需要更改状态中的 `text` 属性值。父组件将 `text` 属性和 `changeText` 函数作为属性传递给子组件 `ChildComponent`。子组件使用这些属性来渲染组件,并在按钮被点击时调用父组件传递的 `onChange` 函数来更新状态中的 `text` 属性值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值