Debian Codegraph @

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

raw
1 import { Fragment, useState } from 'react';
2 import {
3 Alert,
4 AlertProps,
5 AlertGroup,
6 AlertActionCloseButton,
7 AlertVariant,
8 Flex,
9 FlexItem
10 } from '@patternfly/react-core';
11 import buttonStyles from '@patternfly/react-styles/css/components/Button/button';
12
13 export const AlertGroupToast: React.FunctionComponent = () => {
14 const [alerts, setAlerts] = useState<Partial<AlertProps>[]>([]);
15
16 const addAlert = (title: string, variant: AlertProps['variant'], key: React.Key) => {
17 setAlerts((prevAlerts) => [{ title, variant, key }, ...prevAlerts]);
18 };
19
20 const removeAlert = (key: React.Key) => {
21 setAlerts((prevAlerts) => [...prevAlerts.filter((alert) => alert.key !== key)]);
22 };
23
24 const btnClasses = [buttonStyles.button, buttonStyles.modifiers.secondary].join(' ');
25
26 const getUniqueId = () => new Date().getTime();
27
28 const addSuccessAlert = () => {
29 addAlert('Toast success alert', 'success', getUniqueId());
30 };
31
32 const addDangerAlert = () => {
33 addAlert('Toast danger alert', 'danger', getUniqueId());
34 };
35
36 const addInfoAlert = () => {
37 addAlert('Toast info alert', 'info', getUniqueId());
38 };
39
40 return (
41 <Fragment>
42 <Flex gap={{ default: 'gapXs' }}>
43 <FlexItem>
44 <button onClick={addSuccessAlert} type="button" className={btnClasses}>
45 Add toast success alert
46 </button>
47 </FlexItem>
48 <FlexItem>
49 <button onClick={addDangerAlert} type="button" className={btnClasses}>
50 Add toast danger alert
51 </button>
52 </FlexItem>
53 <FlexItem>
54 <button onClick={addInfoAlert} type="button" className={btnClasses}>
55 Add toast info alert
56 </button>
57 </FlexItem>
58 </Flex>
59 <AlertGroup hasAnimations isToast isLiveRegion>
60 {alerts.map(({ key, variant, title }) => (
61 <Alert
62 variant={AlertVariant[variant]}
63 title={title}
64 actionClose={
65 <AlertActionCloseButton
66 title={title as string}
67 variantLabel={`${variant} alert`}
68 onClose={() => removeAlert(key)}
69 />
70 }
71 key={key}
72 />
73 ))}
74 </AlertGroup>
75 </Fragment>
76 );
77 };