Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: datepicker disabled state change is unstable #466

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion angular/projects/catalyst-demo/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,26 @@ <h2>Form Components</h2>
[connector]="countryConnector"
[clearable]="true"
></cat-select>
<!-- Datepicker Section 1 -->
<cat-checkbox #checkBox
label="Should disable next datepicker">
</cat-checkbox>
<cat-datepicker
[disabled]="checkBox.checked"
label="Datepicker"
[nativeAttributes]="{ 'data-test': 'test-attribute' }"
hint="Depends on [disabled] attribute"
></cat-datepicker>
<!-- Datepicker Section 2 -->
<cat-checkbox (catChange)="onCheckboxChange($event)"
formControlName="datepickerDisabled"
label="Should disable next datepicker">
</cat-checkbox>
<cat-datepicker
label="Datepicker"
formControlName="date"
[nativeAttributes]="{ 'data-test': 'test-attribute' }"
hint="this is a hint"
hint="Depends on formControl state"
></cat-datepicker>
<formly-form [form]="form" [fields]="fields"></formly-form>
</form>
17 changes: 16 additions & 1 deletion angular/projects/catalyst-demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class AppComponent implements OnInit {
test: new FormControl('test', [Validators.pattern('a+'), Validators.required, Validators.minLength(3)]),
relatedInput: new FormControl(null, [this.equalTo('test')]),
option: new FormControl(null, [Validators.required]),
date: new FormControl(null, [Validators.required])
date: new FormControl(null, [Validators.required]),
datepickerDisabled: new FormControl(true)
});

countryConnector = countryConnector;
Expand Down Expand Up @@ -148,6 +149,20 @@ export class AppComponent implements OnInit {
this.form.controls.test.valueChanges.subscribe(() => {
this.form.controls.relatedInput.updateValueAndValidity();
});
if (this.form.controls.datepickerDisabled.value) {
this.form.controls.date.disable();
} else {
this.form.controls.date.enable();
}
}

onCheckboxChange(event: Event) {
const checkbox = event.target as HTMLInputElement;
if (checkbox.checked) {
this.form.controls.date.disable();
} else {
this.form.controls.date.enable();
}
}

equalTo(controlName: string) {
Expand Down
7 changes: 5 additions & 2 deletions core/src/components/cat-datepicker/cat-datepicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ export class CatDatepickerFlat {
// has been updated.
this.pickr?.destroy();
this.pickr = undefined;
setTimeout(() => (this.pickr = this.initDatepicker(this.input)));
setTimeout(() => {
this.input ? (this.input.disabled = this.disabled) : null;
this.pickr = this.initDatepicker(this.input);
});
}

componentDidLoad() {
Expand Down Expand Up @@ -297,7 +300,7 @@ export class CatDatepickerFlat {
}

private initDatepicker(input?: HTMLInputElement): flatpickr.Instance | undefined {
if (this.disabled || this.readonly || !input) {
if (!input) {
return;
}

Expand Down
Loading