Skip to content

Commit

Permalink
fix(module: picker): remove lodash isEqual reference (#592)
Browse files Browse the repository at this point in the history
  • Loading branch information
BronzeCui authored and fisherspy committed Sep 24, 2019
1 parent d9706cc commit ee6f4b6
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 deletions.
81 changes: 79 additions & 2 deletions components/picker/picker.component.spec.ts
@@ -1,8 +1,8 @@
import { Component } from '@angular/core';
import { Component, ViewChild } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { async, ComponentFixture, TestBed, fakeAsync } from '@angular/core/testing';
import { ListModule, PickerModule, PickerComponent } from '../..';
import { ListModule, PickerModule, PickerComponent, PickerDirective } from '../..';
import { PickerOptions } from './picker-options.provider';
import { PickerService } from './picker.service';
import { dispatchTouchEvent } from '../core/testing';
Expand Down Expand Up @@ -163,6 +163,79 @@ describe('PickerComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

it('should create', () => {
let data1 = [
{
value: '',
label: 'd',
children: [
{
value: '',
label: '',
children: [
]
}
]
}
];

let data2 = [
{
value: 'dd',
label: '',
children: [
{
value: '',
label: '',
children: [
]
}
]
},
];
let data3 = null;
let data4 = [];

let data5 = [
{
value: 'dd',
label: ''
},
{
value: 'dd',
label: ''
}
];
let data6 = [
{
value: 'dd',
label: 'd'
},
{
value: 'dd',
label: ''
}
];
let data7 = [
{
value: 'dd',
label: ''
},
null
];
expect(component.picker.isPickerDataEqual(data1, data2)).toBe(false);
expect(component.picker.isPickerDataEqual(data1, data3)).toBe(false);
expect(component.picker.isPickerDataEqual(data3, data2)).toBe(false);
expect(component.picker.isPickerDataEqual(data1, data4)).toBe(false);
expect(component.picker.isPickerDataEqual(data1, data5)).toBe(false);
expect(component.picker.isPickerDataEqual(data3, data4)).toBe(false);
expect(component.picker.isPickerDataEqual(data5, data6)).toBe(false);
expect(component.picker.isPickerDataEqual(data5, data7)).toBe(false);
expect(component.picker.isPickerDataEqual(data3, data3)).toBe(true);
expect(component.picker.isPickerDataEqual(data4, data4)).toBe(true);
expect(component.picker.isPickerDataEqual(data1, data1)).toBe(true);
});
});

@Component({
Expand Down Expand Up @@ -230,6 +303,10 @@ export class TestPickerBasicComponent {
mask = true;
modelChange = jasmine.createSpy('ngModel change callback');

@ViewChild(PickerDirective, { static: false })
picker: PickerDirective;


constructor(private _picker: PickerService) {}

getResult(result) {
Expand Down
29 changes: 27 additions & 2 deletions components/picker/picker.directive.ts
Expand Up @@ -20,7 +20,6 @@ import {
import { PickerComponent } from './picker.component';
import { PickerOptions } from './picker-options.provider';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import * as _ from 'lodash';

@Directive({
selector: '[Picker], [nzm-picker]',
Expand Down Expand Up @@ -96,7 +95,7 @@ export class PickerDirective implements OnDestroy, OnInit, OnChanges, ControlVal
this.picker.instance.options.cols = value.cols.currentValue;
}
if (value.data && this.picker) {
if (!_.isEqual(this.picker.instance.options.data, value.data.currentValue)) {
if (!this.isPickerDataEqual(this.picker.instance.options.data, value.data.currentValue)) {
this.picker.instance.options.data = value.data.currentValue;
this.showPicker();
}
Expand Down Expand Up @@ -210,4 +209,30 @@ export class PickerDirective implements OnDestroy, OnInit, OnChanges, ControlVal
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
}

isPickerDataEqual(data1, data2) {
if (!data1 && !data2) {
return true;
}
if (!Array.isArray(data1) || !Array.isArray(data2) || data1.length !== data2.length) {
return false;
}
for (let i = 0; i < data1.length; i++) {
const item1 = data1[i];
const item2 = data2[i];
if ((item1 && !item2) || (!item1 && item2)) {
return false;
}
if (item1.value !== item2.value) {
return false;
}
if (item1.label !== item2.label) {
return false;
}
if (item1.children && item2.children) {
return this.isPickerDataEqual(item1.children, item2.children);
}
}
return true;
}
}

0 comments on commit ee6f4b6

Please sign in to comment.