forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlertDynamicLiveRegion.tsx
More file actions
68 lines (63 loc) · 2.04 KB
/
AlertDynamicLiveRegion.tsx
File metadata and controls
68 lines (63 loc) · 2.04 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
import { Fragment, useState } from 'react';
import { Alert, AlertGroup, AlertVariant, Flex, FlexItem } from '@patternfly/react-core';
import buttonStyles from '@patternfly/react-styles/css/components/Button/button';
interface AlertInfo {
title: string;
variant: AlertVariant;
key: number;
}
export const DynamicLiveRegionAlert: React.FunctionComponent = () => {
const [alerts, setAlerts] = useState<AlertInfo[]>([]);
const getUniqueId: () => number = () => new Date().getTime();
const btnClasses = [buttonStyles.button, buttonStyles.modifiers.secondary].join(' ');
const addAlert = (alertInfo: AlertInfo) => {
setAlerts((prevAlertInfo) => [alertInfo, ...prevAlertInfo]);
};
const addSuccessAlert = () => {
addAlert({
title: 'Single success alert',
variant: AlertVariant.success,
key: getUniqueId()
});
};
const addInfoAlert = () => {
addAlert({
title: 'Single info alert',
variant: AlertVariant.info,
key: getUniqueId()
});
};
const addDangerAlert = () => {
addAlert({
title: 'Single danger alert',
variant: AlertVariant.danger,
key: getUniqueId()
});
};
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={addInfoAlert} type="button" className={btnClasses}>
Add single info alert
</button>
</FlexItem>
<FlexItem>
<button onClick={addDangerAlert} type="button" className={btnClasses}>
Add single danger alert
</button>
</FlexItem>
</Flex>
<AlertGroup hasAnimations isLiveRegion aria-live="polite" aria-relevant="additions text" aria-atomic="false">
{alerts.map(({ title, variant, key }) => (
<Alert variant={variant} title={title} key={key} />
))}
</AlertGroup>
</Fragment>
);
};