Skip to content

Commit fbc694f

Browse files
committed
Handle radio across shadowdom
1 parent 2b425e1 commit fbc694f

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[part=items] {
22
display: flex;
33
flex-direction: column;
4+
gap: 0.25rem;
45
}

src/components/pg/inputRadio/inputRadio.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ export default class PgInputRadio extends HTMLElement {
2323
container: this.$items,
2424
items: this.items,
2525
type: () => PgInputRadioItem,
26+
create: ($item: PgInputRadioItem) => {
27+
$item.addEventListener('change', () => {
28+
Array.from(this.$items.children).forEach(($ele: PgInputRadioItem) => {
29+
if ($ele === $item) {
30+
$ele.checked = true;
31+
} else {
32+
$ele.checked = false;
33+
}
34+
});
35+
});
36+
}
2637
});
2738
}
39+
40+
get radios() {
41+
return [...this.$items.children];
42+
}
2843
}

src/components/pg/inputRadioItem/inputRadioItem.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ label {
2323
svg {
2424
width: 1.5rem;
2525
height: 1.5rem;
26+
margin-left: -0.5rem;
2627
}

src/components/pg/inputRadioItem/inputRadioItem.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,26 @@ import style from './inputRadioItem.css';
1111
export default class PgInputRadioItem extends HTMLElement {
1212
@Prop() label: string = '';
1313
@Prop() value: string = '';
14+
@Prop() checked: boolean = false;
1415

1516
@Part() $input: HTMLInputElement;
1617
@Part() $label: HTMLSpanElement;
1718

19+
connectedCallback() {
20+
this.$input.addEventListener('change', () => {
21+
this.dispatchEvent(new CustomEvent('change'));
22+
});
23+
}
24+
1825
render(changes) {
1926
if (changes.label) {
2027
this.$label.textContent = this.label;
2128
}
2229
if (changes.value) {
2330
this.$input.textContent = this.label;
2431
}
32+
if (changes.checked) {
33+
this.$input.checked = this.checked;
34+
}
2535
}
2636
}

0 commit comments

Comments
 (0)