private setupRadioButtons()

in packages/web-components/fast-foundation/src/radio-group/radio-group.ts [157:220]


    private setupRadioButtons(): void {
        const checkedRadios: HTMLElement[] = this.slottedRadioButtons.filter(
            (radio: HTMLInputElement) => {
                return radio.hasAttribute("checked");
            }
        );
        const numberOfCheckedRadios: number = checkedRadios ? checkedRadios.length : 0;
        if (numberOfCheckedRadios > 1) {
            const lastCheckedRadio: HTMLInputElement = checkedRadios[
                numberOfCheckedRadios - 1
            ] as HTMLInputElement;
            lastCheckedRadio.checked = true;
        }
        let foundMatchingVal: boolean = false;

        this.slottedRadioButtons.forEach((radio: HTMLInputElement) => {
            if (this.name !== undefined) {
                radio.setAttribute("name", this.name);
            }

            if (this.disabled) {
                radio.disabled = true;
            }

            if (this.readOnly) {
                radio.readOnly = true;
            }

            if (this.value && this.value === radio.value) {
                this.selectedRadio = radio;
                this.focusedRadio = radio;
                radio.checked = true;
                radio.setAttribute("tabindex", "0");
                foundMatchingVal = true;
            } else {
                if (!this.isInsideFoundationToolbar) {
                    radio.setAttribute("tabindex", "-1");
                }
                radio.checked = false;
            }
            radio.addEventListener("change", this.radioChangeHandler);
        });

        if (this.value === undefined && this.slottedRadioButtons.length > 0) {
            const checkedRadios: HTMLElement[] = this.slottedRadioButtons.filter(
                (radio: HTMLInputElement) => {
                    return radio.hasAttribute("checked");
                }
            );
            const numberOfCheckedRadios: number =
                checkedRadios !== null ? checkedRadios.length : 0;
            if (numberOfCheckedRadios > 0 && !foundMatchingVal) {
                const lastCheckedRadio: HTMLInputElement = checkedRadios[
                    numberOfCheckedRadios - 1
                ] as HTMLInputElement;
                lastCheckedRadio.checked = true;
                this.focusedRadio = lastCheckedRadio;
                lastCheckedRadio.setAttribute("tabindex", "0");
            } else {
                this.slottedRadioButtons[0].setAttribute("tabindex", "0");
                this.focusedRadio = this.slottedRadioButtons[0] as HTMLInputElement;
            }
        }
    }