Skip to content

Commit b0f5f6f

Browse files
authored
experiment: add LitElement based version of radio-group (#6682)
1 parent c1d2e1f commit b0f5f6f

18 files changed

Lines changed: 242 additions & 38 deletions

‎packages/radio-group/package.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
"type": "module",
2222
"files": [
2323
"src",
24+
"!src/vaadin-lit-radio-button.d.ts",
25+
"!src/vaadin-lit-radio-button.js",
26+
"!src/vaadin-lit-radio-group.d.ts",
27+
"!src/vaadin-lit-radio-group.js",
2428
"theme",
2529
"vaadin-*.d.ts",
2630
"vaadin-*.js",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2017 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
export * from './vaadin-radio-button.js';
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2017 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import { html, LitElement } from 'lit';
7+
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
8+
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
9+
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
10+
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
11+
import { RadioButtonMixin } from './vaadin-radio-button-mixin.js';
12+
import { radioButtonStyles } from './vaadin-radio-button-styles.js';
13+
14+
/**
15+
* LitElement based version of `<vaadin-radio-button>` web component.
16+
*
17+
* ## Disclaimer
18+
*
19+
* This component is an experiment not intended for publishing to npm.
20+
* There is no ETA regarding specific Vaadin version where it'll land.
21+
* Feel free to try this code in your apps as per Apache 2.0 license.
22+
*/
23+
class RadioButton extends RadioButtonMixin(ElementMixin(ThemableMixin(PolylitMixin(LitElement)))) {
24+
static get is() {
25+
return 'vaadin-radio-button';
26+
}
27+
28+
static get styles() {
29+
return radioButtonStyles;
30+
}
31+
32+
static get properties() {
33+
return {
34+
/**
35+
* True if the element is checked.
36+
* @type {boolean}
37+
*/
38+
checked: {
39+
type: Boolean,
40+
value: false,
41+
notify: true,
42+
reflectToAttribute: true,
43+
sync: true,
44+
},
45+
};
46+
}
47+
48+
/** @protected */
49+
render() {
50+
return html`
51+
<div class="vaadin-radio-button-container">
52+
<div part="radio" aria-hidden="true"></div>
53+
<slot name="input"></slot>
54+
<slot name="label"></slot>
55+
</div>
56+
`;
57+
}
58+
}
59+
60+
defineCustomElement(RadioButton);
61+
62+
export { RadioButton };
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2017 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
export * from './vaadin-radio-group.js';
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @license
3+
* Copyright (c) 2017 - 2023 Vaadin Ltd.
4+
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5+
*/
6+
import './vaadin-lit-radio-button.js';
7+
import { html, LitElement } from 'lit';
8+
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
9+
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
10+
import { PolylitMixin } from '@vaadin/component-base/src/polylit-mixin.js';
11+
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
12+
import { RadioGroupMixin } from './vaadin-radio-group-mixin.js';
13+
import { radioGroupStyles } from './vaadin-radio-group-styles.js';
14+
15+
/**
16+
* LitElement based version of `<vaadin-radio-group>` web component.
17+
*
18+
* ## Disclaimer
19+
*
20+
* This component is an experiment not intended for publishing to npm.
21+
* There is no ETA regarding specific Vaadin version where it'll land.
22+
* Feel free to try this code in your apps as per Apache 2.0 license.
23+
*/
24+
class RadioGroup extends RadioGroupMixin(ElementMixin(ThemableMixin(PolylitMixin(LitElement)))) {
25+
static get is() {
26+
return 'vaadin-radio-group';
27+
}
28+
29+
static get styles() {
30+
return radioGroupStyles;
31+
}
32+
33+
/** @protected */
34+
render() {
35+
return html`
36+
<div class="vaadin-group-field-container">
37+
<div part="label">
38+
<slot name="label"></slot>
39+
<span part="required-indicator" aria-hidden="true"></span>
40+
</div>
41+
42+
<div part="group-field">
43+
<slot></slot>
44+
</div>
45+
46+
<div part="helper-text">
47+
<slot name="helper"></slot>
48+
</div>
49+
50+
<div part="error-message">
51+
<slot name="error-message"></slot>
52+
</div>
53+
</div>
54+
55+
<slot name="tooltip"></slot>
56+
`;
57+
}
58+
}
59+
60+
defineCustomElement(RadioGroup);
61+
62+
export { RadioGroup };

‎packages/radio-group/src/vaadin-radio-group.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2017 - 2023 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6+
import './vaadin-radio-button.js';
67
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
78
import { defineCustomElement } from '@vaadin/component-base/src/define.js';
89
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import '../src/vaadin-lit-radio-button.js';
2+
import './radio-button.common.js';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import '../src/vaadin-radio-button.js';
2+
import './radio-button.common.js';

packages/radio-group/test/radio-button.test.js renamed to packages/radio-group/test/radio-button.common.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { expect } from '@esm-bundle/chai';
2-
import { fire, fixtureSync, mousedown, mouseup, nextFrame } from '@vaadin/testing-helpers';
2+
import { fire, fixtureSync, mousedown, mouseup, nextFrame, nextRender, nextUpdate } from '@vaadin/testing-helpers';
33
import { resetMouse, sendKeys, sendMouse } from '@web/test-runner-commands';
44
import sinon from 'sinon';
5-
import '../vaadin-radio-button.js';
65

76
describe('radio-button', () => {
87
let radio, input, label;
@@ -25,8 +24,9 @@ describe('radio-button', () => {
2524
});
2625

2726
describe('default', () => {
28-
beforeEach(() => {
27+
beforeEach(async () => {
2928
radio = fixtureSync('<vaadin-radio-button label="Label"></vaadin-radio-button>');
29+
await nextRender();
3030
label = radio.querySelector('[slot=label]');
3131
});
3232

@@ -44,8 +44,9 @@ describe('radio-button', () => {
4444
});
4545

4646
describe('native input', () => {
47-
beforeEach(() => {
47+
beforeEach(async () => {
4848
radio = fixtureSync('<vaadin-radio-button></vaadin-radio-button>');
49+
await nextRender();
4950
input = radio.querySelector('[slot=input]');
5051
label = radio.querySelector('[slot=label]');
5152
});
@@ -110,8 +111,9 @@ describe('radio-button', () => {
110111
});
111112

112113
describe('disabled attribute', () => {
113-
beforeEach(() => {
114+
beforeEach(async () => {
114115
radio = fixtureSync('<vaadin-radio-button disabled></vaadin-radio-button>');
116+
await nextRender();
115117
input = radio.querySelector('[slot=input]');
116118
label = radio.querySelector('[slot=label]');
117119
});
@@ -138,8 +140,9 @@ describe('radio-button', () => {
138140

139141
// TODO: A legacy suit. Replace with snapshot tests when possible.
140142
describe('active attribute', () => {
141-
beforeEach(() => {
143+
beforeEach(async () => {
142144
radio = fixtureSync('<vaadin-radio-button></vaadin-radio-button>');
145+
await nextRender();
143146
input = radio.querySelector('[slot=input]');
144147
});
145148

@@ -167,8 +170,9 @@ describe('radio-button', () => {
167170
describe('change event', () => {
168171
let spy;
169172

170-
beforeEach(() => {
173+
beforeEach(async () => {
171174
radio = fixtureSync('<vaadin-radio-button></vaadin-radio-button>');
175+
await nextRender();
172176
label = radio.querySelector('[slot=label]');
173177
input = radio.querySelector('[slot=input]');
174178

@@ -195,19 +199,22 @@ describe('radio-button', () => {
195199
expect(spy.calledOnce).to.be.true;
196200
});
197201

198-
it('should not fire on programmatic toggle', () => {
202+
it('should not fire on programmatic toggle', async () => {
199203
radio.checked = true;
204+
await nextUpdate(radio);
200205
expect(spy.called).to.be.false;
201206
});
202207

203-
it('should not fire on input click when checked', () => {
208+
it('should not fire on input click when checked', async () => {
204209
radio.checked = true;
210+
await nextUpdate(radio);
205211
input.click();
206212
expect(spy.called).to.be.false;
207213
});
208214

209-
it('should not fire on label click when checked', () => {
215+
it('should not fire on label click when checked', async () => {
210216
radio.checked = true;
217+
await nextUpdate(radio);
211218
label.click();
212219
expect(spy.called).to.be.false;
213220
});
@@ -227,23 +234,26 @@ describe('radio-button', () => {
227234

228235
describe('delegation', () => {
229236
describe('name attribute', () => {
230-
beforeEach(() => {
237+
beforeEach(async () => {
231238
radio = fixtureSync(`<vaadin-radio-button name="Name"></vaadin-radio-button>`);
239+
await nextRender();
232240
input = radio.querySelector('[slot=input]');
233241
});
234242

235-
it('should delegate name attribute to the input', () => {
243+
it('should delegate name attribute to the input', async () => {
236244
expect(input.getAttribute('name')).to.equal('Name');
237245

238246
radio.removeAttribute('name');
247+
await nextUpdate(radio);
239248
expect(input.hasAttribute('name')).to.be.false;
240249
});
241250
});
242251
});
243252

244253
describe('has-label attribute', () => {
245-
beforeEach(() => {
254+
beforeEach(async () => {
246255
radio = fixtureSync('<vaadin-radio-button></vaadin-radio-button>');
256+
await nextRender();
247257
});
248258

249259
it('should not set has-label attribute when label content is empty', () => {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import '../src/vaadin-lit-radio-group.js';
2+
import './radio-group-keyboard-navigation.common.js';

0 commit comments

Comments
 (0)