Debian Codegraph @

node/@patternfly/react-core/src/components/Alert/examples/AlertGroupAsync.tsx

raw
1 import { Fragment, useState } from 'react';
2 import {
3 Alert,
4 AlertProps,
5 AlertGroup,
6 AlertActionCloseButton,
7 AlertVariant,
8 InputGroup,
9 InputGroupItem,
10 useInterval
11 } from '@patternfly/react-core';
12 import buttonStyles from '@patternfly/react-styles/css/components/Button/button';
13
14 export const AlertGroupAsync: React.FunctionComponent = () => {
15 const [alerts, setAlerts] = useState<Partial<AlertProps>[]>([]);
16 const [isRunning, setIsRunning] = useState(false);
17
18 const btnClasses = [buttonStyles.button, buttonStyles.modifiers.secondary].join(' ');
19
20 const getUniqueId = () => new Date().getTime();
21
22 const addAlert = () => {
23 setAlerts((prevAlerts) => [
24 {
25 title: `Async notification ${prevAlerts.length + 1} was added to the queue.`,
26 variant: 'danger',
27 key: getUniqueId()
28 },
29 ...prevAlerts
30 ]);
31 };
32
33 const removeAlert = (key: React.Key) => {
34 setAlerts((prevAlerts) => [...prevAlerts.filter((alert) => alert.key !== key)]);
35 };
36
37 const startAsyncAlerts = () => {
38 setIsRunning(true);
39 };
40
41 const stopAsyncAlerts = () => {
42 setIsRunning(false);
43 };
44
45 useInterval(addAlert, isRunning ? 4500 : null);
46
47 return (
48 <Fragment>
49 <InputGroup style={{ marginBottom: '16px' }}>
50 <InputGroupItem>
51 <button onClick={startAsyncAlerts} type="button" className={btnClasses}>
52 Start async alerts
53 </button>
54 </InputGroupItem>
55 <InputGroupItem>
56 <button onClick={stopAsyncAlerts} type="button" className={btnClasses}>
57 Stop async alerts
58 </button>
59 </InputGroupItem>
60 </InputGroup>
61 <AlertGroup hasAnimations isToast isLiveRegion aria-live="assertive">
62 {alerts.map(({ title, variant, key }) => (
63 <Alert
64 variant={AlertVariant[variant]}
65 title={title}
66 key={key}
67 actionClose={
68 <AlertActionCloseButton
69 title={title as string}
70 variantLabel={`${variant} alert`}
71 onClose={() => removeAlert(key)}
72 />
73 }
74 />
75 ))}
76 </AlertGroup>
77 </Fragment>
78 );
79 };