Skip to content

Commit b568977

Browse files
committed
chore: radio button click functionality implemented
1 parent 693f7af commit b568977

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

elements/pf-radio/pf-radio.ts

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,59 @@ import { property } from 'lit/decorators/property.js';
1111
@customElement('pf-radio')
1212
export class PfRadio extends LitElement {
1313
static readonly styles: CSSStyleSheet[] = [styles];
14-
@property() checked = false;
14+
static formAssociated = true;
15+
static shadowRootOptions: ShadowRootInit = {
16+
...LitElement.shadowRootOptions,
17+
delegatesFocus: true,
18+
};
19+
20+
@property({
21+
type: Boolean,
22+
// attribute: 'inline-filter',
23+
converter: {
24+
fromAttribute: value => value === 'true',
25+
},
26+
reflect: true,
27+
})
28+
checked = false;
29+
1530
@property({ reflect: true }) name = 'radio-test';
1631
@property({ reflect: true }) label?: string;
17-
// #input:any
32+
@property({ reflect: true }) value = '';
1833

1934
constructor() {
2035
super();
2136
}
2237

2338
connectedCallback(): void {
2439
super.connectedCallback();
40+
}
2541

26-
const root = this.getRootNode();
27-
if (root instanceof Document || root instanceof ShadowRoot) {
28-
const group = root.querySelectorAll(`pf-radio`);
29-
// console.log("------------- the group is", group);
42+
#onRadioButtonClick(event: Event) {
43+
if (!this.checked) {
44+
const root: Node = this.getRootNode();
45+
let radioGroup: NodeListOf<PfRadio>;
46+
if (root instanceof Document || root instanceof ShadowRoot) {
47+
radioGroup = root.querySelectorAll('pf-radio');
48+
radioGroup.forEach(radio => {
49+
const element: HTMLElement = radio as HTMLElement;
50+
element?.removeAttribute('checked');
51+
});
52+
this.checked = true;
53+
}
3054
}
3155
}
3256

33-
3457
render(): TemplateResult<1> {
3558
return html`
36-
<label for=input>${this.label}</label>
37-
<input id=input class="pf-radio" .name=${this.name} type="radio" .checked="${this.checked}">
59+
<label for='input'>${this.label}</label>
60+
<input
61+
@click=${(e: Event) => this.#onRadioButtonClick(e)}
62+
id='input'
63+
.name=${this.name}
64+
type='radio'
65+
.checked='${this.checked}'
66+
/>
3867
`;
3968
}
4069
}

0 commit comments

Comments
 (0)