Skip to content

Commit ca34e75

Browse files
committed
chore: Fix example to not jump on removing tab
1 parent 824a6f0 commit ca34e75

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

examples/src/components/examples/Avengers.js

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ const code = `class Component extends React.Component {
1414
constructor(props) {
1515
super(props);
1616
17-
this.characters = {
18-
"Ant-Man": { img: "${antMan}", color: "IndianRed", text: "white", desc: "\\"I do some dumb things, and the people I love the most...they pay the price.\\"" },
19-
"Black Widow": { img: "${blackWidow}", color: "SlateGrey", text: "white", desc: "\\"After everything that happened with S.H.I.E.L.D., during my little hiatus, I went back to Russia and tried to find my parents. Two little graves linked by a chain fence. I pulled some weeds and left some flowers. We have what we have when we have it.\\"" },
20-
"Captain America": { img: "${captain}", color: "RoyalBlue", text: "white", desc: "\\"I'm not looking for forgiveness. And I'm way past asking for permission. Earth just lost their best defender. So we're here to fight. If you wanna stay in our way... we'll fight you, too.\\"" },
21-
"Director Fury": { img: "${fury}", color: "Sienna", text: "white", desc: "\\"Back in the day, I had eyes everywhere, ears everywhere else. Here we all are, back on earth, with nothing but our wit, and our will to save the world. So stand. Outwit the platinum bastard.\\"" },
22-
Hawkeye: { img: "${hawkeye}", color: "MediumOrchid", text: "white", desc: "\\"Just can't seem to miss.\\"" },
23-
"Iron Man": { img: "${ironman}", color: "LightCoral", text: "black", desc: "\\"My armor was never a distraction or a hobby. It was a cocoon. And now I'm a changed man. You can take away my house, all my tricks and toys. But one thing you can't take away... I am Iron Man.\\"" },
24-
Loki: { img: "${loki}", color: "LightGreen", text: "black", desc: "\\"I, Loki, Prince of Asgard, Odinson, the rightful King of Jotunheim, God of Mischief, do hereby pledge to you, my undying fidelity.\\"" },
25-
Thor: { img: "${thor}", color: "SkyBlue", text: "black", desc: "\\"You know I’m 1500 years old. I’ve killed twice as many enemies as that. And every one of them would have rather killed me than not succeeded. I’m only alive because fate wants me alive. Thanos is just the latest of a long line of bastards, and he’ll be the latest to feel my vengeance. Fate wills it so.\\"" },
26-
"War Machine": { img: "${warMachine}", color: "LightGrey", text: "black", desc: "\\"138 combat missions. That's how many I've flown, Tony. Every one of them could've been my last, but I flew 'em. Because the fight needed to be fought.\\"" }
27-
};
17+
this.characters = [
18+
{ name: "Ant-Man", img: "${antMan}", color: "IndianRed", text: "white", desc: "\\"I do some dumb things, and the people I love the most...they pay the price.\\"" },
19+
{ name: "Black Widow", img: "${blackWidow}", color: "SlateGrey", text: "white", desc: "\\"After everything that happened with S.H.I.E.L.D., during my little hiatus, I went back to Russia and tried to find my parents. Two little graves linked by a chain fence. I pulled some weeds and left some flowers. We have what we have when we have it.\\"" },
20+
{ name: "Captain America", img: "${captain}", color: "RoyalBlue", text: "white", desc: "\\"I'm not looking for forgiveness. And I'm way past asking for permission. Earth just lost their best defender. So we're here to fight. If you wanna stay in our way... we'll fight you, too.\\"" },
21+
{ name: "Director Fury", img: "${fury}", color: "Sienna", text: "white", desc: "\\"Back in the day, I had eyes everywhere, ears everywhere else. Here we all are, back on earth, with nothing but our wit, and our will to save the world. So stand. Outwit the platinum bastard.\\"" },
22+
{ name: "Hawkeye", img: "${hawkeye}", color: "MediumOrchid", text: "white", desc: "\\"Just can't seem to miss.\\"" },
23+
{ name: "Iron Man", img: "${ironman}", color: "LightCoral", text: "black", desc: "\\"My armor was never a distraction or a hobby. It was a cocoon. And now I'm a changed man. You can take away my house, all my tricks and toys. But one thing you can't take away... I am Iron Man.\\"" },
24+
{ name: "Loki", img: "${loki}", color: "LightGreen", text: "black", desc: "\\"I, Loki, Prince of Asgard, Odinson, the rightful King of Jotunheim, God of Mischief, do hereby pledge to you, my undying fidelity.\\"" },
25+
{ name: "Thor", img: "${thor}", color: "SkyBlue", text: "black", desc: "\\"You know I’m 1500 years old. I’ve killed twice as many enemies as that. And every one of them would have rather killed me than not succeeded. I’m only alive because fate wants me alive. Thanos is just the latest of a long line of bastards, and he’ll be the latest to feel my vengeance. Fate wills it so.\\"" },
26+
{ name: "War Machine", img: "${warMachine}", color: "LightGrey", text: "black", desc: "\\"138 combat missions. That's how many I've flown, Tony. Every one of them could've been my last, but I flew 'em. Because the fight needed to be fought.\\"" }
27+
];
2828
2929
this.state = {
3030
"Ant-Man": true,
@@ -35,24 +35,29 @@ const code = `class Component extends React.Component {
3535
Hawkeye: true,
3636
"Iron Man": true,
3737
Thor: true,
38-
"War Machine": true
38+
"War Machine": true,
39+
selectedIndex: 0
3940
};
4041
4142
this.handleCheckClicked = this.handleCheckClicked.bind(this);
4243
}
4344
4445
handleCheckClicked(e) {
45-
this.setState({
46+
const state = {
4647
[e.target.name]: e.target.checked
47-
});
48+
};
49+
if (this.characters.findIndex(({name}) => name === e.target.name) <= this.state.selectedIndex) {
50+
state.selectedIndex = this.state.selectedIndex + (e.target.checked ? 1 : -1);
51+
}
52+
this.setState(state);
4853
}
4954
5055
render() {
5156
const links = [];
5257
const tabs = [];
5358
const tabPanels = [];
5459
55-
Object.keys(this.characters).forEach(name => {
60+
this.characters.forEach(({ name, img, color: backgroundColor, text: color, desc }) => {
5661
links.push(
5762
<label key={name}>
5863
<input
@@ -67,8 +72,6 @@ const code = `class Component extends React.Component {
6772
6873
if (!this.state[name]) return;
6974
70-
const { img, color: backgroundColor, text: color, desc } = this.characters[name];
71-
7275
tabs.push(
7376
<Tab style={{ backgroundColor }} className="avengers-tab" key={name}>
7477
<img src={img} alt={name} height="32" width="32" />
@@ -86,6 +89,8 @@ const code = `class Component extends React.Component {
8689
<div>
8790
<p>{links}</p>
8891
<Tabs
92+
selectedIndex={this.state.selectedIndex}
93+
onSelect={(selectedIndex) => this.setState({ selectedIndex })}
8994
selectedTabClassName="avengers-tab--selected"
9095
selectedTabPanelClassName="avengers-tab-panel--selected"
9196
>

0 commit comments

Comments
 (0)