Skip to content

Commit

Permalink
feat(module:*): global config
Browse files Browse the repository at this point in the history
poc: use decorator to extract property
  • Loading branch information
Wendell committed Jun 14, 2019
1 parent e59171b commit 10b19e0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 4 deletions.
6 changes: 5 additions & 1 deletion components/button/nz-button.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
findFirstNotEmptyNode,
findLastNotEmptyNode,
isEmpty,
ExtractFromConfig,
InputBoolean,
NzSizeLDSType,
NzSizeMap,
Expand All @@ -43,6 +44,8 @@ import {
} from 'ng-zorro-antd/core';
import { NzIconDirective } from 'ng-zorro-antd/icon';

import { NzButtonConfig, NZ_BUTTON_CONFIG } from './nz-button.config';

export type NzButtonType = 'primary' | 'dashed' | 'danger' | 'default' | 'link';
export type NzButtonShape = 'circle' | 'round' | null;

Expand Down Expand Up @@ -73,7 +76,7 @@ export class NzButtonComponent implements AfterContentInit, OnInit, OnDestroy, O
@Input() @InputBoolean() nzLoading = false;
@Input() nzType: NzButtonType = 'default';
@Input() nzShape: NzButtonShape = null;
@Input() nzSize: NzSizeLDSType = 'default';
@ExtractFromConfig('default') @Input() nzSize: NzSizeLDSType;

/** temp solution since no method add classMap to host https://github.com/angular/angular/issues/7289 */
setClassMap(): void {
Expand Down Expand Up @@ -135,6 +138,7 @@ export class NzButtonComponent implements AfterContentInit, OnInit, OnDestroy, O
private renderer: Renderer2,
private nzUpdateHostClassService: NzUpdateHostClassService,
private ngZone: NgZone,
@Optional() @Inject(NZ_BUTTON_CONFIG) public defaultConfig: NzButtonConfig,
@Optional() @Inject(NZ_WAVE_GLOBAL_CONFIG) private waveConfig: NzWaveConfig,
@Optional() @Inject(ANIMATION_MODULE_TYPE) private animationType: string
) {
Expand Down
17 changes: 17 additions & 0 deletions components/button/nz-button.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @license
* Copyright Alibaba.com All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { InjectionToken } from '@angular/core';

import { NzSizeLDSType } from '../core/types';

export const NZ_BUTTON_CONFIG = new InjectionToken<NzButtonConfig>('nz-button-config');

export interface NzButtonConfig {
nzSize?: NzSizeLDSType;
}
1 change: 1 addition & 0 deletions components/button/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
export * from './nz-button.component';
export * from './nz-button-group.component';
export * from './nz-button.module';
export * from './nz-button.config';
2 changes: 1 addition & 1 deletion components/core/util/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function propDecoratorFactory<T, D>(name: string, fallback: (v: T) => D): (targe
const privatePropName = `$$__${propName}`;

if (Object.prototype.hasOwnProperty.call(target, privatePropName)) {
console.warn(`The prop "${privatePropName}" is already exist, it will be overrided by ${name} decorator.`);
console.warn(`The prop "${privatePropName}" is already exist, it will be override by ${name} decorator.`);
}

Object.defineProperty(target, privatePropName, {
Expand Down
39 changes: 39 additions & 0 deletions components/core/util/global-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @license
* Copyright Alibaba.com All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

// tslint:disable:no-invalid-this
// tslint:disable:no-any

/**
* This decorator is used to decorate properties. If a property is decorated, it would try to
*/
export function ExtractFromConfig<T>(innerDefaultValue: T): (t: any, p: string) => void {
return function extractDecorator(target: any, propName: string): void {
const privatePropName = `$$__defaultExtractedValue__${propName}`;

if (Object.prototype.hasOwnProperty.call(target, privatePropName)) {
console.warn(`The prop "${privatePropName}" is already exist, it will be override by ${name} decorator.`);
}

Object.defineProperty(target, privatePropName, {
configurable: true,
writable: true
});

Object.defineProperty(target, propName, {
get(): T {
const defaultConfig = this.defaultConfig;
return this[privatePropName] || defaultConfig[propName] || innerDefaultValue;
},
set(value?: T): void {
const defaultConfig = this.defaultConfig;
this[privatePropName] = value === undefined ? defaultConfig[propName] || innerDefaultValue : value;
}
});
};
}
1 change: 1 addition & 0 deletions components/core/util/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './scroll-into-view-if-needed';
export * from './textarea-caret-position';
export * from './throttleByAnimationFrame';
export * from './time';
export * from './global-config';
5 changes: 3 additions & 2 deletions scripts/site/_site/doc/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PreloadAllModules, RouterModule } from '@angular/router';
import { ServiceWorkerModule } from '@angular/service-worker';
import { IconDefinition } from '@ant-design/icons-angular';
import { LeftOutline, RightOutline } from '@ant-design/icons-angular/icons';
import { NgZorroAntdModule, NZ_ICON_DEFAULT_TWOTONE_COLOR, NZ_ICONS } from 'ng-zorro-antd';
import { NgZorroAntdModule, NZ_ICON_DEFAULT_TWOTONE_COLOR, NZ_ICONS, NZ_BUTTON_CONFIG } from 'ng-zorro-antd';

import { environment } from '../environments/environment';
import { DEMOComponent } from './_demo/demo.component';
Expand All @@ -33,7 +33,8 @@ const icons: IconDefinition[] = [LeftOutline, RightOutline];
providers: [
Title,
{ provide: NZ_ICONS, useValue: icons },
{ provide: NZ_ICON_DEFAULT_TWOTONE_COLOR, useValue: '#1890ff' }
{ provide: NZ_ICON_DEFAULT_TWOTONE_COLOR, useValue: '#1890ff' },
{ provide: NZ_BUTTON_CONFIG, useValue: { nzSize: 'large' }}
],
bootstrap: [AppComponent]
})
Expand Down

0 comments on commit 10b19e0

Please sign in to comment.