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.5 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.hide()
}
4 years ago
4 years ago
useEffect(
() => {
modal.current.focus()
}, []
)
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()
}
4 years ago
return (<>
<div
id="modal-dialog"
data-id="modalDialogContainer"
4 years ago
data-backdrop="static"
4 years ago
data-keyboard="false"
tabIndex={-1}
4 years ago
className="modal d-block"
4 years ago
role="dialog"
>
<div id="modal-background" className="modal-dialog" role="document">
<div
tabIndex={1}
onBlur={(e) => {
e.stopPropagation()
handleHide()
}}
ref={modal}
4 years ago
className={'modal-content remixModalContent ' + (props.opts ? props.opts.class ? props.opts.class : '' : '')}
onKeyDown={({ keyCode }) => { modalKeyEvent(keyCode) }}
>
4 years ago
<div className="modal-header">
<h6 className="modal-title" data-id="modalDialogModalTitle">
4 years ago
{props.title && props.title}
4 years ago
</h6>
{!props.opts.hideClose &&
<span className="modal-close" onClick={() => handleHide()}>
4 years ago
<i id="modal-close" title="Close" className="fas fa-times" aria-hidden="true"></i>
</span>
}
</div>
<div className="modal-body text-break remixModalBody" data-id="modalDialogModalBody">
{props.content &&
props.content
}
</div>
<div className="modal-footer" data-id="modalDialogModalFooter">
{/* todo add autofocus ^^ */}
{props.ok &&
4 years ago
<span
id="modal-footer-ok"
4 years ago
className={'modal-ok btn btn-sm ' + (state.toggleBtn ? 'btn-dark' : 'btn-light')}
onClick={() => {
if (props.ok && props.ok.fn) props.ok.fn()
handleHide()
}}
tabIndex={1}
4 years ago
>
4 years ago
{props.ok && props.ok.label ? props.ok.label : 'OK'}
4 years ago
</span>
}
<span
id="modal-footer-cancel"
4 years ago
className={'modal-cancel btn btn-sm ' + (state.toggleBtn ? 'btn-light' : 'btn-dark')}
data-dismiss="modal"
onClick={() => {
if (props.cancel && props.cancel.fn) props.cancel.fn()
handleHide()
}}
tabIndex={2}
>
4 years ago
{props.cancel && 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