Debian Codegraph @

node/@patternfly/react-core/src/components/Alert/examples/AlertGroupToastOverflowCapture.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 AlertGroupToastOverflowCapture: React.FunctionComponent = () => {
14 const [alerts, setAlerts] = useState<Partial<AlertProps>[]>([]);
15 const [overflowMessage, setOverflowMessage] = useState<string>('');
16
17 const maxDisplayed = 4;
18
19 const getOverflowMessage = (alertsNumber: number) => {
20 const overflow = alertsNumber - maxDisplayed;
21 if (overflow > 0) {
22 return `View ${overflow} more alerts`;
23 }
24 return '';
25 };
26
27 const addAlert = (title: string, variant: AlertProps['variant'], key: React.Key) => {
28 setAlerts((prevAlerts) => [{ title, variant, key }, ...prevAlerts]);
29 setOverflowMessage(getOverflowMessage(alerts.length + 1));
30 };
31
32 const removeAlert = (key: React.Key) => {
33 const newAlerts = alerts.filter((alert) => alert.key !== key);
34 setAlerts(newAlerts);
35 setOverflowMessage(getOverflowMessage(newAlerts.length));
36 };
37
38 const btnClasses = [buttonStyles.button, buttonStyles.modifiers.secondary].join(' ');
39
40 const getUniqueId = () => new Date().getTime();
41
42 const addSuccessAlert = () => {
43 addAlert('Toast success alert', 'success', getUniqueId());
44 };
45
46 const addDangerAlert = () => {
47 addAlert('Toast danger alert', 'danger', getUniqueId());
48 };
49
50 const addInfoAlert = () => {
51 addAlert('Toast info alert', 'info', getUniqueId());
52 };
53
54 const onOverflowClick = () => {
55 // eslint-disable-next-line no-console
56 console.log('Overflow message clicked');
57 };
58
59 return (
60 <Fragment>
61 <Flex gap={{ default: 'gapXs' }} style={{ marginBottom: '16px' }}>
62 <FlexItem>
63 <button onClick={addSuccessAlert} type="button" className={btnClasses}>
64 Add toast success alert
65 </button>
66 </FlexItem>
67 <FlexItem>
68 <button onClick={addDangerAlert} type="button" className={btnClasses}>
69 Add toast danger alert
70 </button>
71 </FlexItem>
72 <FlexItem>
73 <button onClick={addInfoAlert} type="button" className={btnClasses}>
74 Add toast info alert
75 </button>
76 </FlexItem>
77 </Flex>
78 <AlertGroup
79 hasAnimations
80 isToast
81 isLiveRegion
82 onOverflowClick={onOverflowClick}
83 overflowMessage={overflowMessage}
84 >
85 {alerts.slice(0, maxDisplayed).map(({ key, variant, title }) => (
86 <Alert
87 variant={AlertVariant[variant]}
88 title={title}
89 actionClose={
90 <AlertActionCloseButton
91 title={title as string}
92 variantLabel={`${variant} alert`}
93 onClose={() => removeAlert(key)}
94 />
95 }
96 key={key}
97 />
98 ))}
99 </AlertGroup>
100 </Fragment>
101 );
102 };