import React, { Component } from "react";
import "./about.less";
// redux
import { connect } from "react-redux";
// import { setName, setAge } from "../store/action";
interface Props {
}
interface State {
SelectIndex: number,
BtnList: btn[],
ContList: cont[],
}
interface btn {
id: string, btntext: string
}
interface cont {
id: string, conttext: string
}
class About extends Component<Props, State> {
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
timer?: NodeJS.Timeout
constructor(props: Props) {
super(props)
this.state = {
SelectIndex: 0,
BtnList: [
{ id: "1", btntext: "标题一" },
{ id: "2", btntext: "标题二" },
{ id: "3", btntext: "标题三" }
],
ContList: [
{ id: "1", conttext: "内容一" },
{ id: "2", conttext: "内容二" },
{ id: "3", conttext: "内容三" }
]
}
}
FnBian(index: number):void {
this.setState({
SelectIndex: index
})
}
componentDidMount() {
this.FnStart()
}
// 开始
FnStart() {
this.timer = setInterval(() => {
this.FnNext()
}, 1000)
}
FnNext() {
let Index = this.state.SelectIndex
if (Index >= this.state.BtnList.length - 1) {
Index = 0
} else {
Index++
}
this.setState({
SelectIndex: Index
})
}
render() {
return (
<div className="about">
<div className="btn">
{this.state.BtnList.map((item, index) => <div className={this.state.SelectIndex === index ? 'btn-item ac' : 'btn-item'} onClick={this.FnBian.bind(this, index)} key={index}>{item.btntext}</div>)}
</div>
<div className="cont">
{this.state.ContList.map((item, index) => <div className="cont-item" style={{ display: this.state.SelectIndex === index ? 'block' : 'none' }} key={index}>{item.conttext}</div>)}
</div>
</div>
);
}
}
export default connect((props, state) => Object.assign({}, props, state), {})(About);
css
.about {
.btn {
display: flex;
.btn-item {
width: 15vw;
height: 10vh;
text-align: center;
line-height: 10vh;
font-size: 24px;
background-color: aqua;
}
.btn-item.ac {
width: 15vw;
height: 10vh;
text-align: center;
line-height: 10vh;
font-size: 24px;
color: red;
background-color: aqua;
}
}
.cont {
.cont-item {
text-align: center;
line-height: 25vh;
font-size: 24px;
width: 45vw;
height: 25vh;
background-color: yellowgreen;
display: none;
}
}
}