diff --git a/docs/example/Switch/demos/basicUsage.tsx b/docs/example/Switch/demos/basicUsage.tsx index a2be934d..44aa9511 100644 --- a/docs/example/Switch/demos/basicUsage.tsx +++ b/docs/example/Switch/demos/basicUsage.tsx @@ -5,5 +5,5 @@ import { Switch } from '@banana-ui/react'; export default function BasicUsage() { - return ; + return ; } diff --git a/docs/example/Switch/demos/disabled.tsx b/docs/example/Switch/demos/disabled.tsx index 16718942..ae8a4fba 100644 --- a/docs/example/Switch/demos/disabled.tsx +++ b/docs/example/Switch/demos/disabled.tsx @@ -13,8 +13,8 @@ export default function Disabled() { gap: '6px', }} > - - + + ); } diff --git a/docs/example/Switch/demos/size.tsx b/docs/example/Switch/demos/size.tsx index 397797f3..11f5c75f 100644 --- a/docs/example/Switch/demos/size.tsx +++ b/docs/example/Switch/demos/size.tsx @@ -14,8 +14,8 @@ export default function BasicUsage() { gap: '6px', }} > - - + + 自定义尺寸 + /> ); } diff --git a/docs/example/Switch/demos/withContent.tsx b/docs/example/Switch/demos/withContent.tsx index e30fa227..67498690 100644 --- a/docs/example/Switch/demos/withContent.tsx +++ b/docs/example/Switch/demos/withContent.tsx @@ -10,7 +10,7 @@ export default function WithContent() { // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore t="1711257906171" - class="icon" + className="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" @@ -42,7 +42,7 @@ export default function WithContent() { // eslint-disable-next-line @typescript-eslint/ban-ts-comment //@ts-ignore t="1711257880605" - class="icon" + className="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" diff --git a/docs/example/Switch/index.md b/docs/example/Switch/index.md index cb72be2a..db387fc7 100644 --- a/docs/example/Switch/index.md +++ b/docs/example/Switch/index.md @@ -18,6 +18,7 @@ demo: + ## 属性 - Attributes & Properties diff --git a/packages/banana-react/src/switch/index.ts b/packages/banana-react/src/switch/index.ts index 9d45dba0..a509f348 100644 --- a/packages/banana-react/src/switch/index.ts +++ b/packages/banana-react/src/switch/index.ts @@ -1,12 +1,18 @@ import { BSwitch } from '@banana-ui/banana'; -import { createComponent } from '@lit-labs/react'; +import { EventName, createComponent } from '@lit-labs/react'; import * as React from 'react'; +const events = { + onChange: 'change' as EventName< + CustomEvent<{ + value: boolean; + }> + >, +}; + export const Switch = createComponent({ tagName: 'b-switch', react: React, elementClass: BSwitch, - events: { - onChange: 'change', - }, + events, }); diff --git a/packages/banana/src/switch/index.test.ts b/packages/banana/src/switch/index.test.ts index a010c8fc..bcdf0a7f 100644 --- a/packages/banana/src/switch/index.test.ts +++ b/packages/banana/src/switch/index.test.ts @@ -115,27 +115,25 @@ describe('b-switch', () => { expect(spy.calledTwice).to.equal(true); }); - // it('should toggle the display text when provide slot to switch', async () => { - // const element = await fixture(html` - // open - // unOpen - // `); - // const clickElement = element.shadowRoot?.querySelector('.banana-switch') as HTMLLabelElement; - - // const checkedEl = element.shadowRoot?.querySelector('.switch__inner-checked') as HTMLLabelElement; - - // const unCheckedEl = element.shadowRoot?.querySelector('.switch__inner-unchecked') as HTMLLabelElement; - - // console.log('checkedEl', checkedEl); - - // // Change event should be fired. - // expect(unCheckedEl.textContent).equal('unOpen'); - // const spy = sinon.spy(); - // element.addEventListener('change', spy); - // clickElement.click(); - // await element.updateComplete; - // expect(checkedEl.textContent).equal('open'); - // }); + it('should toggle the display text when provide slot to switch', async () => { + const element = await fixture(html` + open + unOpen + `); + const clickElement = element.shadowRoot?.querySelector('.switch__inner') as HTMLLabelElement; + + const checkedEl = element.shadowRoot?.querySelector('slot[name="checked"]') as HTMLSlotElement; + + const unCheckedEl = element.shadowRoot?.querySelector('slot[name="unchecked"]') as HTMLSlotElement; + + // Change event should be fired. + expect(unCheckedEl.assignedElements()[0].textContent).equal('unOpen'); + const spy = sinon.spy(); + element.addEventListener('change', spy); + clickElement.click(); + await element.updateComplete; + expect(checkedEl.assignedElements()[0].textContent).equal('open'); + }); }); describe('form', () => { diff --git a/packages/banana/src/switch/index.ts b/packages/banana/src/switch/index.ts index 8a7671ad..147ce37f 100644 --- a/packages/banana/src/switch/index.ts +++ b/packages/banana/src/switch/index.ts @@ -77,6 +77,13 @@ export default class BSwitch this.dispatchEvent(new CustomEvent('change', eventOptions)); } + private _handleKeyDown(event: KeyboardEvent) { + if (event.key === 'Enter' || event.key === ' ') { + event.preventDefault(); + this._handleChange(); + } + } + protected firstUpdated(): void { if (!this.checked) { this.checked = this.defaultChecked; @@ -97,24 +104,20 @@ export default class BSwitch } render() { - // eslint-disable-next-line lit-a11y/click-events-have-key-events return html`