Skip to content

Commit b3a1f91

Browse files
daKmoRJoren Broekema
authored andcommitted
fix(button): sync type property instead of delegating
1 parent eb1e295 commit b3a1f91

File tree

2 files changed

+35
-24
lines changed

2 files changed

+35
-24
lines changed

packages/button/src/LionButton.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { css, html, DelegateMixin, SlotMixin, DisabledWithTabIndexMixin } from '@lion/core';
2-
import { LionLitElement } from '@lion/core/src/LionLitElement.js';
1+
import { css, html, SlotMixin, DisabledWithTabIndexMixin, LitElement } from '@lion/core';
32

4-
export class LionButton extends DisabledWithTabIndexMixin(
5-
DelegateMixin(SlotMixin(LionLitElement)),
6-
) {
3+
export class LionButton extends DisabledWithTabIndexMixin(SlotMixin(LitElement)) {
74
static get properties() {
85
return {
96
role: {
@@ -14,6 +11,10 @@ export class LionButton extends DisabledWithTabIndexMixin(
1411
type: Boolean,
1512
reflect: true,
1613
},
14+
type: {
15+
type: String,
16+
reflect: true,
17+
},
1718
};
1819
}
1920

@@ -107,14 +108,6 @@ export class LionButton extends DisabledWithTabIndexMixin(
107108
];
108109
}
109110

110-
get delegations() {
111-
return {
112-
...super.delegations,
113-
target: () => this.$$slot('_button'),
114-
attributes: ['type'],
115-
};
116-
}
117-
118111
get slots() {
119112
return {
120113
...super.slots,
@@ -129,9 +122,14 @@ export class LionButton extends DisabledWithTabIndexMixin(
129122
};
130123
}
131124

125+
get _nativeButtonNode() {
126+
return this.querySelector('[slot=_button]');
127+
}
128+
132129
constructor() {
133130
super();
134131
this.role = 'button';
132+
this.type = 'submit';
135133
this.active = false;
136134
this.__setupDelegationInConstructor();
137135
}
@@ -146,12 +144,22 @@ export class LionButton extends DisabledWithTabIndexMixin(
146144
this.__teardownEvents();
147145
}
148146

147+
updated(changedProperties) {
148+
super.updated(changedProperties);
149+
if (changedProperties.has('type')) {
150+
const native = this._nativeButtonNode;
151+
if (native) {
152+
native.type = this.type;
153+
}
154+
}
155+
}
156+
149157
_redispatchClickEvent(oldEvent) {
150158
// replacing `MouseEvent` with `oldEvent.constructor` breaks IE
151159
const newEvent = new MouseEvent(oldEvent.type, oldEvent);
152160
newEvent.__isRedispatchedOnNativeButton = true;
153161
this.__enforceHostEventTarget(newEvent);
154-
this.$$slot('_button').dispatchEvent(newEvent);
162+
this._nativeButtonNode.dispatchEvent(newEvent);
155163
}
156164

157165
/**

packages/button/test/lion-button.test.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,26 @@ describe('lion-button', () => {
2727
expect(el.getAttribute('tabindex')).to.equal('0');
2828
});
2929

30-
it('has no type by default on the native button', async () => {
30+
it('has .type="submit" and type="submit" by default', async () => {
3131
const el = await fixture(`<lion-button>foo</lion-button>`);
32-
const nativeButton = el.$$slot('_button');
33-
expect(nativeButton.getAttribute('type')).to.be.null;
32+
expect(el.type).to.equal('submit');
33+
expect(el.getAttribute('type')).to.be.equal('submit');
34+
expect(el._nativeButtonNode.type).to.equal('submit');
35+
expect(el._nativeButtonNode.getAttribute('type')).to.be.equal('submit');
3436
});
3537

36-
it('has type="submit" on the native button when set', async () => {
37-
const el = await fixture(`<lion-button type="submit">foo</lion-button>`);
38-
const nativeButton = el.$$slot('_button');
39-
expect(nativeButton.getAttribute('type')).to.equal('submit');
38+
it('sync type down to the native button', async () => {
39+
const el = await fixture(`<lion-button type="button">foo</lion-button>`);
40+
expect(el.type).to.equal('button');
41+
expect(el.getAttribute('type')).to.be.equal('button');
42+
expect(el._nativeButtonNode.type).to.equal('button');
43+
expect(el._nativeButtonNode.getAttribute('type')).to.be.equal('button');
4044
});
4145

4246
it('hides the native button in the UI', async () => {
4347
const el = await fixture(`<lion-button>foo</lion-button>`);
44-
const nativeButton = el.$$slot('_button');
45-
expect(nativeButton.getAttribute('tabindex')).to.equal('-1');
46-
expect(window.getComputedStyle(nativeButton).visibility).to.equal('hidden');
48+
expect(el._nativeButtonNode.getAttribute('tabindex')).to.equal('-1');
49+
expect(window.getComputedStyle(el._nativeButtonNode).visibility).to.equal('hidden');
4750
});
4851

4952
it('can be disabled imperatively', async () => {

0 commit comments

Comments
 (0)