0.2.2 • Published 2 years ago
shinem-tab v0.2.2
选项卡/Tab
使用
参数 | 类型 | 备注 |
---|---|---|
toEach | Array | 必填tab列表 |
toEach0.text | String | tab名称 |
toEach0.active | Bool | tab是否默认选中 |
cls | String | tab列表class,用来自定义tab列表的样式 |
itemCls | String | tab项的class,用来自定义tab项的样式 |
onChange | Function | 切换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)
})
0.2.2
2 years ago