Skip to content

Commit 578932c

Browse files
committed
fix(CToast): remove old elements from toasts array
1 parent 72f23d2 commit 578932c

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/components/toast/CToast.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@ export interface CToastProps extends Omit<HTMLAttributes<HTMLDivElement>, 'title
2727
* Sets the color context of the component to one of CoreUI’s themed colors. [docs]
2828
*
2929
* @type 'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info' | 'dark' | 'light' | string
30-
* @default 'primary'
3130
*/
3231
color?: Colors
3332
/**
3433
* Delay hiding the toast (ms). [docs]
3534
*/
3635
delay?: number
36+
/**
37+
* @ignore
38+
*/
39+
index?: number
3740
/**
3841
* @ignore
3942
*/
@@ -47,7 +50,7 @@ export interface CToastProps extends Omit<HTMLAttributes<HTMLDivElement>, 'title
4750
/**
4851
* Method called before the dissmiss animation has started. [docs]
4952
*/
50-
onDismiss?: () => void
53+
onDismiss?: (index: number | null) => void
5154
}
5255

5356
interface ContextProps extends CToastProps {
@@ -65,6 +68,7 @@ export const CToast = forwardRef<HTMLDivElement, CToastProps>(
6568
className,
6669
color,
6770
delay = 5000,
71+
index,
6872
key,
6973
visible = true,
7074
onDismiss,
@@ -106,7 +110,12 @@ export const CToast = forwardRef<HTMLDivElement, CToastProps>(
106110
className,
107111
)
108112
return (
109-
<CSSTransition in={_visible} timeout={250} onExit={onDismiss} unmountOnExit>
113+
<CSSTransition
114+
in={_visible}
115+
timeout={250}
116+
onExit={() => onDismiss && onDismiss(index ? index : null)}
117+
unmountOnExit
118+
>
110119
<CToastContext.Provider value={contextValues}>
111120
<div
112121
className={_className}
@@ -133,6 +142,7 @@ CToast.propTypes = {
133142
className: PropTypes.string,
134143
color: colorPropType,
135144
delay: PropTypes.number,
145+
index: PropTypes.number,
136146
key: PropTypes.number,
137147
onDismiss: PropTypes.func,
138148
visible: PropTypes.bool,

src/components/toast/CToaster.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export interface CToasterProps extends HTMLAttributes<HTMLDivElement> {
1111
* Describes the placement of your component. [docs]
1212
*
1313
* @type 'top-start' | 'top' | 'top-end' | 'middle-start' | 'middle' | 'middle-end' | 'bottom-start' | 'bottom' | 'bottom-end' | string
14-
* @default 'top-end'
1514
*/
1615
placement?:
1716
| 'top-start'
@@ -25,13 +24,13 @@ export interface CToasterProps extends HTMLAttributes<HTMLDivElement> {
2524
| 'bottom-end'
2625
| string
2726
/**
28-
* TODO:. [docs]
27+
* Adds new `CToast` to `CToaster`. [docs]
2928
*/
3029
push?: ReactElement
3130
}
3231

3332
export const CToaster = forwardRef<HTMLDivElement, CToasterProps>(
34-
({ children, className, placement = 'top-end', push, ...rest }, ref) => {
33+
({ children, className, placement, push, ...rest }, ref) => {
3534
const [toasts, setToasts] = useState<ReactElement[]>([])
3635
const index = useRef<number>(0)
3736

@@ -40,13 +39,14 @@ export const CToaster = forwardRef<HTMLDivElement, CToasterProps>(
4039
push && addToast(push)
4140
}, [push])
4241

43-
// TODO: remove invisible items
4442
const addToast = (push: ReactElement) => {
4543
setToasts((state) => [
4644
...state,
4745
React.cloneElement(push, {
46+
index: index.current,
4847
key: index.current,
49-
onDismiss: () => setToasts((state) => state.filter((i) => i.key !== index.current)),
48+
onDismiss: (index: number) =>
49+
setToasts((state) => state.filter((i) => i.props.index !== index)),
5050
}),
5151
])
5252
}

0 commit comments

Comments
 (0)