Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7a99546

Browse files
create custom tabs
Signed-off-by: Amit Amrutiya <amitamrutiya2210@gmail.com>
1 parent 8c41664 commit 7a99546

File tree

8 files changed

+205
-0
lines changed

8 files changed

+205
-0
lines changed

‎React/custom-tabs/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Custom Tabs
2+
3+
![image](https://github.com/user-attachments/assets/e11891f6-761f-4220-8c69-717dab79ee6a)

‎React/custom-tabs/package.json‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "faq-comp",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^5.17.0",
7+
"@testing-library/react": "^13.4.0",
8+
"@testing-library/user-event": "^13.5.0",
9+
"react": "^18.2.0",
10+
"react-dom": "^18.2.0",
11+
"react-scripts": "5.0.1",
12+
"web-vitals": "^2.1.4"
13+
},
14+
"scripts": {
15+
"start": "react-scripts start",
16+
"build": "react-scripts build",
17+
"test": "react-scripts test",
18+
"eject": "react-scripts eject"
19+
},
20+
"eslintConfig": {
21+
"extends": [
22+
"react-app",
23+
"react-app/jest"
24+
]
25+
},
26+
"browserslist": {
27+
"production": [
28+
">0.2%",
29+
"not dead",
30+
"not op_mini all"
31+
],
32+
"development": [
33+
"last 1 chrome version",
34+
"last 1 firefox version",
35+
"last 1 safari version"
36+
]
37+
}
38+
}

‎React/custom-tabs/public/favicon.ico‎

3.78 KB
Binary file not shown.

‎React/custom-tabs/public/index.html‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>

‎React/custom-tabs/src/App.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import Tabs from "./component/Tabs";
2+
3+
function App() {
4+
return (
5+
<div className="App">
6+
<h1>Custom Tabs</h1>
7+
{/* classname, disable */}
8+
<Tabs>
9+
<div title="Home">
10+
<h1>Tab content for Home</h1>
11+
</div>
12+
<div title="About">
13+
<h2> Tab content for About</h2>
14+
</div>
15+
<div title="Contact">
16+
<h3>Tab content for Contact</h3>
17+
</div>
18+
</Tabs>
19+
</div>
20+
);
21+
}
22+
23+
export default App;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React, { useEffect } from "react";
2+
import { useState } from "react";
3+
4+
const Tabs = (props) => {
5+
const [tabsHeaders, setTabsHeaders] = useState([]);
6+
const [contentMap, setContentMap] = useState({});
7+
const [active, setActive] = useState("");
8+
const { children } = props;
9+
10+
useEffect(() => {
11+
const headers = [];
12+
const map = {};
13+
React.Children.forEach(children, (element) => {
14+
console.log("Element : ", element);
15+
if (!React.isValidElement(element)) return;
16+
const { title } = element.props;
17+
headers.push(title);
18+
map[title] = element.props.children;
19+
});
20+
21+
setTabsHeaders(headers); //Headers
22+
setActive(headers[0]);
23+
setContentMap(map); //content
24+
}, [props, children]);
25+
26+
const changeTab = (header) => {
27+
setActive(header);
28+
};
29+
30+
return (
31+
<div>
32+
<div className="headers">
33+
{tabsHeaders.map((header) => (
34+
<button
35+
className={active === header ? "selected" : ""}
36+
key={header}
37+
onClick={() => changeTab(header)}
38+
>
39+
{header}
40+
</button>
41+
))}
42+
</div>
43+
44+
<div>
45+
{Object.keys(contentMap).map((key, idx) => {
46+
if (key === active) {
47+
return <div key={idx}>{contentMap[key]}</div>;
48+
} else {
49+
return null;
50+
}
51+
})}
52+
</div>
53+
</div>
54+
);
55+
};
56+
57+
export default Tabs;

‎React/custom-tabs/src/index.css‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
body {
2+
margin: 0;
3+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5+
sans-serif;
6+
-webkit-font-smoothing: antialiased;
7+
-moz-osx-font-smoothing: grayscale;
8+
}
9+
10+
code {
11+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12+
monospace;
13+
}
14+
15+
.App {
16+
text-align: center;
17+
}
18+
19+
button{
20+
margin: 5px;
21+
padding: 7px 20px;
22+
border: none;
23+
border-radius: 5px;
24+
cursor: pointer;
25+
}
26+
27+
.selected{
28+
background-color: black;
29+
color: white;
30+
}

‎React/custom-tabs/src/index.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from "react";
2+
import ReactDOM from "react-dom/client";
3+
import "./index.css";
4+
import App from "./App";
5+
6+
const root = ReactDOM.createRoot(document.getElementById("root"));
7+
root.render(
8+
<React.StrictMode>
9+
<App />
10+
</React.StrictMode>
11+
);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /