Skip to content

Commit 769983d

Browse files
committed
feat(CDropdown): add onHide and onShow event
1 parent e866781 commit 769983d

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/components/dropdown/CDropdown.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ export interface CDropdownProps extends HTMLAttributes<HTMLDivElement | HTMLLIEl
5151
* @type 'dropup' | 'dropend' | 'dropstart'
5252
*/
5353
direction?: 'dropup' | 'dropend' | 'dropstart'
54+
/**
55+
* Callback fired when the component requests to be hidden.
56+
*/
57+
onHide?: () => void
58+
/**
59+
* Callback fired when the component requests to be shown.
60+
*/
61+
onShow?: () => void
5462
/**
5563
* Describes the placement of your component after Popper.js has applied all the modifiers that may have flipped or altered the originally provided placement property.
5664
*
@@ -85,6 +93,8 @@ export const CDropdown = forwardRef<HTMLDivElement | HTMLLIElement, CDropdownPro
8593
className,
8694
dark,
8795
direction,
96+
onHide,
97+
onShow,
8898
placement = 'bottom-start',
8999
popper = true,
90100
variant = 'btn-group',
@@ -139,6 +149,11 @@ export const CDropdown = forwardRef<HTMLDivElement | HTMLLIElement, CDropdownPro
139149
setVisible(visible)
140150
}, [visible])
141151

152+
useEffect(() => {
153+
_visible && onShow && onShow()
154+
!_visible && onHide && onHide()
155+
}, [_visible])
156+
142157
const handleKeyup = (event: Event) => {
143158
if (!dropdownRef.current?.contains(event.target as HTMLElement)) {
144159
setVisible(false)
@@ -191,6 +206,8 @@ CDropdown.propTypes = {
191206
component: PropTypes.elementType,
192207
dark: PropTypes.bool,
193208
direction: PropTypes.oneOf(['dropup', 'dropend', 'dropstart']),
209+
onHide: PropTypes.func,
210+
onShow: PropTypes.func,
194211
placement: placementPropType,
195212
popper: PropTypes.bool,
196213
variant: PropTypes.oneOf(['btn-group', 'dropdown', 'input-group', 'nav-item']),

0 commit comments

Comments
 (0)