| 1 |
import { Fragment, useState } from 'react'; |
| 2 |
import { Alert, AlertGroup, AlertVariant, Flex, FlexItem } from '@patternfly/react-core'; |
| 3 |
import buttonStyles from '@patternfly/react-styles/css/components/Button/button'; |
| 4 |
|
| 5 |
interface AlertInfo { |
| 6 |
title: string; |
| 7 |
variant: AlertVariant; |
| 8 |
key: number; |
| 9 |
} |
| 10 |
|
| 11 |
export const DynamicLiveRegionAlert: React.FunctionComponent = () => { |
| 12 |
const [alerts, setAlerts] = useState<AlertInfo[]>([]); |
| 13 |
const getUniqueId: () => number = () => new Date().getTime(); |
| 14 |
const btnClasses = [buttonStyles.button, buttonStyles.modifiers.secondary].join(' '); |
| 15 |
|
| 16 |
const addAlert = (alertInfo: AlertInfo) => { |
| 17 |
setAlerts((prevAlertInfo) => [alertInfo, ...prevAlertInfo]); |
| 18 |
}; |
| 19 |
|
| 20 |
const addSuccessAlert = () => { |
| 21 |
addAlert({ |
| 22 |
title: 'Single success alert', |
| 23 |
variant: AlertVariant.success, |
| 24 |
key: getUniqueId() |
| 25 |
}); |
| 26 |
}; |
| 27 |
const addInfoAlert = () => { |
| 28 |
addAlert({ |
| 29 |
title: 'Single info alert', |
| 30 |
variant: AlertVariant.info, |
| 31 |
key: getUniqueId() |
| 32 |
}); |
| 33 |
}; |
| 34 |
const addDangerAlert = () => { |
| 35 |
addAlert({ |
| 36 |
title: 'Single danger alert', |
| 37 |
variant: AlertVariant.danger, |
| 38 |
key: getUniqueId() |
| 39 |
}); |
| 40 |
}; |
| 41 |
|
| 42 |
return ( |
| 43 |
<Fragment> |
| 44 |
<Flex gap={{ default: 'gapXs' }} style={{ marginBottom: '16px' }}> |
| 45 |
<FlexItem> |
| 46 |
<button onClick={addSuccessAlert} type="button" className={btnClasses}> |
| 47 |
Add single success alert |
| 48 |
</button> |
| 49 |
</FlexItem> |
| 50 |
<FlexItem> |
| 51 |
<button onClick={addInfoAlert} type="button" className={btnClasses}> |
| 52 |
Add single info alert |
| 53 |
</button> |
| 54 |
</FlexItem> |
| 55 |
<FlexItem> |
| 56 |
<button onClick={addDangerAlert} type="button" className={btnClasses}> |
| 57 |
Add single danger alert |
| 58 |
</button> |
| 59 |
</FlexItem> |
| 60 |
</Flex> |
| 61 |
<AlertGroup hasAnimations isLiveRegion aria-live="polite" aria-relevant="additions text" aria-atomic="false"> |
| 62 |
{alerts.map(({ title, variant, key }) => ( |
| 63 |
<Alert variant={variant} title={title} key={key} /> |
| 64 |
))} |
| 65 |
</AlertGroup> |
| 66 |
</Fragment> |
| 67 |
); |
| 68 |
}; |