| 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 AlertGroupSingularDynamicOverflow: 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('Success alert', 'success', getUniqueId()); |
| 44 |
}; |
| 45 |
|
| 46 |
const addDangerAlert = () => { |
| 47 |
addAlert('Danger alert', 'danger', getUniqueId()); |
| 48 |
}; |
| 49 |
|
| 50 |
const addInfoAlert = () => { |
| 51 |
addAlert('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 single success alert |
| 65 |
</button> |
| 66 |
</FlexItem> |
| 67 |
<FlexItem> |
| 68 |
<button onClick={addDangerAlert} type="button" className={btnClasses}> |
| 69 |
Add single danger alert |
| 70 |
</button> |
| 71 |
</FlexItem> |
| 72 |
<FlexItem> |
| 73 |
<button onClick={addInfoAlert} type="button" className={btnClasses}> |
| 74 |
Add single info alert |
| 75 |
</button> |
| 76 |
</FlexItem> |
| 77 |
</Flex> |
| 78 |
<AlertGroup hasAnimations isLiveRegion onOverflowClick={onOverflowClick} overflowMessage={overflowMessage}> |
| 79 |
{alerts.slice(0, maxDisplayed).map(({ key, variant, title }) => ( |
| 80 |
<Alert |
| 81 |
isInline |
| 82 |
variant={AlertVariant[variant]} |
| 83 |
title={title} |
| 84 |
actionClose={ |
| 85 |
<AlertActionCloseButton |
| 86 |
title={title as string} |
| 87 |
variantLabel={`${variant} alert`} |
| 88 |
onClose={() => removeAlert(key)} |
| 89 |
/> |
| 90 |
} |
| 91 |
key={key} |
| 92 |
/> |
| 93 |
))} |
| 94 |
</AlertGroup> |
| 95 |
</Fragment> |
| 96 |
); |
| 97 |
}; |