mirror of
https://gitlab.nic.cz/turris/reforis/foris-js.git
synced 2024-12-25 00:11:36 +01:00
Merge branch 'add-alert-animation-and-timeout' into 'dev'
Add Alert animation and timeout See merge request turris/reforis/foris-js!249
This commit is contained in:
commit
6795c3941b
|
@ -5,7 +5,7 @@
|
|||
* See /LICENSE for more information.
|
||||
*/
|
||||
|
||||
import React, { useRef } from "react";
|
||||
import React, { useRef, useEffect, useState } from "react";
|
||||
|
||||
import PropTypes from "prop-types";
|
||||
|
||||
|
@ -40,20 +40,36 @@ Alert.defaultProps = {
|
|||
|
||||
function Alert({ type, onDismiss, children }) {
|
||||
const alertRef = useRef();
|
||||
const [isVisible, setIsVisible] = useState(true);
|
||||
useFocusTrap(alertRef, !!onDismiss);
|
||||
|
||||
useEffect(() => {
|
||||
if (onDismiss) {
|
||||
const timeout = setTimeout(() => setIsVisible(false), 7000);
|
||||
return () => clearTimeout(timeout);
|
||||
}
|
||||
}, [onDismiss]);
|
||||
|
||||
const handleAnimationEnd = () => {
|
||||
if (!isVisible && onDismiss) {
|
||||
onDismiss();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={alertRef}
|
||||
className={`alert alert-${type} ${
|
||||
className={`alert alert-${type} ${isVisible ? "alert-fade-in" : "alert-slide-out-top"} ${
|
||||
onDismiss ? "alert-dismissible" : ""
|
||||
}`.trim()}
|
||||
role="alert"
|
||||
onAnimationEnd={handleAnimationEnd}
|
||||
>
|
||||
{onDismiss && (
|
||||
<button
|
||||
type="button"
|
||||
className="btn-close"
|
||||
onClick={onDismiss}
|
||||
onClick={() => setIsVisible(false)}
|
||||
aria-label={_("Close")}
|
||||
/>
|
||||
)}
|
||||
|
|
|
@ -18,9 +18,9 @@ ThreeDotsMenu.propTypes = {
|
|||
children: PropTypes.arrayOf(PropTypes.node).isRequired,
|
||||
};
|
||||
|
||||
function ThreeDotsMenu({ children }) {
|
||||
function ThreeDotsMenu({ children, ...props }) {
|
||||
return (
|
||||
<div className="dropdown">
|
||||
<div className="dropdown position-static" {...props}>
|
||||
<Button
|
||||
className="btn-sm btn-link text-body"
|
||||
data-bs-toggle="dropdown"
|
||||
|
|
|
@ -43,14 +43,17 @@ describe("AlertContext", () => {
|
|||
expect(componentContainer).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("should dismiss alert with alert button", () => {
|
||||
it("should dismiss alert with alert button", async () => {
|
||||
fireEvent.click(getByText(componentContainer, "Set alert"));
|
||||
// Alert is present
|
||||
expect(getByText(componentContainer, "Alert content")).toBeDefined();
|
||||
|
||||
fireEvent.click(componentContainer.querySelector(".btn-close"));
|
||||
// Alert is gone
|
||||
expect(queryByText(componentContainer, "Alert content")).toBeNull();
|
||||
await (() =>
|
||||
expect(
|
||||
queryByText(componentContainer, "Alert content")
|
||||
).toBeNull());
|
||||
});
|
||||
|
||||
it("should dismiss alert with external button", () => {
|
||||
|
|
|
@ -6,7 +6,7 @@ exports[`AlertContext should render alert 1`] = `
|
|||
id="alert-container"
|
||||
>
|
||||
<div
|
||||
class="alert alert-danger alert-dismissible"
|
||||
class="alert alert-danger alert-fade-in alert-dismissible"
|
||||
role="alert"
|
||||
>
|
||||
<button
|
||||
|
|
Loading…
Reference in New Issue
Block a user