forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlertGroupSingularDynamicOverflow.tsx
More file actions
97 lines (85 loc) · 2.89 KB
/
AlertGroupSingularDynamicOverflow.tsx
File metadata and controls
97 lines (85 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { Fragment, useState } from 'react';
import {
Alert,
AlertProps,
AlertGroup,
AlertActionCloseButton,
AlertVariant,
Flex,
FlexItem
} from '@patternfly/react-core';
import buttonStyles from '@patternfly/react-styles/css/components/Button/button';
export const AlertGroupSingularDynamicOverflow: React.FunctionComponent = () => {
const [alerts, setAlerts] = useState<Partial<AlertProps>[]>([]);
const [overflowMessage, setOverflowMessage] = useState<string>('');
const maxDisplayed = 4;
const getOverflowMessage = (alertsNumber: number) => {
const overflow = alertsNumber - maxDisplayed;
if (overflow > 0) {
return `View ${overflow} more alerts`;
}
return '';
};
const addAlert = (title: string, variant: AlertProps['variant'], key: React.Key) => {
setAlerts((prevAlerts) => [{ title, variant, key }, ...prevAlerts]);
setOverflowMessage(getOverflowMessage(alerts.length + 1));
};
const removeAlert = (key: React.Key) => {
const newAlerts = alerts.filter((alert) => alert.key !== key);
setAlerts(newAlerts);
setOverflowMessage(getOverflowMessage(newAlerts.length));
};
const btnClasses = [buttonStyles.button, buttonStyles.modifiers.secondary].join(' ');
const getUniqueId = () => new Date().getTime();
const addSuccessAlert = () => {
addAlert('Success alert', 'success', getUniqueId());
};
const addDangerAlert = () => {
addAlert('Danger alert', 'danger', getUniqueId());
};
const addInfoAlert = () => {
addAlert('Info alert', 'info', getUniqueId());
};
const onOverflowClick = () => {
// eslint-disable-next-line no-console
console.log('Overflow message clicked');
};
return (
<Fragment>
<Flex gap={{ default: 'gapXs' }} style={{ marginBottom: '16px' }}>
<FlexItem>
<button onClick={addSuccessAlert} type="button" className={btnClasses}>
Add single success alert
</button>
</FlexItem>
<FlexItem>
<button onClick={addDangerAlert} type="button" className={btnClasses}>
Add single danger alert
</button>
</FlexItem>
<FlexItem>
<button onClick={addInfoAlert} type="button" className={btnClasses}>
Add single info alert
</button>
</FlexItem>
</Flex>
<AlertGroup hasAnimations isLiveRegion onOverflowClick={onOverflowClick} overflowMessage={overflowMessage}>
{alerts.slice(0, maxDisplayed).map(({ key, variant, title }) => (
<Alert
isInline
variant={AlertVariant[variant]}
title={title}
actionClose={
<AlertActionCloseButton
title={title as string}
variantLabel={`${variant} alert`}
onClose={() => removeAlert(key)}
/>
}
key={key}
/>
))}
</AlertGroup>
</Fragment>
);
};