Debian Codegraph @

node/@patternfly/react-core/src/components/Alert/examples/AlertGroupSingularDynamic.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 AlertGroupSingularDynamic: 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('Success alert', 'success', getUniqueId());
30 };
31
32 const addDangerAlert = () => {
33 addAlert('Danger alert', 'danger', getUniqueId());
34 };
35
36 const addInfoAlert = () => {
37 addAlert('Info alert', 'info', getUniqueId());
38 };
39
40 return (
41 <Fragment>
42 <Flex gap={{ default: 'gapXs' }} style={{ marginBottom: '16px' }}>
43 <FlexItem>
44 <button onClick={addSuccessAlert} type="button" className={btnClasses}>
45 Add single success alert
46 </button>
47 </FlexItem>
48 <FlexItem>
49 <button onClick={addDangerAlert} type="button" className={btnClasses}>
50 Add single danger alert
51 </button>
52 </FlexItem>
53 <FlexItem>
54 <button onClick={addInfoAlert} type="button" className={btnClasses}>
55 Add single info alert
56 </button>
57 </FlexItem>
58 </Flex>
59 <AlertGroup hasAnimations isLiveRegion>
60 {alerts.map(({ title, variant, key }) => (
61 <Alert
62 isInline
63 variant={AlertVariant[variant]}
64 title={title}
65 key={key}
66 actionClose={
67 <AlertActionCloseButton
68 title={title as string}
69 variantLabel={`${variant} alert`}
70 onClose={() => removeAlert(key)}
71 />
72 }
73 />
74 ))}
75 </AlertGroup>
76 </Fragment>
77 );
78 };