Skip to content

Commit acdd7e2

Browse files
saraparsa13Sara.Parsaee
and
Sara.Parsaee
authored
fix: change tap-radio-group to accept children rather than tap-radio (#181)
* fix: change tap-radio-group to accept children rather than tap-radio * chore: change tap-radio-group storybook * chore: change type in the tap-radio-group * feat: add test cases for the relative scenario * chore: remove unused var * chore: change test case name * revert bottom sheet changes * fix: change the getter of radios to find radios recursively * fix: use queryAssignedElements for getting elements --------- Co-authored-by: Sara.Parsaee <Sara.Parsaee@Tapsi.cab>
1 parent 00f736f commit acdd7e2

File tree

4 files changed

+126
-3
lines changed

4 files changed

+126
-3
lines changed

src/radio-group/radio-group.stories.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { html, TemplateResult } from 'lit';
22
import '../radio';
3+
import '../row';
34
import './index.js';
45
import { Meta } from '@storybook/web-components';
56

@@ -30,8 +31,19 @@ interface ArgTypes {
3031

3132
const Template: Story<ArgTypes> = ({ value, direction }: ArgTypes) => html`
3233
<tap-radio-group value="${value}" direction="${direction}">
33-
<tap-radio value="1"></tap-radio>
34-
<tap-radio value="2"></tap-radio>
34+
<tap-row>
35+
<div slot="leading"><tap-radio value="1"></tap-radio></div>
36+
<div slot="content">
37+
<span>Label for Option 1</span>
38+
</div>
39+
</tap-row>
40+
<tap-row>
41+
<div slot="content">
42+
<tap-radio value="2"></tap-radio>
43+
<span>Label for Option 2</span>
44+
</div>
45+
</tap-row>
46+
3547
<tap-radio value="3"></tap-radio>
3648
<tap-radio value="4"></tap-radio>
3749
</tap-radio-group>

src/radio-group/radio-group.test.ts

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import '../../dist/radio-group/index.js';
2+
import '../../dist/radio/index.js';
3+
4+
import { expect, fixture, html } from '@open-wc/testing';
5+
import { RadioGroup } from './radio-group.js';
6+
7+
describe('tap-radio-group component', () => {
8+
const value = '2';
9+
it('should select the correct radio button based on the initial value', async () => {
10+
const element = await fixture<RadioGroup>(html`
11+
<tap-radio-group value=${value}>
12+
<tap-radio value="1"></tap-radio>
13+
<tap-radio value="2"></tap-radio>
14+
<tap-radio value="3"></tap-radio>
15+
</tap-radio-group>
16+
`);
17+
18+
const radios = element.radios;
19+
20+
expect(radios.length).to.equal(3);
21+
expect(radios.find(radio => radio.value === value)?.checked).to.be.true;
22+
});
23+
24+
it('should select a radio button when a nested element is passed', async () => {
25+
const rootElement = await fixture<RadioGroup>(html`
26+
<tap-radio-group value="2">
27+
<div>
28+
<tap-radio value="1"></tap-radio>
29+
</div>
30+
<div>
31+
<tap-radio value="2"></tap-radio>
32+
</div>
33+
<div>
34+
<tap-radio value="3"></tap-radio>
35+
</div>
36+
</tap-radio-group>
37+
`);
38+
39+
const slot = rootElement.shadowRoot?.querySelector('slot');
40+
const elements = slot!.assignedElements({ flatten: true });
41+
42+
// TODO: use the element.radios
43+
const radios = elements.flatMap(element => {
44+
if (element.nodeType === Node.ELEMENT_NODE) {
45+
const el = element as HTMLElement;
46+
const foundRadios = Array.from(el.querySelectorAll('tap-radio'));
47+
return foundRadios;
48+
}
49+
return [];
50+
});
51+
expect(radios.length).to.equal(3);
52+
expect(radios.find(radio => radio.value === value)?.value).to.equal(value);
53+
});
54+
55+
it('should handle radio buttons nested inside other elements', async () => {
56+
const rootElement = await fixture<RadioGroup>(html`
57+
<tap-radio-group value=${value}>
58+
<tap-row>
59+
<div slot="leading"><tap-radio value="1"></tap-radio></div>
60+
<div slot="content"><span>Label 1</span></div>
61+
</tap-row>
62+
<tap-row>
63+
<div slot="content">
64+
<tap-radio value="2"></tap-radio>
65+
<span>Label 2</span>
66+
</div>
67+
</tap-row>
68+
<tap-row>
69+
<div slot="content">
70+
<tap-radio value="3"></tap-radio>
71+
<span>Label 3</span>
72+
</div>
73+
</tap-row>
74+
</tap-radio-group>
75+
`);
76+
77+
const slot = rootElement.shadowRoot?.querySelector('slot');
78+
const elements = slot!.assignedElements({ flatten: true });
79+
80+
// TODO: use the element.radios
81+
const radios = elements.flatMap(element => {
82+
if (element.nodeType === Node.ELEMENT_NODE) {
83+
const el = element as HTMLElement;
84+
const foundRadios = Array.from(el.querySelectorAll('tap-radio'));
85+
return foundRadios;
86+
}
87+
return [];
88+
});
89+
expect(radios.length).to.equal(3);
90+
expect(radios.find(radio => radio.value === value)?.value).to.equal(value);
91+
});
92+
});

src/radio-group/radio-group.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,31 @@ export class RadioGroup extends LitElement {
77
'vertical';
88
@property({ reflect: true }) value = '';
99

10-
@queryAssignedElements() private radios!: Radio[];
10+
@queryAssignedElements() private elements!: Element[];
1111

1212
connectedCallback() {
1313
super.connectedCallback();
1414
this.addEventListener('radio-input-change', this.handleRadioChangeClick);
1515
}
1616

17+
public get radios(): Radio[] {
18+
const findRadios = (nodes: Element[]): Radio[] => {
19+
let radios: Radio[] = [];
20+
nodes.forEach((node) => {
21+
if (node instanceof Radio) {
22+
radios.push(node);
23+
} else if (node instanceof HTMLElement) {
24+
radios = radios.concat(
25+
findRadios(Array.from(node.querySelectorAll('tap-radio')))
26+
);
27+
}
28+
});
29+
return radios;
30+
};
31+
32+
return findRadios(this.elements);
33+
}
34+
1735
private selectDefaultOption() {
1836
if (!this.value) return;
1937
const selectedRadio = this.radios.find(

src/radio/radio.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export class Radio extends LitElement {
66
...LitElement.shadowRootOptions,
77
delegatesFocus: true,
88
};
9+
static readonly formAssociated = true;
910

1011
@property({ type: Boolean, reflect: true }) checked = false;
1112

0 commit comments

Comments
 (0)