From b786be6386b0fedc337d2eb0690f7a338aafa2e0 Mon Sep 17 00:00:00 2001 From: Austin Walker Date: Thu, 11 Apr 2024 14:03:19 -0600 Subject: [PATCH 1/2] fixes #14010 - added autofocus to all inputs and buttons --- src/app/components/autofocus/autofocus.ts | 9 +- src/app/components/button/button.ts | 10 +- src/app/components/calendar/calendar.ts | 10 +- .../components/cascadeselect/cascadeselect.ts | 12 +- src/app/components/checkbox/checkbox.ts | 10 +- src/app/components/chips/chips.ts | 10 +- src/app/components/colorpicker/colorpicker.ts | 10 +- src/app/components/dialog/dialog.ts | 30 +--- src/app/components/dom/domhandler.ts | 36 ++-- src/app/components/dropdown/dropdown.ts | 3 + src/app/components/inputmask/inputmask.ts | 13 +- src/app/components/inputnumber/inputnumber.ts | 10 +- src/app/components/inputotp/inputotp.ts | 19 +- src/app/components/inputswitch/inputswitch.ts | 10 +- src/app/components/multiselect/multiselect.ts | 10 +- src/app/components/password/password.ts | 11 +- src/app/components/radiobutton/radiobutton.ts | 11 +- src/app/components/rating/rating.ts | 12 +- .../components/selectbutton/selectbutton.ts | 10 +- src/app/components/slider/slider.ts | 12 +- src/app/components/splitbutton/splitbutton.ts | 13 +- .../components/togglebutton/togglebutton.ts | 10 +- src/app/components/treeselect/treeselect.ts | 10 +- .../tristatecheckbox/tristatecheckbox.ts | 10 +- src/app/showcase/doc/apidoc/index.json | 165 +++++++++++++++++- src/app/showcase/doc/calendar/propsdoc.ts | 6 + 26 files changed, 407 insertions(+), 65 deletions(-) diff --git a/src/app/components/autofocus/autofocus.ts b/src/app/components/autofocus/autofocus.ts index 250b592f6e1..11253f8051b 100644 --- a/src/app/components/autofocus/autofocus.ts +++ b/src/app/components/autofocus/autofocus.ts @@ -17,11 +17,18 @@ export class AutoFocus { * When present, it specifies that the component should automatically get focus on load. * @group Props */ - @Input({ transform: booleanAttribute }) autofocus: boolean | undefined; + @Input({ transform: booleanAttribute }) autofocus: boolean = false; focused: boolean = false; ngAfterContentChecked() { + // This sets the `attr.autofocus` which is different than the Input `autofocus` attribute. + if (this.autofocus === false) { + this.host.nativeElement.removeAttribute('autofocus'); + } else { + this.host.nativeElement.setAttribute('autofocus', true); + } + if (!this.focused) { if (this.autofocus) { setTimeout(() => { diff --git a/src/app/components/button/button.ts b/src/app/components/button/button.ts index 4e95dcce8d3..cd02156c196 100755 --- a/src/app/components/button/button.ts +++ b/src/app/components/button/button.ts @@ -24,6 +24,7 @@ import { DomHandler } from 'primeng/dom'; import { SpinnerIcon } from 'primeng/icons/spinner'; import { RippleModule } from 'primeng/ripple'; import { ObjectUtils } from 'primeng/utils'; +import { AutoFocusModule } from 'primeng/autofocus'; type ButtonIconPosition = 'left' | 'right' | 'top' | 'bottom'; @@ -342,6 +343,8 @@ export class ButtonDirective implements AfterViewInit, OnDestroy { [attr.data-pc-name]="'button'" [attr.data-pc-section]="'root'" [attr.tabindex]="tabindex" + pAutoFocus + [autofocus]="autofocus" > @@ -477,6 +480,11 @@ export class Button implements AfterContentInit { * @group Props */ @Input() ariaLabel: string | undefined; + /** + * When present, it specifies that the component should automatically get focus on load. + * @group Props + */ + @Input({ transform: booleanAttribute }) autofocus: boolean | undefined; /** * Callback to execute when button is clicked. * This event is intended to be used with the component. Using a regular @@ -52,6 +56,8 @@ type SplitButtonIconPosition = 'left' | 'right'; [disabled]="buttonDisabled" [attr.tabindex]="tabindex" [attr.aria-label]="buttonProps?.['aria-label']" + pAutoFocus + [autofocus]="autofocus" >