Skip to content

Commit 3fc40c5

Browse files
committed
feat: add disabled prop to datepicker ✨ (#1363)
* feat: add disabled prop to datepicker ✨ * fix: broken test 🔨 (cherry picked from commit 6d90a21)
1 parent b0754ab commit 3fc40c5

File tree

8 files changed

+70
-0
lines changed

8 files changed

+70
-0
lines changed

projects/ion-test-app/src/app/app.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,13 @@ export class AppComponent implements OnInit {
11881188
disabled: true,
11891189
multiple: true,
11901190
},
1191+
{
1192+
key: 'data_abertura',
1193+
className: 'col-6',
1194+
type: 'datepicker',
1195+
label: 'Data de Abertura',
1196+
disabled: true,
1197+
},
11911198
{
11921199
key: 'bio',
11931200
className: 'col-12',

projects/ion/src/lib/core/bn-form/bn-form.component.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ import { BnMaskDirective } from '../../mask/mask.directive';
9595
[rangePicker]="field.rangePicker ?? false"
9696
[direction]="field.direction"
9797
[disabledDate]="field.disabledDate"
98+
[disabled]="isDisabled(field)"
9899
[predefinedRanges]="field.predefinedRanges ?? []"
99100
[value]="formGroup().get(field.key)?.value"
100101
(event)="onValueChange(field.key, $event)"

projects/ion/src/lib/picker/date-picker/date-picker-input/date-picker-input.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
data-testid="container-input"
55
>
66
<ion-input
7+
data-testid="date-picker-input-element"
78
[value]="date() || ''"
89
[placeholder]="placeholder()"
910
[iconInput]="date() ? '' : 'calendar'"
1011
iconDirection="right"
1112
[inputButton]="!!date()"
1213
[inputButtonConfig]="clearButtonConfig"
1314
[readonly]="true"
15+
[disabled]="disabled()"
1416
(clickButton)="clearDateValue()"
1517
></ion-input>
1618
</div>

projects/ion/src/lib/picker/date-picker/date-picker-input/date-picker-input.component.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,30 @@ describe('IonDatePickerInputComponent', () => {
3333

3434
expect(clearSpy).toHaveBeenCalled();
3535
});
36+
37+
it('should be disabled when disabled input is true', () => {
38+
const fixture = TestBed.createComponent(IonDatePickerInputComponent);
39+
fixture.componentRef.setInput('disabled', true);
40+
fixture.detectChanges();
41+
42+
const inputElement = screen.getByTestId('input-div');
43+
expect(inputElement).toHaveClass('disabled');
44+
});
45+
46+
it('should not emit clearDate when disabled', () => {
47+
const fixture = TestBed.createComponent(IonDatePickerInputComponent);
48+
fixture.componentRef.setInput('date', '2023-01-28');
49+
fixture.componentRef.setInput('disabled', true);
50+
fixture.detectChanges();
51+
52+
const clearSpy = jest.fn();
53+
fixture.componentRef.instance.clearDate.subscribe(clearSpy);
54+
55+
const clearButton = screen.queryByTestId('input-button');
56+
if (clearButton) {
57+
fireEvent.click(within(clearButton).getByRole('button'));
58+
}
59+
60+
expect(clearSpy).not.toHaveBeenCalled();
61+
});
3662
});

projects/ion/src/lib/picker/date-picker/date-picker-input/date-picker-input.component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class IonDatePickerInputComponent {
2626
rangePicker = input<boolean>(false);
2727
placeholder = input<string>('Selecione a data');
2828
clearDate = output<void>();
29+
disabled = input<boolean>(false);
2930

3031
public clearButtonConfig: IonButtonProps = {
3132
iconType: 'close-solid',
@@ -34,6 +35,7 @@ export class IonDatePickerInputComponent {
3435
};
3536

3637
clearDateValue(): void {
38+
if(this.disabled()) return;
3739
this.clearDate.emit();
3840
}
3941
}

projects/ion/src/lib/picker/date-picker/date-picker.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#trigger="cdkOverlayOrigin"
44
[date]="inputDate()"
55
[rangePicker]="rangePicker()"
6+
[disabled]="disabled()"
67
(click)="setVisibleCalendar(!showDatepicker())"
78
(clearDate)="clearDate()"
89
></date-picker-input>

projects/ion/src/lib/picker/date-picker/date-picker.component.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,31 @@ describe('IonDatepickerComponent with CDK Overlay', () => {
6464
overlayContainerElement.querySelector('.container-calendar'),
6565
).toBeFalsy();
6666
});
67+
68+
it('should not open overlay when disabled and clicked', () => {
69+
fixture.componentRef.setInput('disabled', true);
70+
fixture.detectChanges();
71+
72+
const inputDebug = fixture.debugElement.query(By.css('date-picker-input'));
73+
inputDebug.triggerEventHandler('click', null);
74+
fixture.detectChanges();
75+
76+
const calendarContainer = overlayContainerElement.querySelector(
77+
'.container-calendar',
78+
);
79+
expect(calendarContainer).toBeFalsy();
80+
});
81+
82+
it('should not open overlay via setVisibleCalendar when disabled', () => {
83+
fixture.componentRef.setInput('disabled', true);
84+
fixture.detectChanges();
85+
86+
component.setVisibleCalendar(true);
87+
fixture.detectChanges();
88+
89+
const calendarContainer = overlayContainerElement.querySelector(
90+
'.container-calendar',
91+
);
92+
expect(calendarContainer).toBeFalsy();
93+
});
6794
});

projects/ion/src/lib/picker/date-picker/date-picker.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export class IonDatepickerComponent {
7373
>(undefined);
7474
goToMonth = signal<string | undefined>(undefined);
7575
goToYear = signal<string | undefined>(undefined);
76+
disabled = input<boolean>(false);
7677

7778
overlayPositions = computed<ConnectedPosition[]>(() => {
7879
const direction = String(
@@ -160,6 +161,9 @@ export class IonDatepickerComponent {
160161
}
161162

162163
setVisibleCalendar(visible: boolean): void {
164+
if (this.disabled()) {
165+
return;
166+
}
163167
this.showDatepicker.set(visible);
164168
}
165169

0 commit comments

Comments
 (0)