0.2.2 • Published 2 years ago

shinem-tab v0.2.2

Weekly downloads
-
License
ISC
Repository
gitlab
Last release
2 years ago

选项卡/Tab

使用

参数类型备注
toEachArray必填tab列表
toEach0.textStringtab名称
toEach0.activeBooltab是否默认选中
clsStringtab列表class,用来自定义tab列表的样式
itemClsStringtab项的class,用来自定义tab项的样式
onChangeFunction切换tab时的回调
import React from 'react'
import ReactDOM from 'react-dom'
import Tab from '@xm/Tab'
import styles from './index.scss'

class App extends React.Component {
  constructor (props) {
    super(props)

    this.state = {
      tabs: [
        {
          value: 1,
          text: 'tab1',
          active: true
        },
        {
          value: 2,
          text: 'tab2'
        }
      ]
    }

    this.handleClick = this.handleClick.bind(this)
    this.addTab = this.addTab.bind(this)
  }

  handleClick (tab) {
    const {tabs} = this.state

    tabs.forEach((t) => {
      if (t.active) {
        delete t.active
      }

      if (t.value === tab.value) {
        t.active = true
      }
    })

    this.setState({
      tabs
    })
  }

  addTab () {
    const {tabs} = this.state
    const v = tabs[tabs.length - 1].value + 1
    tabs.push({
      value: v,
      text: `tab${v}`
    })

    this.setState({
      tabs
    })
  }

  render () {
    const {tabs} = this.state

    return (
      <div>
        <Tab cls={styles['custom-tabs']} toEach={tabs} onChange={this.handleClick} />
        <button onClick={this.addTab}>添加</button>
      </div>
    )
  }
}

window.addEventListener('DOMContentLoaded', () => {
  ReactDOM.render((<App />), document.body)
})