Skip to content

mat-select is scrolling up when selecting - continuation #30931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
AntonBobylev opened this issue Apr 23, 2025 · 1 comment
Closed

mat-select is scrolling up when selecting - continuation #30931

AntonBobylev opened this issue Apr 23, 2025 · 1 comment

Comments

@AntonBobylev
Copy link

AntonBobylev commented Apr 23, 2025

The select expects options to be of a certain height and we currently don't support overriding it so you're doing it at your own risk.

Originally posted by @crisbeto in #18154

Look, I have the multiple mat-select, all rows have equal height, but scroll moves itself when select any option.

scroll.webm

template:

<div style="position: relative; width: 100%">
    <div class="loading-spinner-wrapper"
         [style.display]="isLoading() ? 'flex' : 'none'"
    >
        <app-loading-spinner [isLoading]="isLoading()" />
    </div>

    <mat-form-field appearance="outline"
                    [formGroup]="parentFormGroup"
                    [floatLabel]="floatLabel"
    >
        @if (label) {
            <mat-label>{{ label }}</mat-label>
        }
        <mat-select #fieldCtrl
                    [multiple]="isSupercombo"
                    [formControlName]="name"
                    [placeholder]="placeholder"
                    (valueChange)="onValueChanged.emit($event)"
        >
            @if (isSupercombo && valuesSeparator !== ',') {
                <mat-select-trigger>
                    <span>{{ getSelectedItemsDisplayValue() }}</span>
                </mat-select-trigger>
            }

            <!--@if (searchRequired && !EmsowAngular.isEmpty(data)) {
                <mat-option>
                    <ngx-mat-select-search [formControl]="searchCtrl"
                                           [noEntriesFoundLabel]="searchNothingFoundMessage"
                                           [placeholderLabel]="searchTooltip"
                                           [showToggleAllCheckbox]="showToggleAllCheckbox"
                                           (toggleAll)="onToggleAll($event)"
                                           [toggleAllCheckboxTooltipMessage]="toggleAllTooltip"
                    >
                        <mat-icon ngxMatSelectSearchClear
                                  class="material-symbols-outlined"
                        >
                            close
                        </mat-icon>
                    </ngx-mat-select-search>
                </mat-option>
            }-->

            @if (emptyOptionRequired) {
                <mat-option><span class="option">{{ emptyOptionLabel }}</span></mat-option>
            }

            <mat-option *ngFor="let record of filteredRecords()"
                        [value]="record"
            >
                <span class="option">{{ record.displayValue }}</span>
            </mat-option>
        </mat-select>

        @if (isAddNewEntity) {
            <app-icon-button matSuffix
                             icon="add"
                             (onClickEvent)="onAddNewEntity($event)"
            />
        }

        @if (clearBtnRequired && getValue() && !EmsowAngular.isEmpty(getValue())) {
            <app-icon-button matSuffix
                             icon="close"
                             (onClickEvent)="clear($event)"
            />
        }
    </mat-form-field>
</div>

styles:

mat-form-field {
    width: 100%;
}

.option {
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}
@AntonBobylev
Copy link
Author

Ok, after some research, I can commit that this method is working: #18651 (comment)

made it like so:

let me: this = this;
me.fieldCtrl.optionSelectionChanges.subscribe((): void => {
    me.scrollTopBeforeSelection = me.fieldCtrl.panel.nativeElement.scrollTop;
    let selectionInProgress: boolean = true;
    me.fieldCtrl.panel.nativeElement.onscroll = function (event: any): void {
        if (!selectionInProgress) {
            return;
        }
        event.target.scrollTop = me.scrollTopBeforeSelection;
        selectionInProgress = false;
    };
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant