Skip to content
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

Remove unnecessary ControlValueAccessor code and replace it with override of writeValue #1161

Closed
Closed
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
2 changes: 1 addition & 1 deletion projects/ng-datetime-picker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sq-ui/ng-datetime-picker",
"version": "2.0.1",
"version": "2.0.2",
"license": "MIT",
"private": false,
"description": "Simple Quality UI Datetime-Picker for Angular",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Component, forwardRef, OnInit, ViewEncapsulation,
Input, Output, EventEmitter, AfterViewInit,
Input, Output, EventEmitter,
OnChanges
} from '@angular/core';
import { InputCoreComponent } from '@sq-ui/ng-sq-common';
Expand Down Expand Up @@ -28,7 +28,7 @@ const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR = {
encapsulation: ViewEncapsulation.None,
providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR]
})
export class DatetimePickerComponent extends InputCoreComponent implements OnInit, AfterViewInit, OnChanges {
export class DatetimePickerComponent extends InputCoreComponent implements OnInit, OnChanges {
@Input() locale = 'en';
@Input() maxDate: moment.Moment | Date;
@Input() minDate: moment.Moment | Date;
Expand Down Expand Up @@ -62,16 +62,8 @@ export class DatetimePickerComponent extends InputCoreComponent implements OnIni
moment.locale(this.locale);
this.calendarManager.setLocale(this.locale);
const now = moment().hours(0).minutes(0).locale(this.locale);
this.selectedDates = List([now.clone()]);
this.weekdays = this.calendarManager.getWeekdays();
this.calendar = this.getMonthCalendar(now.clone());
this.initializeAuthorValuesIfAny();
}

ngAfterViewInit() {
setTimeout(() => {
this.setValueResult();
});
}

ngOnChanges(changesObj) {
Expand All @@ -80,6 +72,28 @@ export class DatetimePickerComponent extends InputCoreComponent implements OnIni
}
}

override writeValue(newValue): void {
super.writeValue(newValue);

if (newValue) {
this.deselectAll();

if (Array.isArray(newValue)) {
newValue.forEach((date) => {
const convertedDate = this.calendarManager.findADateFromCalendar(moment(date), this.calendar);
this.markDateAsSelected(convertedDate);
});
} else {
const calendarDay = this.calendarManager.findADateFromCalendar(moment(newValue), this.calendar);
this.markDateAsSelected(calendarDay);
}
} else {
const now = moment().hours(0).minutes(0).locale(this.locale);
const convertedDate = this.calendarManager.findADateFromCalendar(now, this.calendar);
this.markDateAsSelected(convertedDate);
}
}

onDateClick(date: CalendarDay) {
switch (date.relativityToCurrentMonth) {
case CalendarPeriodRelativityEnum.After:
Expand Down Expand Up @@ -196,28 +210,6 @@ export class DatetimePickerComponent extends InputCoreComponent implements OnIni
this.dateSelectionChange.emit(this.value);
}

private initializeAuthorValuesIfAny() {
const subscription = this._modelToViewChange.subscribe((newValue) => {
if (this.selectedDates.size === 1 && this.selectedDates.get(0).isSame(moment(), 'day')) {
if (newValue) {
this.deselectAll();

if (Array.isArray(newValue)) {
newValue.forEach((date) => {
const convertedDate = this.calendarManager.findADateFromCalendar(moment(date), this.calendar);
this.markDateAsSelected(convertedDate);
});
} else {
const calendarDay = this.calendarManager.findADateFromCalendar(moment(newValue), this.calendar);
this.markDateAsSelected(calendarDay);
}
}
}

subscription.unsubscribe();
});
}

private markDateAsSelected(date: CalendarDay) {
const selectedMomentObj = moment(date.momentObj);
const selectedIndex = this.calendarManager.getSelectedItemIndex(selectedMomentObj, this.selectedDates.toArray());
Expand Down
2 changes: 1 addition & 1 deletion projects/ng-form-elements/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sq-ui/ng-form-elements",
"version": "2.0.1",
"version": "2.0.2",
"dependencies": {
"@sq-ui/ng-sq-common": "^2.0.1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export class TagsInputComponent extends InputCoreComponent implements OnInit, Af

private isModelEmpty: boolean = false;
private enteredItemsSubscription: Subscription;
private valueChangedSubscription: Subscription;
private inputEventSubscription: Subscription;
private innerEnteredItemsListCopy: List<string>;

Expand All @@ -51,13 +50,13 @@ export class TagsInputComponent extends InputCoreComponent implements OnInit, Af
const itemsCopy = this.innerEnteredItemsListCopy;
this.value = itemsCopy.toArray();
});
}

this.valueChangedSubscription = this._modelToViewChange.subscribe((predefinedEnteredItems) => {
if (this.enteredItems.size === 0 && predefinedEnteredItems && predefinedEnteredItems.length > 0) {
this.enteredItems = List<string>(predefinedEnteredItems);
this.valueChangedSubscription.unsubscribe();
}
});
override writeValue(predefinedEnteredItems): void {
super.writeValue(predefinedEnteredItems);
if (this.enteredItems.size === 0 && predefinedEnteredItems && predefinedEnteredItems.length > 0) {
this.enteredItems = List<string>(predefinedEnteredItems);
}
}

ngAfterViewInit() {
Expand All @@ -79,10 +78,6 @@ export class TagsInputComponent extends InputCoreComponent implements OnInit, Af
if (this.inputEventSubscription) {
this.inputEventSubscription.unsubscribe();
}

if (!this.valueChangedSubscription.closed) {
this.valueChangedSubscription.unsubscribe();
}
}

onUserInput($event) {
Expand Down
24 changes: 8 additions & 16 deletions projects/ng-form-elements/src/lib/typeahead/typeahead.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export class TypeaheadComponent extends InputCoreComponent

private onInputValueChangeSubscription: Subscription;
private onQueryInputControlSubscription: Subscription;
private valueChangedSubscription: Subscription;

selectedItems: List<LabelValuePair> = List<LabelValuePair>();
options: List<LabelValuePair> = List<LabelValuePair>();
Expand Down Expand Up @@ -80,18 +79,15 @@ export class TypeaheadComponent extends InputCoreComponent
}
},
);
}

this.valueChangedSubscription = this._modelToViewChange.subscribe(
(predefinedEnteredItems) => {
if (this.selectedItems.size === 0 && predefinedEnteredItems && predefinedEnteredItems.length > 0) {
this.transformToLabelValuePairList(predefinedEnteredItems).forEach((item) => {
this.selectItem(item, false, true);
});
}

this.valueChangedSubscription.unsubscribe();
},
);
override writeValue(predefinedEnteredItems): void {
super.writeValue(predefinedEnteredItems);
if (this.selectedItems.size === 0 && predefinedEnteredItems && predefinedEnteredItems.length > 0) {
this.transformToLabelValuePairList(predefinedEnteredItems).forEach((item) => {
this.selectItem(item, false, true);
});
}
}

ngOnChanges(changesObj: SimpleChanges) {
Expand All @@ -112,10 +108,6 @@ export class TypeaheadComponent extends InputCoreComponent
this.listenForOutsideClick = false;
this.onQueryInputControlSubscription.unsubscribe();
this.onInputValueChangeSubscription.unsubscribe();

if (!this.valueChangedSubscription.closed) {
this.valueChangedSubscription.unsubscribe();
}
}

selectSearchResult(result: LabelValuePair) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { ControlValueAccessor } from '@angular/forms';
* Its only purpose is to implement the ControlValueAccessor interface.
**/
export class ControlValueAccessorEnabler implements ControlValueAccessor {
protected _modelToViewChange: EventEmitter<any> = new EventEmitter();
protected _value: any;
protected _onChange: any = () => {};
protected _onTouched: any = () => {};
Expand All @@ -32,7 +31,6 @@ export class ControlValueAccessorEnabler implements ControlValueAccessor {
writeValue(newValue: any): void {
if (newValue !== this._value) {
this._value = newValue;
this._modelToViewChange.emit(newValue);
}
}

Expand Down
Loading