remix-project mirror
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
remix-project/libs/remix-ui/modal-dialog/src/lib/remix-ui-modal-dialog.tsx

115 lines
3.6 KiB

4 years ago
import React, { useRef, useState, useEffect } from 'react' // eslint-disable-line
import { ModalDialogProps } from './types' // eslint-disable-line
4 years ago
4 years ago
import './remix-ui-modal-dialog.css'
4 years ago
export const ModalDialog = (props: ModalDialogProps) => {
const [state, setState] = useState({
toggleBtn: true
4 years ago
})
const modal = useRef(null)
const handleHide = () => {
props.handleHide()
}
4 years ago
useEffect(() => {
modal.current.focus()
}, [props.hide])
4 years ago
4 years ago
const modalKeyEvent = (keyCode) => {
if (keyCode === 27) { // Esc
if (props.cancel && props.cancel.fn) props.cancel.fn()
handleHide()
} else if (keyCode === 13) { // Enter
enterHandler()
4 years ago
} else if (keyCode === 37) {
// todo && footerIsActive) { // Arrow Left
4 years ago
setState((prevState) => { return { ...prevState, toggleBtn: true } })
4 years ago
} else if (keyCode === 39) {
// todo && footerIsActive) { // Arrow Right
4 years ago
setState((prevState) => { return { ...prevState, toggleBtn: false } })
4 years ago
}
}
4 years ago
const enterHandler = () => {
if (state.toggleBtn) {
if (props.ok && props.ok.fn) props.ok.fn()
} else {
if (props.cancel && props.cancel.fn) props.cancel.fn()
}
handleHide()
}
const handleBlur = (e) => {
if (!e.currentTarget.contains(e.relatedTarget)) {
e.stopPropagation()
handleHide()
}
}
return (
4 years ago
<div
4 years ago
data-id={`${props.id}ModalDialogContainer-react`}
4 years ago
data-backdrop="static"
4 years ago
data-keyboard="false"
className='modal'
style={{ display: props.hide ? 'none' : 'block' }}
4 years ago
role="dialog"
>
<div className="modal-dialog" role="document">
<div
onBlur={handleBlur}
ref={modal}
tabIndex={-1}
className={'modal-content remixModalContent ' + (props.modalClass ? props.modalClass : '')}
4 years ago
onKeyDown={({ keyCode }) => { modalKeyEvent(keyCode) }}
>
4 years ago
<div className="modal-header">
4 years ago
<h6 className="modal-title" data-id={`${props.id}ModalDialogModalTitle-react`}>
4 years ago
{props.title && props.title}
4 years ago
</h6>
{!props.showCancelIcon &&
<span className="modal-close" onClick={() => handleHide()}>
<i title="Close" className="fas fa-times" aria-hidden="true"></i>
4 years ago
</span>
}
</div>
4 years ago
<div className="modal-body text-break remixModalBody" data-id={`${props.id}ModalDialogModalBody-react`}>
{ props.children ? props.children : props.message }
4 years ago
</div>
4 years ago
<div className="modal-footer" data-id={`${props.id}ModalDialogModalFooter-react`}>
4 years ago
{/* todo add autofocus ^^ */}
{ props.ok &&
<span
4 years ago
data-id={`${props.id}-modal-footer-ok-react`}
className={'modal-ok btn btn-sm ' + (state.toggleBtn ? 'btn-dark' : 'btn-light')}
onClick={() => {
if (props.ok.fn) props.ok.fn()
handleHide()
}}
>
{ props.ok.label ? props.ok.label : 'OK' }
</span>
}
{ props.cancel &&
<span
4 years ago
data-id={`${props.id}-modal-footer-cancel-react`}
className={'modal-cancel btn btn-sm ' + (state.toggleBtn ? 'btn-light' : 'btn-dark')}
data-dismiss="modal"
onClick={() => {
if (props.cancel.fn) props.cancel.fn()
handleHide()
}}
>
{ props.cancel.label ? props.cancel.label : 'Cancel' }
</span>
}
4 years ago
</div>
</div>
</div>
</div>
)
4 years ago
}
4 years ago
4 years ago
export default ModalDialog