From 640083bd1f3fa146d6f9d823971f21f1c91f9749 Mon Sep 17 00:00:00 2001 From: Greg Thompson Date: Mon, 15 Jun 2020 14:42:12 -0600 Subject: [PATCH 1/4] call onToggle --- .../src/views/accordion/accordion_example.js | 5 +++-- .../views/accordion/accordion_forceState.js | 7 +++++++ src/components/accordion/accordion.tsx | 21 +++++++++++-------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src-docs/src/views/accordion/accordion_example.js b/src-docs/src/views/accordion/accordion_example.js index e1e19e1a241..2a3ea4b4a9e 100644 --- a/src-docs/src/views/accordion/accordion_example.js +++ b/src-docs/src/views/accordion/accordion_example.js @@ -332,7 +332,7 @@ export const AccordionExample = { demo: , }, { - title: 'Force accordion state', + title: 'External state control', source: [ { type: GuideSectionTypes.JS, @@ -346,7 +346,8 @@ export const AccordionExample = { text: (

Use the forceState prop to control open and close - state. + state. The onToggle callback prop can still be used + to update external state or perform side effects.

), snippet: accordionForceStateSnippet, diff --git a/src-docs/src/views/accordion/accordion_forceState.js b/src-docs/src/views/accordion/accordion_forceState.js index 7d3ee0335d1..b99843692a6 100644 --- a/src-docs/src/views/accordion/accordion_forceState.js +++ b/src-docs/src/views/accordion/accordion_forceState.js @@ -29,6 +29,12 @@ export default () => { setID(id); }; + const onToggle = isOpen => { + const newState = isOpen ? 'closed' : 'open'; + setTrigger(newState); + setID(`${idPrefix}--${newState}`); + }; + return (
{

diff --git a/src/components/accordion/accordion.tsx b/src/components/accordion/accordion.tsx index ab0b7e2549d..f392762c7c0 100644 --- a/src/components/accordion/accordion.tsx +++ b/src/components/accordion/accordion.tsx @@ -121,15 +121,18 @@ export class EuiAccordion extends Component< onToggle = () => { const { forceState } = this.props; - if (forceState) return this.setState({ isOpen: forceState === 'open' }); - this.setState( - prevState => ({ - isOpen: !prevState.isOpen, - }), - () => { - this.props.onToggle && this.props.onToggle(this.state.isOpen); - } - ); + if (forceState) { + this.props.onToggle && this.props.onToggle(forceState === 'open'); + } else { + this.setState( + prevState => ({ + isOpen: !prevState.isOpen, + }), + () => { + this.props.onToggle && this.props.onToggle(this.state.isOpen); + } + ); + } }; setChildContentRef = (node: HTMLDivElement | null) => { From cafdefbd03a9f3d98e76e4bd6fa540f02f44f04b Mon Sep 17 00:00:00 2001 From: Greg Thompson Date: Mon, 15 Jun 2020 14:43:17 -0600 Subject: [PATCH 2/4] test it --- .../__snapshots__/accordion.test.tsx.snap | 12 ++++++------ src/components/accordion/accordion.test.tsx | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/components/accordion/__snapshots__/accordion.test.tsx.snap b/src/components/accordion/__snapshots__/accordion.test.tsx.snap index edc08525519..3e52ffc5477 100644 --- a/src/components/accordion/__snapshots__/accordion.test.tsx.snap +++ b/src/components/accordion/__snapshots__/accordion.test.tsx.snap @@ -3,7 +3,7 @@ exports[`EuiAccordion behavior closes when clicked twice 1`] = ` @@ -14,7 +14,7 @@ exports[`EuiAccordion behavior closes when clicked twice 1`] = ` className="euiAccordion__triggerWrapper" >