Skip to content

Commit 015f617

Browse files
alinelariguetjhonyeduardo
authored andcommitted
feat(fields): implementa o método que habilita o foco
Implementa o método que habilita o foco nos seguintes componentes: - Checkbox Group - Combo - Datepicker - Datepicker Range - Decimal - Email - Input - Login - Lookup - Multiselect - Number - Password - Radio Group - Rich Text - Select - Switch - Textarea - Upload - Url Fixes DTHFUI-1720
1 parent 71ccfc8 commit 015f617

File tree

56 files changed

+1028
-112
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1028
-112
lines changed

projects/ui/src/lib/components/po-button/po-button.component.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,21 @@ describe('PoButtonComponent: ', () => {
146146
expect(component.buttonElement.nativeElement.focus).toHaveBeenCalled();
147147
});
148148

149+
it('focus: should`t call `focus` of button if `disabled`', () => {
150+
component.buttonElement = {
151+
nativeElement: {
152+
focus: () => {}
153+
}
154+
};
155+
component.disabled = true;
156+
157+
spyOn(component.buttonElement.nativeElement, 'focus');
158+
159+
component.focus();
160+
161+
expect(component.buttonElement.nativeElement.focus).not.toHaveBeenCalled();
162+
});
163+
149164
});
150165

151166
describe('Templates: ', () => {

projects/ui/src/lib/components/po-button/po-button.component.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,25 @@ export class PoButtonComponent extends PoButtonBaseComponent {
3131

3232
/**
3333
* Função que atribui foco ao componente.
34+
*
35+
* Para utilizá-la é necessário ter a instância do componente no DOM, podendo ser utilizado o ViewChild da seguinte forma:
36+
*
37+
* ```
38+
* import { PoButtonComponent } from '@portinari/portinari-ui';
39+
*
40+
* ...
41+
*
42+
* @ViewChild(PoButtonComponent, { static: true }) button: PoButtonComponent;
43+
*
44+
* focusButton() {
45+
* this.button.focus();
46+
* }
47+
* ```
3448
*/
3549
focus(): void {
36-
this.buttonElement.nativeElement.focus();
50+
if (!this.disabled) {
51+
this.buttonElement.nativeElement.focus();
52+
}
3753
}
3854

3955
onClick() {

projects/ui/src/lib/components/po-field/po-checkbox-group/po-checkbox-group.component.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,47 @@ describe('PoCheckboxGroupComponent:', () => {
105105

106106
describe('Methods:', () => {
107107

108+
it('focus: should call `focus` of checkbox', () => {
109+
component.options = [{ label: 'teste1', value: 'teste1' }, { label: 'teste2', value: 'teste2' }];
110+
111+
fixture.detectChanges();
112+
113+
spyOn(component.checkboxLabels.toArray()[0].nativeElement, 'focus');
114+
115+
component.focus();
116+
117+
expect(component.checkboxLabels.toArray()[0].nativeElement.focus).toHaveBeenCalled();
118+
});
119+
120+
it('focus: should`t call `focus` of checkbox if option is `disabled`', () => {
121+
component.options = [{ label: 'teste1', value: 'teste1', disabled: true }, { label: 'teste2', value: 'teste2' }];
122+
123+
fixture.detectChanges();
124+
125+
spyOn(component.checkboxLabels.toArray()[0].nativeElement, 'focus');
126+
spyOn(component.checkboxLabels.toArray()[1].nativeElement, 'focus');
127+
128+
component.focus();
129+
130+
expect(component.checkboxLabels.toArray()[0].nativeElement.focus).not.toHaveBeenCalled();
131+
expect(component.checkboxLabels.toArray()[1].nativeElement.focus).toHaveBeenCalled();
132+
});
133+
134+
it('focus: should`t call `focus` of checkbox if `disabled`', () => {
135+
component.options = [{ label: 'teste1', value: 'teste1', disabled: true }, { label: 'teste2', value: 'teste2' }];
136+
component.disabled = true;
137+
138+
fixture.detectChanges();
139+
140+
spyOn(component.checkboxLabels.toArray()[0].nativeElement, 'focus');
141+
spyOn(component.checkboxLabels.toArray()[1].nativeElement, 'focus');
142+
143+
component.focus();
144+
145+
expect(component.checkboxLabels.toArray()[0].nativeElement.focus).not.toHaveBeenCalled();
146+
expect(component.checkboxLabels.toArray()[1].nativeElement.focus).not.toHaveBeenCalled();
147+
});
148+
108149
describe('onKeyDown:', () => {
109150
let option;
110151
let fakeEvent: any;

projects/ui/src/lib/components/po-field/po-checkbox-group/po-checkbox-group.component.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AfterViewChecked, ChangeDetectorRef, Component, forwardRef } from '@angular/core';
1+
import { AfterViewChecked, ChangeDetectorRef, Component, ElementRef, forwardRef, QueryList, ViewChildren } from '@angular/core';
22
import { NG_VALIDATORS, NG_VALUE_ACCESSOR } from '@angular/forms';
33

44
import { PoCheckboxGroupBaseComponent } from './po-checkbox-group-base.component';
@@ -42,6 +42,8 @@ import { PoCheckboxGroupOption } from './po-checkbox-group-option.interface';
4242
})
4343
export class PoCheckboxGroupComponent extends PoCheckboxGroupBaseComponent implements AfterViewChecked {
4444

45+
@ViewChildren('checkboxLabel') checkboxLabels: QueryList<ElementRef>;
46+
4547
constructor(private changeDetector: ChangeDetectorRef) {
4648
super();
4749
}
@@ -50,6 +52,33 @@ export class PoCheckboxGroupComponent extends PoCheckboxGroupBaseComponent imple
5052
this.changeDetector.detectChanges();
5153
}
5254

55+
/**
56+
* Função que atribui foco ao componente.
57+
*
58+
* Para utilizá-la é necessário ter a instância do componente no DOM, podendo ser utilizado o ViewChild da seguinte forma:
59+
*
60+
* ```
61+
* import { PoCheckboxGroupComponent } from '@portinari/portinari-ui';
62+
*
63+
* ...
64+
*
65+
* @ViewChild(PoCheckboxGroupComponent, { static: true }) checkbox: PoCheckboxGroupComponent;
66+
*
67+
* focusCheckbox() {
68+
* this.checkbox.focus();
69+
* }
70+
* ```
71+
*/
72+
focus(): void {
73+
if (this.checkboxLabels && !this.disabled) {
74+
const checkboxLabel = this.checkboxLabels.find((_, index) => !this.options[index].disabled);
75+
76+
if (checkboxLabel) {
77+
checkboxLabel.nativeElement.focus();
78+
}
79+
}
80+
}
81+
5382
onKeyDown(event: KeyboardEvent, option: PoCheckboxGroupOption) {
5483
const spaceBar = 32;
5584

projects/ui/src/lib/components/po-field/po-combo/po-combo.component.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,35 @@ describe('PoComboComponent:', () => {
474474
stopPropagation: () => {}
475475
};
476476

477+
it('focus: should call `focus` of combo', () => {
478+
component.inputElement = {
479+
nativeElement: {
480+
focus: () => {}
481+
}
482+
};
483+
484+
spyOn(component.inputElement.nativeElement, 'focus');
485+
486+
component.focus();
487+
488+
expect(component.inputElement.nativeElement.focus).toHaveBeenCalled();
489+
});
490+
491+
it('focus: should`t call `focus` of combo if `disabled`', () => {
492+
component.inputElement = {
493+
nativeElement: {
494+
focus: () => {}
495+
}
496+
};
497+
component.disabled = true;
498+
499+
spyOn(component.inputElement.nativeElement, 'focus');
500+
501+
component.focus();
502+
503+
expect(component.inputElement.nativeElement.focus).not.toHaveBeenCalled();
504+
});
505+
477506
describe('onKeyUp:', () => {
478507

479508
function fakeKeypressEvent(code: number, target: any = 1) {

projects/ui/src/lib/components/po-field/po-combo/po-combo.component.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,29 @@ export class PoComboComponent extends PoComboBaseComponent implements DoCheck, O
158158
}
159159
}
160160

161+
/**
162+
* Função que atribui foco ao componente.
163+
*
164+
* Para utilizá-la é necessário ter a instância do componente no DOM, podendo ser utilizado o ViewChild da seguinte forma:
165+
*
166+
* ```
167+
* import { PoComboComponent } from '@portinari/portinari-ui';
168+
*
169+
* ...
170+
*
171+
* @ViewChild(PoComboComponent, { static: true }) combo: PoComboComponent;
172+
*
173+
* focusCombo() {
174+
* this.combo.focus();
175+
* }
176+
* ```
177+
*/
178+
focus(): void {
179+
if (!this.disabled) {
180+
this.inputElement.nativeElement.focus();
181+
}
182+
}
183+
161184
onKeyDown(event?: any) {
162185
const key = event.keyCode;
163186
const inputValue = event.target.value;

projects/ui/src/lib/components/po-field/po-combo/samples/sample-po-combo-hotels/sample-po-combo-hotels.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<div class="po-row">
88
<po-datepicker
9+
#datepicker
910
class="po-md-4"
1011
name="checkin"
1112
[(ngModel)]="checkin"

projects/ui/src/lib/components/po-field/po-combo/samples/sample-po-combo-hotels/sample-po-combo-hotels.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, ViewChild } from '@angular/core';
22
import { NgForm } from '@angular/forms';
33

4-
import { PoNotificationService, PoSelectOption } from '@portinari/portinari-ui';
4+
import { PoDatepickerComponent, PoNotificationService, PoSelectOption } from '@portinari/portinari-ui';
55

66
import { SamplePoComboHotelsService } from './sample-po-combo-hotels.service';
77

@@ -40,6 +40,7 @@ export class SamplePoComboHotelsComponent {
4040
];
4141

4242
@ViewChild('bookingForm', { static: true }) form: NgForm;
43+
@ViewChild('datepicker', { static: true }) datepickerComponent: PoDatepickerComponent;
4344

4445
constructor(
4546
public comboService: SamplePoComboHotelsService,
@@ -49,6 +50,8 @@ export class SamplePoComboHotelsComponent {
4950
this.poNotification.success('Hotel booked successfully');
5051

5152
this.formReset();
53+
54+
this.datepickerComponent.focus();
5255
}
5356

5457
private formReset() {

projects/ui/src/lib/components/po-field/po-datepicker-range/po-datepicker-range.component.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,35 @@ describe('PoDatepickerRangeComponent:', () => {
210210
expect(component.endDateInputValue).toBe('');
211211
});
212212

213+
it('focus: should call `focus` of datepicker-range', () => {
214+
component.startDateInput = {
215+
nativeElement: {
216+
focus: () => {}
217+
}
218+
};
219+
220+
spyOn(component.startDateInput.nativeElement, 'focus');
221+
222+
component.focus();
223+
224+
expect(component.startDateInput.nativeElement.focus).toHaveBeenCalled();
225+
});
226+
227+
it('focus: should`t call `focus` of datepicker-range if `disabled`', () => {
228+
component.startDateInput = {
229+
nativeElement: {
230+
focus: () => {}
231+
}
232+
};
233+
component.disabled = true;
234+
235+
spyOn(component.startDateInput.nativeElement, 'focus');
236+
237+
component.focus();
238+
239+
expect(component.startDateInput.nativeElement.focus).not.toHaveBeenCalled();
240+
});
241+
213242
it('onBlur: should call `removeFocusFromDatePickerRangeField`', () => {
214243
spyOn(component, <any>'removeFocusFromDatePickerRangeField');
215244

projects/ui/src/lib/components/po-field/po-datepicker-range/po-datepicker-range.component.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,29 @@ export class PoDatepickerRangeComponent extends PoDatepickerRangeBaseComponent i
132132
this.updateModel(this.dateRange);
133133
}
134134

135+
/**
136+
* Função que atribui foco ao componente.
137+
*
138+
* Para utilizá-la é necessário ter a instância do componente no DOM, podendo ser utilizado o ViewChild da seguinte forma:
139+
*
140+
* ```
141+
* import { PoDatepickerRangeComponent } from '@portinari/portinari-ui';
142+
*
143+
* ...
144+
*
145+
* @ViewChild(PoDatepickerRangeComponent, { static: true }) datepickerRange: PoDatepickerRangeComponent;
146+
*
147+
* focusDatepickerRange() {
148+
* this.datepickerRange.focus();
149+
* }
150+
* ```
151+
*/
152+
focus(): void {
153+
if (!this.disabled) {
154+
this.startDateInput.nativeElement.focus();
155+
}
156+
}
157+
135158
onBlur() {
136159
this.removeFocusFromDatePickerRangeField();
137160
}

0 commit comments

Comments
 (0)