forked from pansinm/react-chrome-tabs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.tsx
86 lines (74 loc) · 2.03 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React, { useEffect } from "react";
import { useState } from "react";
import { render } from "react-dom";
import { Tabs } from "../src";
import { TabProperties } from "../src/chrome-tabs";
import "../css/chrome-tabs.css";
import './css/demo.css'
import fb from "./images/facebook-favicon.ico";
import google from "./images/google-favicon.ico";
let id = 0;
function App() {
const [tabs, setTabs] = useState<TabProperties[]>([
{ id: "abc", favicon: fb, title: "测试", active: true },
]);
const addTabWithIcon = () => {
id++;
setTabs([
...tabs,
{
id: `tab-id-${id}`,
title: `New Tabs ${id}`,
favicon: tabs.length % 2 ? fb : google,
},
]);
}
const addTabWithIconClass = () => {
id++;
setTabs([
...tabs,
{
id: `tab-id-${id}`,
title: `New Tabs ${id}`,
faviconClass: ' emoji'
},
]);
}
const active = (id: string) => {
setTabs(tabs.map((tab) => ({ ...tab, active: id === tab.id })));
};
const close = (id: string) => {
setTabs(tabs.filter((tab) => tab.id !== id));
};
const reorder = (tabId: string, fromIndex: number, toIndex: number) => {
const beforeTab = tabs.find(tab => tab.id === tabId);
if (!beforeTab) {
return;
}
let newTabs = tabs.filter(tab => tab.id !== tabId);
newTabs.splice(toIndex, 0, beforeTab);
setTabs(newTabs);
};
const closeAll = () => setTabs([]);
const newTab = () => {
setTabs([
...tabs.map((x) => ({ ...x, active: false })),
{ id: `${Math.random()}`, title: "New tab", active: true },
]);
};
return (
<div>
<Tabs
onTabClose={close}
onTabReorder={reorder}
onTabActive={active}
onNewTab={newTab}
tabs={tabs}
></Tabs>
<button onClick={addTabWithIcon}>Add Tab with icon</button>
<button onClick={addTabWithIconClass}>Add Tab with iconClass</button>
<button onClick={closeAll}>Close All</button>
</div>
);
}
render(<App />, document.getElementById("root"));