MCPcopy Create free account
hub / github.com/angular/components / _updateTabIndex

Method _updateTabIndex

src/material/radio/radio.ts:735–784  ·  view source on GitHub ↗

Gets the tabindex for the underlying input element.

()

Source from the content-addressed store, hash-verified

733
734 /** Gets the tabindex for the underlying input element. */
735 private _updateTabIndex() {
736 const group = this.radioGroup;
737 let value: number;
738
739 // Implement a roving tabindex if the button is inside a group. For most cases this isn't
740 // necessary, because the browser handles the tab order for inputs inside a group automatically,
741 // but we need an explicitly higher tabindex for the selected button in order for things like
742 // the focus trap to pick it up correctly.
743 if (!group || !group.selected || this.disabled) {
744 value = this.tabIndex;
745 } else {
746 value = group.selected === this ? this.tabIndex : -1;
747 }
748
749 if (value !== this._previousTabIndex) {
750 // We have to set the tabindex directly on the DOM node, because it depends on
751 // the selected state which is prone to "changed after checked errors".
752 const input: HTMLInputElement | undefined = this._inputElement?.nativeElement;
753
754 if (input) {
755 input.setAttribute('tabindex', value + '');
756 this._previousTabIndex = value;
757 // Wait for any pending tabindex changes to be applied
758 afterNextRender(
759 () => {
760 queueMicrotask(() => {
761 // The radio group uses a "selection follows focus" pattern for tab management, so if this
762 // radio button is currently focused and another radio button in the group becomes
763 // selected, we should move focus to the newly selected radio button to maintain
764 // consistency between the focused and selected states.
765 if (
766 group &&
767 group.selected &&
768 group.selected !== this &&
769 document.activeElement === input
770 ) {
771 group.selected?._inputElement.nativeElement.focus();
772 // If this radio button still has focus, the selected one must be disabled. In this
773 // case the radio group as a whole should lose focus.
774 if (document.activeElement === input) {
775 this._inputElement.nativeElement.blur();
776 }
777 }
778 });
779 },
780 {injector: this._injector},
781 );
782 }
783 }
784 }
785}

Callers 2

ngDoCheckMethod · 0.95
ngAfterViewInitMethod · 0.95

Calls 2

focusMethod · 0.65
blurMethod · 0.65

Tested by

no test coverage detected