Skip to content

Commit

Permalink
EuiAccordion onToggle callback (#1974)
Browse files Browse the repository at this point in the history
* add onToggle callback to EuiAccordion

* add onToggle tests

* add docs example for onToggle prop

* use jest.fn; remove snapshot

* #1974 CL entry
  • Loading branch information
thompsongl authored May 29, 2019
1 parent c50b2f4 commit f903564
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

- Converted `EuiTableRowCellCheckbox` to TS ([#1964](https://github.com/elastic/eui/pull/1964))
- Updated `caniuse-lite` version resolution ([#1970](https://github.com/elastic/eui/pull/1970))
- Added a webpack directive for naming icon chunks ([#1944])(https://github.com/elastic/eui/pull/1944))
- Added a webpack directive for naming icon chunks ([#1944](https://github.com/elastic/eui/pull/1944))
- Added ability to update `EuiInMemoryTable` `sorting` prop and remove columns after sorting is applied ([#1972](https://github.com/elastic/eui/pull/1972))
- Added `onToggle` callback to `EuiAccordion` ([#1974](https://github.com/elastic/eui/pull/1974))

**Bug fixes**

Expand Down
22 changes: 22 additions & 0 deletions src-docs/src/views/accordion/accordion_callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';

import { EuiAccordion, EuiText, EuiCode } from '../../../../src/components';

export default () => (
<div>
<EuiAccordion
id="accordion1"
buttonContent="I have an `onToggle` callback"
onToggle={isOpen =>
console.log(`EuiAccordion is now ${isOpen ? 'open' : 'closed'}`)
}
paddingSize="l">
<EuiText>
<p>
Any content inside of <EuiCode>EuiAccordion</EuiCode> will appear
here.
</p>
</EuiText>
</EuiAccordion>
</div>
);
33 changes: 33 additions & 0 deletions src-docs/src/views/accordion/accordion_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ const accordionOpenSnippet = `<EuiAccordion
</EuiAccordion>
`;

import AccordionCallback from './accordion_callback';
const accordionCallbackSource = require('!!raw-loader!./accordion_callback');
const accordionCallbackHtml = renderToHtml(AccordionCallback);
const accordionCallbackSnippet = `<EuiAccordion
id={accordionId}
buttonContent="Clickable title"
onToggle={isOpen => handleOnToggle(isOpen)}
>
<!-- Content to show when expanded -->
</EuiAccordion>
`;

import AccordionGrow from './accordion_grow';
const accordionGrowSource = require('!!raw-loader!./accordion_grow');
const accordionGrowHtml = renderToHtml(AccordionGrow);
Expand Down Expand Up @@ -204,6 +216,27 @@ export const AccordionExample = {
snippet: accordionOpenSnippet,
demo: <AccordionOpen />,
},
{
title: 'Accordion can call a function on open and close',
source: [
{
type: GuideSectionTypes.JS,
code: accordionCallbackSource,
},
{
type: GuideSectionTypes.HTML,
code: accordionCallbackHtml,
},
],
text: (
<p>
Use the <EuiCode>onToggle</EuiCode> prop to pass a callback method
that will be called on open and close.
</p>
),
snippet: accordionCallbackSnippet,
demo: <AccordionCallback />,
},
{
title: 'Accordion content can dynamically change height',
source: [
Expand Down
15 changes: 12 additions & 3 deletions src/components/accordion/accordion.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,14 @@ export class EuiAccordion extends Component {
}

onToggle() {
this.setState(prevState => ({
isOpen: !prevState.isOpen,
}));
this.setState(
prevState => ({
isOpen: !prevState.isOpen,
}),
() => {
this.props.onToggle && this.props.onToggle(this.state.isOpen);
}
);
}

setChildContentRef = node => {
Expand Down Expand Up @@ -183,6 +188,10 @@ EuiAccordion.propTypes = {
* The padding around the exposed accordion content.
*/
paddingSize: PropTypes.oneOf(PADDING_SIZES),
/**
* Optional callback method called on open and close with a single `isOpen` parameter
*/
onToggle: PropTypes.func,
};

EuiAccordion.defaultProps = {
Expand Down
15 changes: 15 additions & 0 deletions src/components/accordion/accordion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,20 @@ describe('EuiAccordion', () => {

expect(component).toMatchSnapshot();
});

it('accepts and calls an optional callback on open and close', () => {
const onToggleHandler = jest.fn();
const component = mount(
<EuiAccordion id={getId()} onToggle={onToggleHandler} />
);

component.find('button').simulate('click');
expect(onToggleHandler).toBeCalled();
expect(onToggleHandler).toBeCalledWith(true);

component.find('button').simulate('click');
expect(onToggleHandler).toBeCalled();
expect(onToggleHandler).toBeCalledWith(false);
});
});
});
3 changes: 2 additions & 1 deletion src/components/accordion/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ declare module '@elastic/eui' {
buttonContent?: ReactNode;
extraAction?: ReactNode;
initialIsOpen?: boolean;
onToggle?: (isOpen: boolean) => void;
paddingSize?: EuiAccordionSize;
}

export class EuiAccordion extends Component<
CommonProps & HTMLAttributes<HTMLDivElement> & EuiAccordionProps
> {}
> {}
}

0 comments on commit f903564

Please sign in to comment.