-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
10-modal-development #49
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,55 @@ | ||
import React, { useState } from 'react'; | ||
import React, { useEffect, useState } from 'react'; | ||
|
||
import styles from '../../styles/components/widgets/ModalWidget.module.scss'; | ||
|
||
|
||
const ModalWidget = ({header, paragraph}) => { | ||
return ( | ||
<div className={styles.modalBackground}> | ||
<div className={styles.modalContainer}> | ||
const ModalWidget = ({header, paragraph, cancelText, confirmText, onConfirm,}) => { | ||
const [openModal, setOpenModal] = useState(true); | ||
const switchModal = () => { | ||
setOpenModal(!openModal); | ||
} | ||
Comment on lines
+7
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. openModal should be passed as a prop "open" and then we will be able to control the state outside the component |
||
|
||
const pressEscape = (e) => { | ||
if (e.key == "Escape") { | ||
setOpenModal(false); | ||
} | ||
} | ||
|
||
<div className={styles.header}> | ||
<h2 className={styles.headerText}>{header}</h2> | ||
</div> | ||
<div className={styles.paragraph}> | ||
<p>{paragraph}</p> | ||
</div> | ||
<div className={styles.footer}> | ||
<button className={`${styles.modalButton} ${styles.cancel}`}>Cancel</button> | ||
<button className={`${styles.modalButton} ${styles.confirm}`}>Confirm</button> | ||
</div> | ||
</div> | ||
document.body.addEventListener('keydown', pressEscape); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, move it to useEffect function, but not the returned one, above return. |
||
useEffect(() => { | ||
return () => {document.body.removeEventListener('keydown', pressEscape)} | ||
}, []) | ||
|
||
const pressConfirm = () => { | ||
switchModal(); | ||
onConfirm(true); | ||
} | ||
|
||
return ( | ||
<div> | ||
<button onClick={switchModal}> Show modal </button> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we will have open prop state, then we don't have to have a button here :) |
||
{openModal && <> | ||
<div className={styles.modalBackground}> | ||
<div className={styles.overlay} onClick={switchModal}></div> | ||
<div className={styles.modalContainer}> | ||
<div className={styles.header}> | ||
<h2 className={styles.headerText}>{header}</h2> | ||
</div> | ||
<div className={styles.paragraph}> | ||
<p>{paragraph}</p> | ||
</div> | ||
<div className={styles.footer}> | ||
<button className={`${styles.modalButton} ${styles.cancel}`} onClick={switchModal}> | ||
{cancelText} | ||
</button> | ||
<button className={`${styles.modalButton} ${styles.confirm}`} onClick={pressConfirm}> | ||
Comment on lines
+42
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. onClick can just include function which will be passed and we don't have to wrap it with another one |
||
{confirmText} | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
</> | ||
} | ||
</div> | ||
); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is unnecessary comma after last prop