Skip to content

Commit f36fd1c

Browse files
committed
[Refactor] Refactor modal to blur the background
1 parent 6a79cdf commit f36fd1c

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

frontend/src/components/Modal.js

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
1-
import React, {useState} from 'react';
2-
3-
/**
4-
* Modal component
5-
* @param {string} title - A string which is the title at the top of the modal.
6-
* @param {React.JSX.Element} trigger - React.JSX.Element that when clicked will open the modal.
7-
* @param {Map} inputFields - {var : "Input Var Here", ...} A map of variables where things typed into a text box will be stored to the string that the user will see above that text box.
8-
* @param {(e: any) => void} changeHandler - The handler that handles changes in text and assigns them to a variable.
9-
* @param {(e: any) => Promise<void>} confirmHandler - The handler that calls an API upon clicking confirm
10-
*/
11-
export const Modal = ({title, trigger, inputFields, changeHandler, confirmHandler}) => {
1+
import React, { useState } from 'react';
122

3+
export const Modal = ({ title, trigger, inputFields, changeHandler, confirmHandler, onToggle }) => {
134
const [showModal, setShowModal] = useState(false);
145

156
const close = () => {
167
setShowModal(false);
8+
if (onToggle) onToggle(false);
179
};
1810

1911
const open = () => {
2012
setShowModal(true);
13+
if (onToggle) onToggle(true);
2114
};
2215

23-
var fieldList = []
24-
Object.entries(inputFields).map(([name, text]) => (
25-
fieldList.push(
26-
<div className="p-2">
27-
<label>{text+ ": "} </label>
28-
<input className="border-2 border-slate-300 rounded p-2 size-11/12"
16+
const fieldList = Object.entries(inputFields).map(([name, text]) => (
17+
<div className="p-2" key={name}>
18+
<label className="block font-medium">{text + ":"}</label>
19+
<input
20+
className="border-2 border-gray-300 rounded p-2 size-11/12"
2921
type="text"
3022
name={name}
3123
onChange={changeHandler}
32-
/>
33-
</div>
34-
)
24+
/>
25+
</div>
3526
));
3627

3728
return (
3829
<div>
3930
<div onClick={open}>{trigger}</div>
40-
{showModal ?
41-
<div>
42-
<div className='opacity-50 bg-black fixed top-0 left-0 h-full w-full' onClick={close}/>
43-
<div className="flex-auto bg-white rounded-lg border-2 border-slate-300 absolute min-w-80 top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 p-3">
44-
<h3 className="font-semibold">
45-
{title}
46-
</h3>
47-
{fieldList}
48-
<div className="grid grid-rows-1 grid-cols-2 p-2">
49-
<button className="justify-self-start border-2 border-slate-300 p-1 rounded" onClick=
50-
{() => close()}>
31+
{/* Modal */}
32+
{showModal && (
33+
<div>
34+
<div
35+
className="fixed z-[1040] overflow-auto backdrop-blur bg-opacity-50 bg-black fixed top-0 left-0 h-full w-full"
36+
onClick={close}
37+
/>
38+
{/* Modal Content */}
39+
<div className="fixed z-[1050] top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 bg-white p-4 rounded-lg shadow-lg border-2 border-slate-300 absolute min-w-80">
40+
<h3 className="font-semibold text-lg mb-4">{title}</h3>
41+
{fieldList}
42+
<div className="mt-4 flex justify-end space-x-2">
43+
<button
44+
className="px-4 py-2 border border-gray-300 rounded bg-gray-200 hover:bg-gray-300"
45+
onClick={close}
46+
>
5147
Cancel
52-
</button>
53-
<button className="justify-self-end border-2 border-slate-300 p-1 rounded bg-green-400" onClick={confirmHandler}>
54-
Confirm
55-
</button>
56-
</div>
48+
</button>
49+
<button
50+
className="px-4 py-2 border border-gray-300 rounded bg-green-500 text-white hover:bg-green-600"
51+
onClick={confirmHandler}
52+
>
53+
Confirm
54+
</button>
55+
</div>
56+
</div>
5757
</div>
58-
</div>:null}
58+
)}
5959
</div>
6060
);
61-
}
61+
};

0 commit comments

Comments
 (0)