diff --git a/packages/components/src/components/bal-accordion/bal-accordion.tsx b/packages/components/src/components/bal-accordion/bal-accordion.tsx index dd072b3796..bfb484f3e6 100644 --- a/packages/components/src/components/bal-accordion/bal-accordion.tsx +++ b/packages/components/src/components/bal-accordion/bal-accordion.tsx @@ -1,10 +1,13 @@ -import { Component, Host, h, Prop, Method, Event, EventEmitter } from '@stencil/core' +import { Component, Host, h, Prop, Method, Event, EventEmitter, Watch } from '@stencil/core' +import { debounceEvent } from '../../helpers/helpers' import { ColorTypesBasic, BalButtonColor } from '../../types/color.types' @Component({ tag: 'bal-accordion', }) export class Accordion { + private didInit = false + /** * Type defines the theme of the accordion toggle */ @@ -13,7 +16,27 @@ export class Accordion { /** * Controls if the accordion is collapsed or not */ - @Prop({ mutable: true, reflect: true }) isActive = false + @Prop({ mutable: true, reflect: true }) value = false + + /** + * Update the native input element when the value changes + */ + @Watch('value') + protected async valueChanged(newValue: boolean, oldValue: boolean) { + if (this.didInit && newValue !== oldValue) { + this.balChange.emit(newValue) + } + } + + /** + * Set the amount of time, in milliseconds, to wait to trigger the `balChange` event after each keystroke. This also impacts form bindings such as `ngModel` or `v-model`. + */ + @Prop() debounce = 0 + + @Watch('debounce') + protected debounceChanged() { + this.balChange = debounceEvent(this.balChange, this.debounce) + } /** * Label of the open trigger button @@ -43,24 +66,35 @@ export class Accordion { /** * Emmited when the accordion has changed */ - @Event() balCollapse!: EventEmitter + @Event() balChange!: EventEmitter + + connectedCallback() { + this.debounceChanged() + } + + componentDidLoad() { + this.didInit = true + if (this.value !== undefined) { + this.valueChanged(this.value, false) + } + } /** * Open the accordion */ @Method() - async open() { - this.isActive = true - this.balCollapse.emit(this.isActive) + async present() { + this.value = true + // this.balChange.emit(this.value) } /** * Close the accordion */ @Method() - async close() { - this.isActive = false - this.balCollapse.emit(this.isActive) + async dismiss() { + this.value = false + // this.balChange.emit(this.value) } /** @@ -68,8 +102,8 @@ export class Accordion { */ @Method() async toggle() { - this.isActive = !this.isActive - this.balCollapse.emit(this.isActive) + this.value = !this.value + // this.balChange.emit(this.value) } get buttonType(): BalButtonColor { @@ -77,11 +111,11 @@ export class Accordion { } get label() { - return this.isActive ? this.closeLabel : this.openLabel + return this.value ? this.closeLabel : this.openLabel } get icon() { - return this.isActive ? this.closeIcon : this.openIcon + return this.value ? this.closeIcon : this.openIcon } render() { @@ -94,13 +128,13 @@ export class Accordion { icon={this.icon} onClick={() => this.toggle()} top-rounded={!this.card} - bottomRounded={!this.isActive} + bottomRounded={!this.value} > {this.label}
diff --git a/packages/components/src/components/bal-accordion/stories/bal-accordion.stories.ts b/packages/components/src/components/bal-accordion/stories/bal-accordion.stories.ts index 349199d2f5..af41efabc7 100644 --- a/packages/components/src/components/bal-accordion/stories/bal-accordion.stories.ts +++ b/packages/components/src/components/bal-accordion/stories/bal-accordion.stories.ts @@ -15,7 +15,7 @@ export default component.story export const Basic = args => ({ components: { ...component.components }, setup: () => ({ args }), - template: ` + template: `

{{ args.content }}

diff --git a/packages/components/src/components/bal-accordion/test/bal-accordion.e2e.ts b/packages/components/src/components/bal-accordion/test/bal-accordion.e2e.ts index 4c0719ed2c..bd3c7cd3ba 100644 --- a/packages/components/src/components/bal-accordion/test/bal-accordion.e2e.ts +++ b/packages/components/src/components/bal-accordion/test/bal-accordion.e2e.ts @@ -10,7 +10,7 @@ describe('bal-accordion', () => { await page.setContent(` TEST CONTENT `) - balCollapseEvent = await page.spyOnEvent('balCollapse') + balCollapseEvent = await page.spyOnEvent('balChange') balAccordionTriggerElement = await page.find('bal-accordion bal-button') })