Skip to content

Commit

Permalink
fix(module:grid): fix grid responsive (#2915)
Browse files Browse the repository at this point in the history
close #2908
  • Loading branch information
vthinkxie authored Feb 18, 2019
1 parent a7b4e09 commit ab05619
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 276 deletions.
13 changes: 9 additions & 4 deletions components/form/nz-form-control.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
AfterContentInit,
AfterContentInit, AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Expand All @@ -18,7 +18,7 @@ import { startWith } from 'rxjs/operators';
import { NzUpdateHostClassService } from '../core/services/update-host-class.service';
import { NgClassType } from '../core/types/ng-class';
import { toBoolean } from '../core/util/convert';
import { NzColComponent } from '../grid/nz-col.component';
import { NzColDirective } from '../grid/nz-col.directive';
import { NzRowDirective } from '../grid/nz-row.directive';
import { NzFormItemComponent } from './nz-form-item.component';

Expand All @@ -37,7 +37,7 @@ import { NzFormItemComponent } from './nz-form-item.component';
`
]
})
export class NzFormControlComponent extends NzColComponent implements OnDestroy, OnInit, AfterContentInit {
export class NzFormControlComponent extends NzColDirective implements OnDestroy, OnInit, AfterContentInit, AfterViewInit, OnDestroy {
private _hasFeedback = false;
validateChanges: Subscription;
validateString: string;
Expand Down Expand Up @@ -120,7 +120,7 @@ export class NzFormControlComponent extends NzColComponent implements OnDestroy,
}

constructor(nzUpdateHostClassService: NzUpdateHostClassService, elementRef: ElementRef, @Optional() @Host() nzFormItemComponent: NzFormItemComponent, @Optional() @Host() nzRowDirective: NzRowDirective, private cdr: ChangeDetectorRef, renderer: Renderer2) {
super(nzUpdateHostClassService, elementRef, nzFormItemComponent, nzRowDirective);
super(nzUpdateHostClassService, elementRef, nzFormItemComponent || nzRowDirective, renderer);
renderer.addClass(elementRef.nativeElement, 'ant-form-item-control-wrapper');
}

Expand All @@ -131,11 +131,16 @@ export class NzFormControlComponent extends NzColComponent implements OnDestroy,

ngOnDestroy(): void {
this.removeSubscribe();
super.ngOnDestroy();
}

ngAfterContentInit(): void {
if (this.defaultValidateControl && (!this.validateControl) && (!this.validateString)) {
this.nzValidateStatus = this.defaultValidateControl;
}
}

ngAfterViewInit(): void {
super.ngAfterViewInit();
}
}
4 changes: 2 additions & 2 deletions components/form/nz-form-item.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { takeUntil } from 'rxjs/operators';
import { NzUpdateHostClassService } from '../core/services/update-host-class.service';
import { toBoolean } from '../core/util/convert';
import { NzRowComponent } from '../grid/nz-row.component';
import { NzRowDirective } from '../grid/nz-row.directive';
import { NzFormExplainComponent } from './nz-form-explain.component';

/** should add nz-row directive to host, track https://github.com/angular/angular/issues/8785 **/
Expand All @@ -39,7 +39,7 @@ import { NzFormExplainComponent } from './nz-form-explain.component';
`
]
})
export class NzFormItemComponent extends NzRowComponent implements AfterContentInit, OnDestroy {
export class NzFormItemComponent extends NzRowDirective implements AfterContentInit, OnDestroy {
private _flex = false;
@ContentChildren(NzFormExplainComponent, { descendants: true }) listOfNzFormExplainComponent: QueryList<NzFormExplainComponent>;

Expand Down
17 changes: 13 additions & 4 deletions components/form/nz-form-label.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
ElementRef,
Host,
Input,
Input, OnDestroy,
Optional, Renderer2,
ViewEncapsulation
} from '@angular/core';
import { NzUpdateHostClassService } from '../core/services/update-host-class.service';
import { InputBoolean } from '../core/util/convert';
import { NzColComponent } from '../grid/nz-col.component';
import { NzColDirective } from '../grid/nz-col.directive';
import { NzRowDirective } from '../grid/nz-row.directive';
import { NzFormItemComponent } from './nz-form-item.component';

Expand All @@ -21,12 +22,20 @@ import { NzFormItemComponent } from './nz-form-item.component';
changeDetection : ChangeDetectionStrategy.OnPush,
templateUrl : './nz-form-label.component.html'
})
export class NzFormLabelComponent extends NzColComponent {
export class NzFormLabelComponent extends NzColDirective implements OnDestroy, AfterViewInit {
@Input() nzFor: string;
@Input() @InputBoolean() nzRequired = false;

constructor(nzUpdateHostClassService: NzUpdateHostClassService, elementRef: ElementRef, @Optional() @Host() nzFormItemComponent: NzFormItemComponent, @Optional() @Host() nzRowDirective: NzRowDirective, renderer: Renderer2) {
super(nzUpdateHostClassService, elementRef, nzFormItemComponent, nzRowDirective);
super(nzUpdateHostClassService, elementRef, nzFormItemComponent || nzRowDirective, renderer);
renderer.addClass(elementRef.nativeElement, 'ant-form-item-label');
}

ngOnDestroy(): void {
super.ngOnDestroy();
}

ngAfterViewInit(): void {
super.ngAfterViewInit();
}
}
1 change: 0 additions & 1 deletion components/grid/nz-col.component.html

This file was deleted.

111 changes: 0 additions & 111 deletions components/grid/nz-col.component.ts

This file was deleted.

106 changes: 99 additions & 7 deletions components/grid/nz-col.directive.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,114 @@
import {
AfterViewInit,
Directive,
ElementRef,
Host,
Optional
Input,
OnChanges, OnDestroy,
OnInit,
Optional, Renderer2,
SimpleChange
} from '@angular/core';
import { Subject } from 'rxjs';
import { startWith, takeUntil } from 'rxjs/operators';

import { NzUpdateHostClassService } from '../core/services/update-host-class.service';
import { isNotNil } from '../core/util/check';

import { NzColComponent } from './nz-col.component';
import { NzRowComponent } from './nz-row.component';
import { NzRowDirective } from './nz-row.directive';

export interface EmbeddedProperty {
span: number;
pull: number;
push: number;
offset: number;
order: number;
}

@Directive({
selector : '[nz-col]',
selector : '[nz-col],nz-col',
providers: [ NzUpdateHostClassService ]
})
export class NzColDirective extends NzColComponent {
constructor(nzUpdateHostClassService: NzUpdateHostClassService, elementRef: ElementRef, @Optional() @Host() nzRowComponent: NzRowComponent, @Optional() @Host() nzRowDirective: NzRowDirective) {
super(nzUpdateHostClassService, elementRef, nzRowComponent, nzRowDirective);
export class NzColDirective implements OnInit, OnChanges, AfterViewInit, OnDestroy {
private el: HTMLElement = this.elementRef.nativeElement;
private prefixCls = 'ant-col';
protected destroy$ = new Subject();

@Input() nzSpan: number;
@Input() nzOrder: number;
@Input() nzOffset: number;
@Input() nzPush: number;
@Input() nzPull: number;
@Input() nzXs: number | EmbeddedProperty;
@Input() nzSm: number | EmbeddedProperty;
@Input() nzMd: number | EmbeddedProperty;
@Input() nzLg: number | EmbeddedProperty;
@Input() nzXl: number | EmbeddedProperty;
@Input() nzXXl: number | EmbeddedProperty;

/** temp solution since no method add classMap to host https://github.com/angular/angular/issues/7289*/
setClassMap(): void {
const classMap = {
[ `${this.prefixCls}-${this.nzSpan}` ] : isNotNil(this.nzSpan),
[ `${this.prefixCls}-order-${this.nzOrder}` ] : isNotNil(this.nzOrder),
[ `${this.prefixCls}-offset-${this.nzOffset}` ]: isNotNil(this.nzOffset),
[ `${this.prefixCls}-pull-${this.nzPull}` ] : isNotNil(this.nzPull),
[ `${this.prefixCls}-push-${this.nzPush}` ] : isNotNil(this.nzPush),
...this.generateClass()
};
this.nzUpdateHostClassService.updateHostClass(this.el, classMap);
}

generateClass(): object {
const listOfSizeInputName = [ 'nzXs', 'nzSm', 'nzMd', 'nzLg', 'nzXl', 'nzXXl' ];
const listClassMap = {};
listOfSizeInputName.forEach(name => {
const sizeName = name.replace('nz', '').toLowerCase();
if (isNotNil(this[ name ])) {
if ((typeof (this[ name ]) === 'number') || (typeof (this[ name ]) === 'string')) {
listClassMap[ `${this.prefixCls}-${sizeName}-${this[ name ]}` ] = true;
} else {
listClassMap[ `${this.prefixCls}-${sizeName}-${this[ name ].span}` ] = this[ name ] && isNotNil(this[ name ].span);
listClassMap[ `${this.prefixCls}-${sizeName}-pull-${this[ name ].pull}` ] = this[ name ] && isNotNil(this[ name ].pull);
listClassMap[ `${this.prefixCls}-${sizeName}-push-${this[ name ].push}` ] = this[ name ] && isNotNil(this[ name ].push);
listClassMap[ `${this.prefixCls}-${sizeName}-offset-${this[ name ].offset}` ] = this[ name ] && isNotNil(this[ name ].offset);
listClassMap[ `${this.prefixCls}-${sizeName}-order-${this[ name ].order}` ] = this[ name ] && isNotNil(this[ name ].order);
}
}

});
return listClassMap;
}

constructor(private nzUpdateHostClassService: NzUpdateHostClassService,
private elementRef: ElementRef,
@Optional() @Host() public nzRowDirective: NzRowDirective,
public renderer: Renderer2
) {
}

ngOnChanges(changes: { [ propertyName: string ]: SimpleChange }): void {
this.setClassMap();
}

ngAfterViewInit(): void {
if (this.nzRowDirective) {
this.nzRowDirective.actualGutter$.pipe(
startWith(this.nzRowDirective.actualGutter),
takeUntil(this.destroy$)
).subscribe((actualGutter) => {
this.renderer.setStyle(this.el, 'padding-left', `${actualGutter / 2}px`);
this.renderer.setStyle(this.el, 'padding-right', `${actualGutter / 2}px`);
});
}
}

ngOnInit(): void {
this.setClassMap();
}

ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}
6 changes: 2 additions & 4 deletions components/grid/nz-grid.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import { PlatformModule } from '@angular/cdk/platform';
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';

import { NzColComponent } from './nz-col.component';
import { NzColDirective } from './nz-col.directive';
import { NzRowComponent } from './nz-row.component';
import { NzRowDirective } from './nz-row.directive';

@NgModule({
declarations: [ NzRowComponent, NzColDirective, NzColComponent, NzRowDirective ],
exports : [ NzRowComponent, NzColDirective, NzColComponent, NzRowDirective ],
declarations: [ NzColDirective, NzRowDirective ],
exports : [ NzColDirective, NzRowDirective ],
imports : [ CommonModule, LayoutModule, PlatformModule ]
})
export class NzGridModule {
Expand Down
1 change: 0 additions & 1 deletion components/grid/nz-row.component.html

This file was deleted.

Loading

0 comments on commit ab05619

Please sign in to comment.