From 66e35bfb6bdab26fd3dc655e9ea86e0024272952 Mon Sep 17 00:00:00 2001 From: kyletsang <6854874+kyletsang@users.noreply.github.com> Date: Thu, 3 Nov 2022 22:03:14 -0700 Subject: [PATCH] feat(AccordionBody): add transition callback props --- src/AccordionBody.tsx | 43 +++++++++++++++++++++++++++++++++++++++++- test/AccordionSpec.tsx | 32 +++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/src/AccordionBody.tsx b/src/AccordionBody.tsx index 5f941450a2..e0bbbc219f 100644 --- a/src/AccordionBody.tsx +++ b/src/AccordionBody.tsx @@ -2,6 +2,7 @@ import classNames from 'classnames'; import * as React from 'react'; import { useContext } from 'react'; import PropTypes from 'prop-types'; +import { TransitionCallbacks } from '@restart/ui/types'; import { useBootstrapPrefix } from './ThemeProvider'; import AccordionCollapse from './AccordionCollapse'; import AccordionItemContext from './AccordionItemContext'; @@ -9,6 +10,7 @@ import { BsPrefixRefForwardingComponent, BsPrefixProps } from './helpers'; export interface AccordionBodyProps extends BsPrefixProps, + TransitionCallbacks, React.HTMLAttributes {} const propTypes = { @@ -17,6 +19,31 @@ const propTypes = { /** @default 'accordion-body' */ bsPrefix: PropTypes.string, + + /** + * Callback fired before the component expands + */ + onEnter: PropTypes.func, + /** + * Callback fired after the component starts to expand + */ + onEntering: PropTypes.func, + /** + * Callback fired after the component has expanded + */ + onEntered: PropTypes.func, + /** + * Callback fired before the component collapses + */ + onExit: PropTypes.func, + /** + * Callback fired after the component starts to collapse + */ + onExiting: PropTypes.func, + /** + * Callback fired after the component has collapsed + */ + onExited: PropTypes.func, }; const AccordionBody: BsPrefixRefForwardingComponent<'div', AccordionBodyProps> = @@ -27,6 +54,12 @@ const AccordionBody: BsPrefixRefForwardingComponent<'div', AccordionBodyProps> = as: Component = 'div', bsPrefix, className, + onEnter, + onEntering, + onEntered, + onExit, + onExiting, + onExited, ...props }, ref, @@ -35,7 +68,15 @@ const AccordionBody: BsPrefixRefForwardingComponent<'div', AccordionBodyProps> = const { eventKey } = useContext(AccordionItemContext); return ( - + ', () => { onSelectSpy.should.be.calledWith(['0']); }); + + it('should pass transition callbacks to underlying AccordionCollapse', async () => { + const increment = sinon.spy(); + + const { getByText } = render( + + + header0 + + body + + + , + ); + + fireEvent.click(getByText('header0')); + + // Wait for body to open. + await waitFor(() => increment.callCount.should.equal(3)); + + fireEvent.click(getByText('header0')); + + // Wait for body to close. + await waitFor(() => increment.callCount.should.equal(6)); + }); });