Skip to content

Commit

Permalink
Expose close function to Accordion::Item
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ju committed Jun 19, 2024
1 parent 6cc1757 commit 211a212
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</div>
</:toggle>

<:content>
<:content as |c|>
<Hds::Text::Body
class="hds-accordion-item__content"
@tag="div"
Expand All @@ -35,7 +35,7 @@
@color="primary"
id={{this.contentId}}
>
{{yield to="content"}}
{{yield (hash close=c.close) to="content"}}
</Hds::Text::Body>
</:content>
</Hds::DisclosurePrimitive>
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ export interface HdsAccordionItemSignature {
forceState?: 'open' | 'close';
};
Blocks: {
content?: [];
toggle?: [];
content: [
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
close: (...args: any[]) => void;
}
];
};
Element: HTMLElement;
}
Expand Down
15 changes: 14 additions & 1 deletion showcase/app/templates/components/accordion.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@

<Shw::Grid {{style gap="2rem"}} @columns={{2}} as |SG|>
<SG.Item @label="Forced state">
<button {{style padding=".25rem" marginBottom="1rem"}} type="button" {{on "click" this.toggleState}}>
<button type="button" {{style padding=".25rem" marginBottom="1rem"}} {{on "click" this.toggleState}}>
{{if (eq this.state "open") "Collapse all" "Expand all"}}
</button>
<Hds::Accordion @forceState={{this.state}} as |A|>
Expand Down Expand Up @@ -401,6 +401,19 @@
</SF.Item>
</Shw::Flex>

<Shw::Text::Body><strong>close</strong></Shw::Text::Body>

<Shw::Flex {{style gap="2rem" marginBottom="2rem"}} @direction="column" as |SF|>
<SF.Item @label="Close action within content">
<Hds::Accordion::Item>
<:toggle>Item one</:toggle>
<:content as |c|>
<button type="button" {{style padding=".25rem" marginBottom="1rem"}} {{on "click" c.close}}>Close</button>
</:content>
</Hds::Accordion::Item>
</SF.Item>
</Shw::Flex>

<Shw::Divider @level={{2}} />

<Shw::Text::H3>Accordion::Item::Button</Shw::Text::H3>
Expand Down
19 changes: 19 additions & 0 deletions showcase/tests/integration/components/hds/accordion/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,23 @@ module('Integration | Component | hds/accordion/index', function (hooks) {
await click('.hds-accordion-item__button');
assert.dom('.hds-accordion-item__content').exists({ count: 1 });
});

// close

test('it should hide the content when an accordion item triggers `close`', async function (assert) {
await render(hbs`
<Hds::Accordion::Item>
<:toggle>Item one</:toggle>
<:content as |c|>
<button type="button" {{on "click" c.close}}>Close</button>
</:content>
</Hds::Accordion::Item>
`);
await click('.hds-accordion-item__button');
assert.dom('.hds-accordion-item__content').exists();

await click('.hds-accordion-item__content button');
assert.dom('.hds-accordion-item__content').doesNotExist();
assert.dom('.hds-accordion-item__content button').doesNotExist();
});
});

0 comments on commit 211a212

Please sign in to comment.