fsl-lab 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fsl/__init__.py +161 -0
- fsl/__main__.py +5 -0
- fsl/cli.py +45 -0
- fsl/programs/README.md +49 -0
- fsl/programs/p01/index.html +12 -0
- fsl/programs/p01/package.json +20 -0
- fsl/programs/p01/src/App.jsx +38 -0
- fsl/programs/p01/src/main.jsx +9 -0
- fsl/programs/p01/src/pages/Home.jsx +10 -0
- fsl/programs/p01/src/pages/Register.jsx +45 -0
- fsl/programs/p01/src/pages/RegisteredUsers.jsx +31 -0
- fsl/programs/p01/vite.config.js +6 -0
- fsl/programs/p02/index.html +12 -0
- fsl/programs/p02/package.json +19 -0
- fsl/programs/p02/src/App.jsx +24 -0
- fsl/programs/p02/src/components/Footer.jsx +9 -0
- fsl/programs/p02/src/components/Header.jsx +10 -0
- fsl/programs/p02/src/components/Navbar.jsx +13 -0
- fsl/programs/p02/src/main.jsx +9 -0
- fsl/programs/p02/vite.config.js +6 -0
- fsl/programs/p03/index.html +12 -0
- fsl/programs/p03/package.json +19 -0
- fsl/programs/p03/src/App.jsx +23 -0
- fsl/programs/p03/src/components/StudentForm.jsx +30 -0
- fsl/programs/p03/src/components/StudentList.jsx +20 -0
- fsl/programs/p03/src/main.jsx +9 -0
- fsl/programs/p03/vite.config.js +6 -0
- fsl/programs/p04/index.html +12 -0
- fsl/programs/p04/package.json +19 -0
- fsl/programs/p04/src/App.jsx +48 -0
- fsl/programs/p04/src/main.jsx +9 -0
- fsl/programs/p04/vite.config.js +6 -0
- fsl/programs/p05/index.html +12 -0
- fsl/programs/p05/package.json +19 -0
- fsl/programs/p05/src/App.jsx +22 -0
- fsl/programs/p05/src/main.jsx +9 -0
- fsl/programs/p05/vite.config.js +6 -0
- fsl/programs/p06/index.html +12 -0
- fsl/programs/p06/package.json +19 -0
- fsl/programs/p06/src/App.jsx +25 -0
- fsl/programs/p06/src/main.jsx +9 -0
- fsl/programs/p06/vite.config.js +6 -0
- fsl/programs/p07/index.html +12 -0
- fsl/programs/p07/package.json +19 -0
- fsl/programs/p07/public/students.json +7 -0
- fsl/programs/p07/src/App.jsx +14 -0
- fsl/programs/p07/src/components/StudentList.jsx +38 -0
- fsl/programs/p07/src/main.jsx +9 -0
- fsl/programs/p07/vite.config.js +6 -0
- fsl/programs/p08/index.html +12 -0
- fsl/programs/p08/package.json +19 -0
- fsl/programs/p08/src/App.jsx +26 -0
- fsl/programs/p08/src/Employee.jsx +20 -0
- fsl/programs/p08/src/main.jsx +9 -0
- fsl/programs/p08/vite.config.js +6 -0
- fsl/programs/p09/syncFile.js +24 -0
- fsl/programs/p10/asyncFile.js +31 -0
- fsl/programs/p10/asyncFilePromises.js +24 -0
- fsl/programs/p11/streams.js +41 -0
- fsl/programs/p12/bufferExample.js +28 -0
- fsl/programs/p13/inventory.js +64 -0
- fsl/programs/p13/inventory_mongosh.js +34 -0
- fsl/programs/p13/package.json +11 -0
- fsl/programs/p14/package.json +12 -0
- fsl/programs/p14/server.js +77 -0
- fsl/programs/p14/test-commands.txt +19 -0
- fsl/programs/p15/package.json +12 -0
- fsl/programs/p15/public/index.html +144 -0
- fsl/programs/p15/server.js +133 -0
- fsl/programs/p15/test-commands.txt +17 -0
- fsl/programs/p16/package.json +12 -0
- fsl/programs/p16/server.js +88 -0
- fsl/programs/p16/test-commands.txt +23 -0
- fsl_lab-1.0.0.dist-info/METADATA +61 -0
- fsl_lab-1.0.0.dist-info/RECORD +78 -0
- fsl_lab-1.0.0.dist-info/WHEEL +5 -0
- fsl_lab-1.0.0.dist-info/entry_points.txt +2 -0
- fsl_lab-1.0.0.dist-info/top_level.txt +1 -0
fsl/__init__.py
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"""fsl — Full Stack Lab programs 1-16.
|
|
2
|
+
|
|
3
|
+
Terminal:
|
|
4
|
+
fsl list all labs
|
|
5
|
+
fsl 14 show code of lab 14
|
|
6
|
+
fsl 14 get save lab 14 as folder p14
|
|
7
|
+
fsl all save all labs
|
|
8
|
+
|
|
9
|
+
Python / Jupyter:
|
|
10
|
+
import fsl
|
|
11
|
+
fsl.labs() list all labs
|
|
12
|
+
fsl.lab14() show code of lab 14
|
|
13
|
+
fsl.get_lab14() save lab 14 as folder p14
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from importlib.resources import files as _files
|
|
17
|
+
from pathlib import Path as _Path
|
|
18
|
+
|
|
19
|
+
__version__ = "1.0.0"
|
|
20
|
+
|
|
21
|
+
LABS = {
|
|
22
|
+
1: "React Router: Home / Register / Registered Users",
|
|
23
|
+
2: "Reusable Header, Navbar, Footer",
|
|
24
|
+
3: "Student form + list component",
|
|
25
|
+
4: "useState task list",
|
|
26
|
+
5: "useRef auto-focus",
|
|
27
|
+
6: "useEffect clock",
|
|
28
|
+
7: "JSON file + fetch into list component",
|
|
29
|
+
8: "Parent -> child props (employee cards)",
|
|
30
|
+
9: "Synchronous fs",
|
|
31
|
+
10: "Asynchronous fs (callbacks + async/await)",
|
|
32
|
+
11: "Readable / Writable streams",
|
|
33
|
+
12: "Buffers",
|
|
34
|
+
13: "MongoDB CRUD - electronics inventory",
|
|
35
|
+
14: "Bookstore CRUD API",
|
|
36
|
+
15: "Clinic appointment booking (API + web page)",
|
|
37
|
+
16: "University student records CRUD API",
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
RUN = {
|
|
41
|
+
**{n: ["npm install", "npm run dev", "# open http://localhost:5173"] for n in range(1, 9)},
|
|
42
|
+
9: ["node syncFile.js"],
|
|
43
|
+
10: ["node asyncFile.js", "node asyncFilePromises.js"],
|
|
44
|
+
11: ["node streams.js"],
|
|
45
|
+
12: ["node bufferExample.js"],
|
|
46
|
+
13: ["# MongoDB must be running", "npm install", "node inventory.js"],
|
|
47
|
+
14: ["# MongoDB must be running", "npm install", "node server.js", "# test: see test-commands.txt"],
|
|
48
|
+
15: ["# MongoDB must be running", "npm install", "node server.js", "# open http://localhost:3000"],
|
|
49
|
+
16: ["# MongoDB must be running", "npm install", "node server.js", "# test: see test-commands.txt"],
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _num(n):
|
|
54
|
+
s = str(n).strip().lower().lstrip("p")
|
|
55
|
+
if not s.isdigit() or int(s) not in LABS:
|
|
56
|
+
raise ValueError(f"No lab {n!r}. Labs are 1 to 16.")
|
|
57
|
+
return int(s)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _dir(n):
|
|
61
|
+
return _files(__package__) / "programs" / f"p{n:02d}"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _walk(node, prefix=""):
|
|
65
|
+
for c in sorted(node.iterdir(), key=lambda c: c.name):
|
|
66
|
+
if c.is_dir():
|
|
67
|
+
yield from _walk(c, f"{prefix}{c.name}/")
|
|
68
|
+
else:
|
|
69
|
+
yield f"{prefix}{c.name}", c
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def list():
|
|
73
|
+
"""Print all labs."""
|
|
74
|
+
print("Full Stack Lab programs\n")
|
|
75
|
+
for n, topic in LABS.items():
|
|
76
|
+
print(f" {n:>2}. {topic}")
|
|
77
|
+
print("\nfsl 14 -> show code of lab 14")
|
|
78
|
+
print("fsl 14 get -> save lab 14 as a folder")
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def code(n, file=None):
|
|
82
|
+
"""Return the code of lab n as text (all files, or just one)."""
|
|
83
|
+
n = _num(n)
|
|
84
|
+
out = []
|
|
85
|
+
for name, node in _walk(_dir(n)):
|
|
86
|
+
if file and name != file and name.split("/")[-1] != file:
|
|
87
|
+
continue
|
|
88
|
+
text = node.read_text(encoding="utf-8")
|
|
89
|
+
out.append(text if file else f"\n========== {name} ==========\n{text}")
|
|
90
|
+
if not out:
|
|
91
|
+
names = ", ".join(name for name, _ in _walk(_dir(n)))
|
|
92
|
+
raise ValueError(f"Lab {n} has no file {file!r}. Files: {names}")
|
|
93
|
+
return "\n".join(out)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def show(n, file=None):
|
|
97
|
+
"""Print the code of lab n."""
|
|
98
|
+
n = _num(n)
|
|
99
|
+
if not file:
|
|
100
|
+
print(f"Lab {n}: {LABS[n]}")
|
|
101
|
+
print(code(n, file))
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def get(n, folder=None):
|
|
105
|
+
"""Save lab n as a folder (default p01..p16) and print how to run it."""
|
|
106
|
+
n = _num(n)
|
|
107
|
+
dest = _Path(folder or f"p{n:02d}").resolve()
|
|
108
|
+
for name, node in _walk(_dir(n)):
|
|
109
|
+
target = dest / name
|
|
110
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
111
|
+
target.write_bytes(node.read_bytes())
|
|
112
|
+
print(f"Saved lab {n} to {dest}\n\nTo run it:\n cd {dest.name}")
|
|
113
|
+
for line in RUN[n]:
|
|
114
|
+
print(f" {line}")
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def all(folder="fsl-labs"):
|
|
118
|
+
"""Save all 16 labs into one folder."""
|
|
119
|
+
dest = _Path(folder).resolve()
|
|
120
|
+
for n in LABS:
|
|
121
|
+
d = dest / f"p{n:02d}"
|
|
122
|
+
for name, node in _walk(_dir(n)):
|
|
123
|
+
t = d / name
|
|
124
|
+
t.parent.mkdir(parents=True, exist_ok=True)
|
|
125
|
+
t.write_bytes(node.read_bytes())
|
|
126
|
+
readme = _files(__package__) / "programs" / "README.md"
|
|
127
|
+
(dest / "README.md").write_text(readme.read_text(encoding="utf-8"), encoding="utf-8")
|
|
128
|
+
print(f"Saved all 16 labs to {dest}")
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def labs():
|
|
132
|
+
"""List all labs (Jupyter-friendly)."""
|
|
133
|
+
print("Full Stack Lab programs\n")
|
|
134
|
+
for n, topic in LABS.items():
|
|
135
|
+
print(f" fsl.lab{n}()".ljust(16) + topic)
|
|
136
|
+
print("\nfsl.lab14() -> show code of lab 14")
|
|
137
|
+
print("fsl.get_lab14() -> save lab 14 as folder p14")
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
# fsl.lab1() ... fsl.lab16() show code
|
|
141
|
+
# fsl.get_lab1() ... fsl.get_lab16() save folder
|
|
142
|
+
def _make_show(n):
|
|
143
|
+
def f(file=None):
|
|
144
|
+
show(n, file)
|
|
145
|
+
f.__name__ = f"lab{n}"
|
|
146
|
+
f.__doc__ = f"Show code of lab {n}: {LABS[n]}. Optional: a file name, e.g. fsl.lab{n}('server.js')"
|
|
147
|
+
return f
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _make_get(n):
|
|
151
|
+
def f(folder=None):
|
|
152
|
+
get(n, folder)
|
|
153
|
+
f.__name__ = f"get_lab{n}"
|
|
154
|
+
f.__doc__ = f"Save lab {n} ({LABS[n]}) as a folder p{n:02d}, or a folder name you give"
|
|
155
|
+
return f
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
for _n in LABS:
|
|
159
|
+
globals()[f"lab{_n}"] = _make_show(_n)
|
|
160
|
+
globals()[f"get_lab{_n}"] = _make_get(_n)
|
|
161
|
+
del _n
|
fsl/__main__.py
ADDED
fsl/cli.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""The `fsl` terminal command."""
|
|
2
|
+
|
|
3
|
+
import sys
|
|
4
|
+
|
|
5
|
+
import fsl
|
|
6
|
+
|
|
7
|
+
HELP = """fsl - Full Stack Lab programs
|
|
8
|
+
|
|
9
|
+
fsl list all 16 labs
|
|
10
|
+
fsl 14 show code of lab 14
|
|
11
|
+
fsl 14 server.js show only one file of lab 14
|
|
12
|
+
fsl 14 get save lab 14 as folder p14 (+ how to run it)
|
|
13
|
+
fsl 14 get myapp save lab 14 as folder myapp
|
|
14
|
+
fsl all save all labs into folder fsl-labs
|
|
15
|
+
fsl help this help
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def main(argv=None):
|
|
20
|
+
args = sys.argv[1:] if argv is None else argv
|
|
21
|
+
try:
|
|
22
|
+
if not args or args[0] in ("list", "ls"):
|
|
23
|
+
fsl.list()
|
|
24
|
+
elif args[0] in ("help", "-h", "--help"):
|
|
25
|
+
print(HELP)
|
|
26
|
+
elif args[0] in ("-v", "--version", "version"):
|
|
27
|
+
print(f"fsl {fsl.__version__}")
|
|
28
|
+
elif args[0] == "all":
|
|
29
|
+
fsl.all(args[1] if len(args) > 1 else "fsl-labs")
|
|
30
|
+
elif len(args) > 1 and args[1] in ("get", "save"):
|
|
31
|
+
fsl.get(args[0], args[2] if len(args) > 2 else None)
|
|
32
|
+
elif args[0] in ("get", "save") and len(args) > 1: # also allow: fsl get 14
|
|
33
|
+
fsl.get(args[1], args[2] if len(args) > 2 else None)
|
|
34
|
+
elif args[0] == "show" and len(args) > 1: # also allow: fsl show 14
|
|
35
|
+
fsl.show(args[1], args[2] if len(args) > 2 else None)
|
|
36
|
+
else:
|
|
37
|
+
fsl.show(args[0], args[1] if len(args) > 1 else None)
|
|
38
|
+
except ValueError as e:
|
|
39
|
+
print(f"Error: {e}\n\n{HELP}", file=sys.stderr)
|
|
40
|
+
return 1
|
|
41
|
+
return 0
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
if __name__ == "__main__":
|
|
45
|
+
sys.exit(main())
|
fsl/programs/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# Full Stack Development Lab – 16 Programs
|
|
2
|
+
|
|
3
|
+
| # | Folder | Topic | Main file(s) |
|
|
4
|
+
|---|--------|-------|--------------|
|
|
5
|
+
| 1 | p01 | React Router: Home / Register / Registered Users | src/App.jsx, src/pages/* |
|
|
6
|
+
| 2 | p02 | Reusable Header, Navbar, Footer | src/components/* |
|
|
7
|
+
| 3 | p03 | Student form + list component | src/components/StudentForm.jsx, StudentList.jsx |
|
|
8
|
+
| 4 | p04 | useState task list | src/App.jsx |
|
|
9
|
+
| 5 | p05 | useRef auto-focus | src/App.jsx |
|
|
10
|
+
| 6 | p06 | useEffect clock | src/App.jsx |
|
|
11
|
+
| 7 | p07 | JSON file + fetch into list component | public/students.json, src/components/StudentList.jsx |
|
|
12
|
+
| 8 | p08 | Parent → child props (employee cards) | src/App.jsx, src/Employee.jsx |
|
|
13
|
+
| 9 | p09 | Synchronous fs | syncFile.js |
|
|
14
|
+
| 10 | p10 | Asynchronous fs | asyncFile.js (callbacks), asyncFilePromises.js (async/await) |
|
|
15
|
+
| 11 | p11 | Readable / Writable streams | streams.js |
|
|
16
|
+
| 12 | p12 | Buffers | bufferExample.js |
|
|
17
|
+
| 13 | p13 | MongoDB CRUD – electronics inventory | inventory.js (Node driver), inventory_mongosh.js (shell) |
|
|
18
|
+
| 14 | p14 | Bookstore CRUD API | server.js, test-commands.txt |
|
|
19
|
+
| 15 | p15 | Clinic appointment booking (REST API + web page) | server.js, public/index.html |
|
|
20
|
+
| 16 | p16 | University student records CRUD API | server.js, test-commands.txt |
|
|
21
|
+
|
|
22
|
+
## How to run
|
|
23
|
+
|
|
24
|
+
**React programs (1–8)** – each folder is a ready Vite project:
|
|
25
|
+
```
|
|
26
|
+
cd p01
|
|
27
|
+
npm install
|
|
28
|
+
npm run dev # open the URL it prints (http://localhost:5173)
|
|
29
|
+
```
|
|
30
|
+
If your lab uses create-react-app instead, just copy the files from `src/`
|
|
31
|
+
into your project's `src/` (rename `.jsx` to `.js` if you like). Program 1 needs
|
|
32
|
+
`npm install react-router-dom`.
|
|
33
|
+
|
|
34
|
+
**Node programs (9–12)** – no install needed:
|
|
35
|
+
```
|
|
36
|
+
cd p09
|
|
37
|
+
node syncFile.js
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**MongoDB programs (13–16)** – MongoDB must be running on `localhost:27017`
|
|
41
|
+
(start the MongoDB service, or use MongoDB Compass to confirm it's up):
|
|
42
|
+
```
|
|
43
|
+
cd p14
|
|
44
|
+
npm install
|
|
45
|
+
node server.js
|
|
46
|
+
```
|
|
47
|
+
Then test with Postman / Thunder Client or the curl lines in `test-commands.txt`.
|
|
48
|
+
For program 15 open http://localhost:3000 in the browser.
|
|
49
|
+
For program 13 you can also run the shell version: `mongosh inventory_mongosh.js`.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>React Lab Program</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="root"></div>
|
|
10
|
+
<script type="module" src="/src/main.jsx"></script>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "program-01",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "vite build",
|
|
9
|
+
"preview": "vite preview"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"react": "^18.3.1",
|
|
13
|
+
"react-dom": "^18.3.1",
|
|
14
|
+
"react-router-dom": "^6.28.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
18
|
+
"vite": "^5.4.11"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Program 1: Functional components + navigation between
|
|
2
|
+
// Home, Register and Registered Users pages using React Router.
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
|
|
5
|
+
import Home from './pages/Home';
|
|
6
|
+
import Register from './pages/Register';
|
|
7
|
+
import RegisteredUsers from './pages/RegisteredUsers';
|
|
8
|
+
|
|
9
|
+
function App() {
|
|
10
|
+
// users list is kept in App so both Register and RegisteredUsers can use it
|
|
11
|
+
const [users, setUsers] = useState([]);
|
|
12
|
+
|
|
13
|
+
const addUser = (user) => {
|
|
14
|
+
setUsers([...users, user]);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
return (
|
|
18
|
+
<BrowserRouter>
|
|
19
|
+
<nav style={{ padding: 10, background: '#282c34' }}>
|
|
20
|
+
<Link to="/" style={linkStyle}>Home</Link>
|
|
21
|
+
<Link to="/register" style={linkStyle}>Register</Link>
|
|
22
|
+
<Link to="/users" style={linkStyle}>Registered Users</Link>
|
|
23
|
+
</nav>
|
|
24
|
+
|
|
25
|
+
<div style={{ padding: 20 }}>
|
|
26
|
+
<Routes>
|
|
27
|
+
<Route path="/" element={<Home />} />
|
|
28
|
+
<Route path="/register" element={<Register addUser={addUser} />} />
|
|
29
|
+
<Route path="/users" element={<RegisteredUsers users={users} />} />
|
|
30
|
+
</Routes>
|
|
31
|
+
</div>
|
|
32
|
+
</BrowserRouter>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const linkStyle = { color: 'white', marginRight: 20, textDecoration: 'none' };
|
|
37
|
+
|
|
38
|
+
export default App;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
import { useNavigate } from 'react-router-dom';
|
|
3
|
+
|
|
4
|
+
function Register({ addUser }) {
|
|
5
|
+
const [name, setName] = useState('');
|
|
6
|
+
const [email, setEmail] = useState('');
|
|
7
|
+
const navigate = useNavigate();
|
|
8
|
+
|
|
9
|
+
const handleSubmit = (e) => {
|
|
10
|
+
e.preventDefault();
|
|
11
|
+
if (!name || !email) {
|
|
12
|
+
alert('Please fill all fields');
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
addUser({ name, email });
|
|
16
|
+
setName('');
|
|
17
|
+
setEmail('');
|
|
18
|
+
navigate('/users'); // go to Registered Users page after registering
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<div>
|
|
23
|
+
<h1>Register</h1>
|
|
24
|
+
<form onSubmit={handleSubmit}>
|
|
25
|
+
<input
|
|
26
|
+
type="text"
|
|
27
|
+
placeholder="Name"
|
|
28
|
+
value={name}
|
|
29
|
+
onChange={(e) => setName(e.target.value)}
|
|
30
|
+
/>
|
|
31
|
+
<br /><br />
|
|
32
|
+
<input
|
|
33
|
+
type="email"
|
|
34
|
+
placeholder="Email"
|
|
35
|
+
value={email}
|
|
36
|
+
onChange={(e) => setEmail(e.target.value)}
|
|
37
|
+
/>
|
|
38
|
+
<br /><br />
|
|
39
|
+
<button type="submit">Register</button>
|
|
40
|
+
</form>
|
|
41
|
+
</div>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export default Register;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
function RegisteredUsers({ users }) {
|
|
2
|
+
return (
|
|
3
|
+
<div>
|
|
4
|
+
<h1>Registered Users</h1>
|
|
5
|
+
{users.length === 0 ? (
|
|
6
|
+
<p>No users registered yet.</p>
|
|
7
|
+
) : (
|
|
8
|
+
<table border="1" cellPadding="8">
|
|
9
|
+
<thead>
|
|
10
|
+
<tr>
|
|
11
|
+
<th>#</th>
|
|
12
|
+
<th>Name</th>
|
|
13
|
+
<th>Email</th>
|
|
14
|
+
</tr>
|
|
15
|
+
</thead>
|
|
16
|
+
<tbody>
|
|
17
|
+
{users.map((user, index) => (
|
|
18
|
+
<tr key={index}>
|
|
19
|
+
<td>{index + 1}</td>
|
|
20
|
+
<td>{user.name}</td>
|
|
21
|
+
<td>{user.email}</td>
|
|
22
|
+
</tr>
|
|
23
|
+
))}
|
|
24
|
+
</tbody>
|
|
25
|
+
</table>
|
|
26
|
+
)}
|
|
27
|
+
</div>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export default RegisteredUsers;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>React Lab Program</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="root"></div>
|
|
10
|
+
<script type="module" src="/src/main.jsx"></script>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "program-02",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "vite build",
|
|
9
|
+
"preview": "vite preview"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"react": "^18.3.1",
|
|
13
|
+
"react-dom": "^18.3.1"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
17
|
+
"vite": "^5.4.11"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Program 2: Reusable functional components - Header, Navbar, Footer.
|
|
2
|
+
import Header from './components/Header';
|
|
3
|
+
import Navbar from './components/Navbar';
|
|
4
|
+
import Footer from './components/Footer';
|
|
5
|
+
|
|
6
|
+
function App() {
|
|
7
|
+
const menuItems = ['Home', 'About', 'Services', 'Contact'];
|
|
8
|
+
|
|
9
|
+
return (
|
|
10
|
+
<div style={{ display: 'flex', flexDirection: 'column', minHeight: '100vh' }}>
|
|
11
|
+
<Header title="My Website" subtitle="Built with reusable React components" />
|
|
12
|
+
<Navbar items={menuItems} />
|
|
13
|
+
|
|
14
|
+
<main style={{ flex: 1, padding: 20 }}>
|
|
15
|
+
<h2>Main Content</h2>
|
|
16
|
+
<p>Header, Navbar and Footer are separate components that can be reused on any page.</p>
|
|
17
|
+
</main>
|
|
18
|
+
|
|
19
|
+
<Footer text="© 2026 My Website. All rights reserved." />
|
|
20
|
+
</div>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export default App;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
function Header({ title, subtitle }) {
|
|
2
|
+
return (
|
|
3
|
+
<header style={{ background: '#1e3a8a', color: 'white', padding: 20, textAlign: 'center' }}>
|
|
4
|
+
<h1 style={{ margin: 0 }}>{title}</h1>
|
|
5
|
+
<p style={{ margin: 0 }}>{subtitle}</p>
|
|
6
|
+
</header>
|
|
7
|
+
);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default Header;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function Navbar({ items }) {
|
|
2
|
+
return (
|
|
3
|
+
<nav style={{ background: '#3b82f6', padding: 10 }}>
|
|
4
|
+
{items.map((item) => (
|
|
5
|
+
<a key={item} href="#" style={{ color: 'white', marginRight: 20, textDecoration: 'none' }}>
|
|
6
|
+
{item}
|
|
7
|
+
</a>
|
|
8
|
+
))}
|
|
9
|
+
</nav>
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default Navbar;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
6
|
+
<title>React Lab Program</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<div id="root"></div>
|
|
10
|
+
<script type="module" src="/src/main.jsx"></script>
|
|
11
|
+
</body>
|
|
12
|
+
</html>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "program-03",
|
|
3
|
+
"private": true,
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"dev": "vite",
|
|
8
|
+
"build": "vite build",
|
|
9
|
+
"preview": "vite preview"
|
|
10
|
+
},
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"react": "^18.3.1",
|
|
13
|
+
"react-dom": "^18.3.1"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
17
|
+
"vite": "^5.4.11"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Program 3: Form to add student details (Name, Roll No, Department)
|
|
2
|
+
// and display records dynamically using a list component.
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import StudentForm from './components/StudentForm';
|
|
5
|
+
import StudentList from './components/StudentList';
|
|
6
|
+
|
|
7
|
+
function App() {
|
|
8
|
+
const [students, setStudents] = useState([]);
|
|
9
|
+
|
|
10
|
+
const addStudent = (student) => {
|
|
11
|
+
setStudents([...students, student]);
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
return (
|
|
15
|
+
<div style={{ padding: 20 }}>
|
|
16
|
+
<h1>Student Registration</h1>
|
|
17
|
+
<StudentForm onAdd={addStudent} />
|
|
18
|
+
<StudentList students={students} />
|
|
19
|
+
</div>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export default App;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
|
|
3
|
+
function StudentForm({ onAdd }) {
|
|
4
|
+
const [name, setName] = useState('');
|
|
5
|
+
const [rollNo, setRollNo] = useState('');
|
|
6
|
+
const [department, setDepartment] = useState('');
|
|
7
|
+
|
|
8
|
+
const handleSubmit = (e) => {
|
|
9
|
+
e.preventDefault();
|
|
10
|
+
if (!name || !rollNo || !department) {
|
|
11
|
+
alert('All fields are required');
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
onAdd({ name, rollNo, department });
|
|
15
|
+
setName('');
|
|
16
|
+
setRollNo('');
|
|
17
|
+
setDepartment('');
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
return (
|
|
21
|
+
<form onSubmit={handleSubmit}>
|
|
22
|
+
<input placeholder="Name" value={name} onChange={(e) => setName(e.target.value)} />
|
|
23
|
+
<input placeholder="Roll No" value={rollNo} onChange={(e) => setRollNo(e.target.value)} />
|
|
24
|
+
<input placeholder="Department" value={department} onChange={(e) => setDepartment(e.target.value)} />
|
|
25
|
+
<button type="submit">Add Student</button>
|
|
26
|
+
</form>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export default StudentForm;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function StudentList({ students }) {
|
|
2
|
+
if (students.length === 0) {
|
|
3
|
+
return <p>No students added yet.</p>;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
return (
|
|
7
|
+
<div>
|
|
8
|
+
<h2>Student Records</h2>
|
|
9
|
+
<ul>
|
|
10
|
+
{students.map((s, index) => (
|
|
11
|
+
<li key={index}>
|
|
12
|
+
<b>{s.name}</b> | Roll No: {s.rollNo} | Department: {s.department}
|
|
13
|
+
</li>
|
|
14
|
+
))}
|
|
15
|
+
</ul>
|
|
16
|
+
</div>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default StudentList;
|