tab页签-代码部分

tab页签-代码部分

child
import React,{useEffect, useState} from 'react'

const Child = (props: any) => {
  const [loading,setLoading] = useState(true)
  useEffect(()=>{
    setTimeout(()=>{
      setLoading(false)
    },2000)
  },[])
  return (
    <div>
      {loading ? 'loading...': '我加载咯'}
    </div>
  )
}

export default Child
tab-item
import React, { useEffect, useState } from 'react'

const TabItem = (props: any) => {
  const {index,animate} = props
  const [visible,setVisible] = useState(false)
  useEffect(()=>{
    if(props.visible) {
      setVisible(true)
    }
  },[props.visible])
  let leftIndex = animate ? index*100+'%' : ''
  return (
    <div className={"tab-div__children " + (props.visible ? 'show':'')} style={{left: leftIndex}}>
      { visible && props.children}
    </div>
  )
}

export default TabItem
tab
import React, { useState } from 'react'
import './index.scss'

const Tab = (props: any) => {
  const {topBars,onChangeKeys,activeKey,animate} = props
  const [index,setIndex] = useState(0)
  const liArr = topBars.map((item: any,index: number) => {
    return <li key={index} className={item===activeKey ? "active": ""} onClick={() => {onChangeKeys(item);setIndex(index)}}>{item}</li>
  })

  let mapChild = props.children.map((item: any,index: number) => 
    {
      return React.cloneElement(item,{...item.props,visible:activeKey===item.key,index,animate: props.animate},item.props.children)
    }
  )
  const marginLeft = animate ? (-index*100+'%'): ''
  return (
    <div className="tab">
      <ul>
        {liArr}
      </ul>
      <div className={`tab-div ${animate? 'animate':''}`} style={{marginLeft}} >
        {
          mapChild
        }
      </div>
    </div>
  )
}

export default Tab
Index
import React,{useState} from 'react'
import Tab from './tab'
import TabItem from './tab-item'
import Child from './child'
const Index = () => {
  const [topBars,setTopBars] = useState(['aaa','bbb','ccc'])
  const [activeKey,setActiveKey] = useState('aaa')

  const handleChangeKey = (activeKey: string) => {
    setActiveKey(activeKey)
  }

  return (
    <Tab 
      topBars={topBars}
      activeKey={activeKey}
      onChangeKeys={handleChangeKey}
      animate={false}
    >
      {topBars.map((item,index) => {
        return (
          <TabItem key={item} activeKey={item}>
            <Child />
          </TabItem>
        )
      })}
    </Tab>
  )
}

export default Index
Index.scss
$prefixCls: 'tab';

.#{$prefixCls} {
  width: 276px;
  height: 200px;
  overflow: hidden;
  ul {
    li {
      float: left;
      margin: 0 10px;
      border: 1px solid #ccc;
      width: 70px;
      text-align: center;
      cursor: pointer;
      &.active {
        background-color: skyblue;
      }
      &:hover {
        background-color: skyblue;
      }
    }
  }
  &-div {
    &.animate {
      transition: all 0.5s;
      .tab-div__children {
        visibility: visible;
      }
    }
    position: relative;
    width: 100%;
    &__children {
      visibility: hidden;
      position: absolute;
      top: 25px;
      width: 276px;
      height: 200px;
      &:nth-child(1) {
        background-color: skyblue;
      }
      &:nth-child(2) {
        background-color: red;
      }
      &:nth-child(3) {
        background-color: yellowgreen;
      }
      &.show {
        visibility: visible;
      }
    }
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值