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 a9a3351

Browse files
create transfer list
Signed-off-by: Amit Amrutiya <amitamrutiya2210@gmail.com>
1 parent 234f642 commit a9a3351

File tree

8 files changed

+267
-0
lines changed

8 files changed

+267
-0
lines changed

‎React/transfer-list/README.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Transfer List
2+
3+
![image](https://github.com/user-attachments/assets/a2616015-ba54-4611-909f-46ddd98203a8)

‎React/transfer-list/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+
}
3.78 KB
Binary file not shown.
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/transfer-list/src/App.js‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { useState } from "react";
2+
import { data } from "./data";
3+
4+
function App() {
5+
const [leftItems, setLeftItems] = useState(data);
6+
const [rightItems, setRightItems] = useState([]);
7+
8+
const checkedList = (list, id, checked) => {
9+
return list.map((item) => {
10+
if (id === item.id) {
11+
return {
12+
...item,
13+
checked: !checked,
14+
};
15+
}
16+
return item;
17+
});
18+
};
19+
20+
const handleClick = (id, checked, direction) => {
21+
if (direction === "LEFT") {
22+
let copyList = [...leftItems];
23+
copyList = checkedList(copyList, id, checked);
24+
console.log(copyList);
25+
setLeftItems(copyList);
26+
} else {
27+
let copyList = [...rightItems];
28+
copyList = checkedList(copyList, id, checked);
29+
setRightItems(copyList);
30+
}
31+
};
32+
33+
const resetItems = (list) => {
34+
return list.map((item) => {
35+
return {
36+
...item,
37+
checked: false,
38+
};
39+
});
40+
};
41+
42+
const handleTransferBtn = (dir) => {
43+
if (dir === "LEFT_TO_RIGHT") {
44+
if (leftItems.length) {
45+
const copyList = [...leftItems];
46+
const checkList = copyList.filter((item) => item.checked);
47+
const unCheckList = copyList.filter((item) => !item.checked);
48+
setRightItems(resetItems([...rightItems, ...checkList]));
49+
setLeftItems(unCheckList);
50+
}
51+
} else {
52+
const copyList = [...rightItems];
53+
const checkList = copyList.filter((item) => item.checked);
54+
const unCheckList = copyList.filter((item) => !item.checked);
55+
setLeftItems(resetItems([...leftItems, ...checkList]));
56+
setRightItems(unCheckList);
57+
}
58+
};
59+
60+
return (
61+
<div className="App">
62+
<h1>Transfer List</h1>
63+
<div className="container">
64+
<div className="box">
65+
{leftItems.map(({ title, id, checked }) => (
66+
<div
67+
onClick={() => handleClick(id, checked, "LEFT")}
68+
className={`item ${checked && "checked"}`}
69+
id={id}
70+
key={id}
71+
>
72+
{title}
73+
</div>
74+
))}
75+
</div>
76+
77+
<div className="actions">
78+
{/* buttons */}
79+
<button onClick={() => handleTransferBtn("LEFT_TO_RIGHT")}>
80+
Left
81+
</button>
82+
<button onClick={() => handleTransferBtn("RIGHT_TO_LEFT")}>
83+
Right
84+
</button>
85+
</div>
86+
87+
<div className="box">
88+
{rightItems.map(({ title, id, checked }) => (
89+
<div
90+
onClick={() => handleClick(id, checked, "RIGHT")}
91+
className={`item ${checked && "checked"}`}
92+
id={id}
93+
key={id}
94+
>
95+
{title}
96+
</div>
97+
))}
98+
</div>
99+
</div>
100+
</div>
101+
);
102+
}
103+
104+
export default App;

‎React/transfer-list/src/data.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const data = [
2+
{ title: 'First', id: 0, checked: false },
3+
{ title: 'Second', id: 1, checked: false },
4+
{ title: 'Third', id: 2, checked: false },
5+
{ title: 'Fourth', id: 3, checked: false },
6+
];

‎React/transfer-list/src/index.css‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
.container{
20+
display: flex;
21+
justify-content: center;
22+
align-items: center;
23+
}
24+
25+
.item{
26+
height: 30px;
27+
background-color: lightblue;
28+
margin: 10px 0px;
29+
padding: 5px;
30+
font-size: 20px;
31+
border-radius: 5px;
32+
display: flex;
33+
justify-content: center;
34+
align-items: center;
35+
cursor: pointer;
36+
}
37+
38+
.checked{
39+
background-color: black;
40+
color: white;
41+
}
42+
43+
.box{
44+
width: 200px;
45+
height: 250px;
46+
border: 1px solid black;
47+
padding: 10px;
48+
}
49+
50+
.actions{
51+
display: flex;
52+
flex-direction: column;
53+
}
54+
55+
button{
56+
width: 100px;
57+
height: 30px;
58+
border: none;
59+
border-radius: 5px;
60+
margin: 10px;
61+
font-size: 20px;
62+
}

‎React/transfer-list/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 によって変換されたページ (->オリジナル) /