diff --git a/src/bundle/ui-dev/src/modules/common/popup/popup.component.js b/src/bundle/ui-dev/src/modules/common/popup/popup.component.js index f8234288a4..6025d84c8c 100644 --- a/src/bundle/ui-dev/src/modules/common/popup/popup.component.js +++ b/src/bundle/ui-dev/src/modules/common/popup/popup.component.js @@ -1,8 +1,10 @@ -import React, { Component } from 'react'; +import React, { useEffect, useRef, useMemo } from 'react'; import PropTypes from 'prop-types'; import Icon from '../icon/icon'; -const { Translator } = window; +import { createCssClassNames } from '@ibexa-admin-ui/src/bundle/ui-dev/src/modules/common/helpers/css.class.names'; + +const { bootstrap, Translator } = window; const CLASS_NON_SCROLLABLE = 'ibexa-non-scrollable'; const CLASS_MODAL_OPEN = 'modal-open'; @@ -16,205 +18,128 @@ const MODAL_SIZE_CLASS = { large: 'modal-lg', }; -class Popup extends Component { - constructor(props) { - super(props); - - this._refModal = null; - - this.setModalRef = this.setModalRef.bind(this); - this.onKeyUp = this.onKeyUp.bind(this); - - this.state = { - currentProps: { - isVisible: props.isVisible, - isLoading: props.isLoading, - }, - isVisible: props.isVisible, - isLoading: props.isLoading, - }; - } - - static getDerivedStateFromProps(nextProps, prevState) { - if (nextProps.isVisible !== prevState.currentProps.isVisible || nextProps.isLoading !== prevState.currentProps.isLoading) { - return { - currentProps: { - isVisible: nextProps.isVisible, - isLoading: nextProps.isLoading, - }, - isVisible: nextProps.isVisible, - isLoading: nextProps.isLoading, - }; - } - +const Popup = ({ + isVisible, + onClose, + children, + title, + subtitle, + hasFocus, + noKeyboard, + actionBtns, + size, + noHeader, + noCloseBtn, + extraClasses, +}) => { + if (!isVisible) { return null; } - componentDidMount() { - const { noKeyboard, hasFocus } = this.props; - const { isVisible: show } = this.state; - - if (show) { - const bootstrapModal = window.bootstrap.Modal.getOrCreateInstance(this._refModal, { - ...MODAL_CONFIG, - keyboard: !noKeyboard, - focus: hasFocus, - }); - - bootstrapModal.show(); - - this.attachModalEventHandlers(); - } - } - - componentDidUpdate() { - const { isVisible: show } = this.state; - - const bootstrapModal = window.bootstrap.Modal.getOrCreateInstance(this._refModal, { + const modalRef = useRef(null); + const modalClasses = createCssClassNames({ + 'c-popup modal fade': true, + 'c-popup--no-header': noHeader, + [extraClasses]: extraClasses, + }); + const closeBtnLabel = Translator.trans(/*@Desc("Close")*/ 'popup.close.label', {}, 'ibexa_universal_discovery_widget'); + const hidePopup = () => { + bootstrap.Modal.getOrCreateInstance(modalRef.current).hide(); + document.body.classList.remove(CLASS_MODAL_OPEN, CLASS_NON_SCROLLABLE); + }; + const showPopup = () => { + const bootstrapModal = bootstrap.Modal.getOrCreateInstance(modalRef.current, { ...MODAL_CONFIG, - focus: this.props.hasFocus, + keyboard: !noKeyboard, + focus: hasFocus, }); - if (show) { - bootstrapModal.show(); - this.attachModalEventHandlers(); - } else { - bootstrapModal.hide(); - } - } - - componentWillUnmount() { - window.bootstrap.Modal.getOrCreateInstance(this._refModal).hide(); - document.body.classList.remove(CLASS_MODAL_OPEN, CLASS_NON_SCROLLABLE); - } - - attachModalEventHandlers() { - this._refModal.addEventListener('keyup', this.onKeyUp); - this._refModal.addEventListener('hidden.bs.modal', this.props.onClose); - } - - onKeyUp(event) { - const { originalEvent } = event; - const escKeyCode = 27; - const escKeyPressed = originalEvent && (originalEvent.which === escKeyCode || originalEvent.keyCode === escKeyCode); - - if (escKeyPressed) { - this.props.onClose(); - } - } - - setModalRef(component) { - this._refModal = component; - } - - renderHeader() { - return ( -