Skip to content

Commit

Permalink
fix(select): trata placeholder quando é Safari
Browse files Browse the repository at this point in the history
  • Loading branch information
felipepetuco committed Dec 15, 2023
1 parent 4607209 commit 5fc3593
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,23 @@
[required]="required"
(change)="onSelectChange($event.target.value)"
>
<option
*ngIf="!selectedValue?.toString() || !!placeholder"
[disabled]="!!placeholder"
[hidden]="!selectedValue?.toString() && !placeholder"
[selected]="!selectedValue?.toString()"
[value]="placeholder ?? ''"
>
{{ placeholder }}
</option>
<!-- necessário manter essa option para uso do select com ReactiveForms -->
<option [hidden]="true">
{{ displayValue }}
</option>
<ng-container *ngIf="!isSafari">
<option
*ngIf="!selectedValue?.toString() || !!placeholder"
[disabled]="!!placeholder"
[hidden]="!selectedValue?.toString() && !placeholder"
[selected]="!selectedValue?.toString()"
[value]="placeholder ?? ''"
>
{{ placeholder }}
</option>
<option [hidden]="true">{{ displayValue }}</option>
</ng-container>

<ng-container *ngIf="isSafari">
<option disabled="true" *ngIf="isSafari">{{ displayValue || placeholder }}</option>
</ng-container>

<ng-container *ngIf="optionWithoutGroup.length > 0">
<option *ngFor="let item of optionWithoutGroup" [disabled]="readonly" [value]="item?.[this.fieldValue]">
{{ item?.[this.fieldLabel] }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { AbstractControl, NG_VALIDATORS, NG_VALUE_ACCESSOR } from '@angular/form

import {
convertToBoolean,
isSafari,
removeDuplicatedOptions,
removeUndefinedAndNullOptions,
uuid,
Expand Down Expand Up @@ -128,6 +129,7 @@ export class PoSelectComponent extends PoFieldValidateModel<any> implements OnCh
optionsDefault = [];
listGroupOptions = [];
optionWithoutGroup = [];
isSafari: boolean = isSafari();

protected onModelTouched: any;

Expand Down Expand Up @@ -287,7 +289,14 @@ export class PoSelectComponent extends PoFieldValidateModel<any> implements OnCh
const optionFound: any = this.findOptionValue(value);

if (optionFound) {
this.updateValues(optionFound);
if (!isSafari()) {
this.updateValues(optionFound);
} else {
this.selectElement.nativeElement.querySelector('option').disabled = false;

this.updateValues(optionFound);
this.disableDisplayValue();
}
}
}
}
Expand Down Expand Up @@ -328,6 +337,18 @@ export class PoSelectComponent extends PoFieldValidateModel<any> implements OnCh
this.changeDetector.detectChanges();
}

disableDisplayValue() {
if (!this.selectElement.nativeElement.querySelector('option').disabled) {
setTimeout(() => {
this.selectElement.nativeElement.querySelector('option').disabled = true;
}, 30);
}
}

ngAfterContentChecked() {
if (isSafari) this.disableDisplayValue();

Check failure on line 349 in projects/ui/src/lib/components/po-field/po-select/po-select.component.ts

View workflow job for this annotation

GitHub Actions / lint

Expected { after 'if' condition
}

extraValidation(c: AbstractControl): { [key: string]: any } {
return null;
}
Expand Down
7 changes: 7 additions & 0 deletions projects/ui/src/lib/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,13 @@ export function isFirefox() {
return userAgent.toLowerCase().indexOf('firefox') > -1;
}

// Verifica se o navegador em que está sendo usado é Safari
export function isSafari() {
const userAgent = window.navigator.userAgent;

return /^(?!.*chrome).*safari.*$/.test(userAgent.toLocaleLowerCase());
}

// Verifica qual o dispositivo que está sendo usado
export function isMobile() {
const userAgent = window.navigator.userAgent;
Expand Down

0 comments on commit 5fc3593

Please sign in to comment.