Skip to content

Commit 150c6ca

Browse files
authored
feat: add provide function (#7952)
* feat(core:config): add provideNzConfig function * doc: update * doc: update * add provideNzWave and provideNzI18n
1 parent 71ead99 commit 150c6ca

12 files changed

Lines changed: 56 additions & 53 deletions

components/core/config/config.spec.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { By } from '@angular/platform-browser';
44

55
import { NzButtonComponent, NzButtonModule } from 'ng-zorro-antd/button';
66

7-
import { NZ_CONFIG } from './config';
7+
import { provideNzConfig } from './config';
88
import { NzConfigService } from './config.service';
99

1010
@Component({
@@ -56,14 +56,11 @@ describe('nz global config', () => {
5656
imports: [NzButtonModule],
5757
declarations: [NzGlobalConfigTestBasicComponent],
5858
providers: [
59-
{
60-
provide: NZ_CONFIG,
61-
useValue: {
62-
button: {
63-
nzSize: 'large'
64-
}
59+
provideNzConfig({
60+
button: {
61+
nzSize: 'large'
6562
}
66-
}
63+
})
6764
]
6865
}).compileComponents();
6966
})

components/core/config/config.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { Direction } from '@angular/cdk/bidi';
7-
import { InjectionToken, TemplateRef, Type } from '@angular/core';
7+
import { EnvironmentProviders, InjectionToken, makeEnvironmentProviders, TemplateRef, Type } from '@angular/core';
88
import { SafeUrl } from '@angular/platform-browser';
99

1010
import { ThemeType } from '@ant-design/icons-angular';
@@ -381,3 +381,7 @@ export type NzConfigKey = keyof NzConfig;
381381
* User should provide an object implements this interface to set global configurations.
382382
*/
383383
export const NZ_CONFIG = new InjectionToken<NzConfig>('nz-config');
384+
385+
export function provideNzConfig(config: NzConfig): EnvironmentProviders {
386+
return makeEnvironmentProviders([{ provide: NZ_CONFIG, useValue: config }]);
387+
}

components/core/wave/nz-wave.directive.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
Inject,
1111
InjectionToken,
1212
Input,
13+
makeEnvironmentProviders,
14+
EnvironmentProviders,
1315
NgZone,
1416
OnDestroy,
1517
OnInit,
@@ -30,13 +32,10 @@ export const NZ_WAVE_GLOBAL_DEFAULT_CONFIG: NzWaveConfig = {
3032
disabled: false
3133
};
3234

33-
export const NZ_WAVE_GLOBAL_CONFIG = new InjectionToken<NzWaveConfig>('nz-wave-global-options', {
34-
providedIn: 'root',
35-
factory: NZ_WAVE_GLOBAL_CONFIG_FACTORY
36-
});
35+
export const NZ_WAVE_GLOBAL_CONFIG = new InjectionToken<NzWaveConfig>('nz-wave-global-options');
3736

38-
export function NZ_WAVE_GLOBAL_CONFIG_FACTORY(): NzWaveConfig {
39-
return NZ_WAVE_GLOBAL_DEFAULT_CONFIG;
37+
export function provideNzWave(config: NzWaveConfig): EnvironmentProviders {
38+
return makeEnvironmentProviders([{ provide: NZ_WAVE_GLOBAL_CONFIG, useValue: config }]);
4039
}
4140

4241
@Directive({

components/core/wave/nz-wave.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
import { PlatformModule } from '@angular/cdk/platform';
77
import { NgModule } from '@angular/core';
88

9-
import { NzWaveDirective } from './nz-wave.directive';
9+
import { NzWaveDirective, provideNzWave, NZ_WAVE_GLOBAL_DEFAULT_CONFIG } from './nz-wave.directive';
1010

1111
@NgModule({
1212
imports: [PlatformModule],
1313
exports: [NzWaveDirective],
14-
declarations: [NzWaveDirective]
14+
declarations: [NzWaveDirective],
15+
providers: [provideNzWave(NZ_WAVE_GLOBAL_DEFAULT_CONFIG)]
1516
})
1617
export class NzWaveModule {}

components/i18n/nz-i18n.service.spec.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { Component, OnDestroy } from '@angular/core';
1+
import { Component, Inject, OnDestroy } from '@angular/core';
22
import { ComponentFixture, inject, TestBed } from '@angular/core/testing';
33
import { Subscription } from 'rxjs';
44

5+
import { NZ_I18N, provideNzI18n } from 'ng-zorro-antd/i18n/nz-i18n.token';
6+
57
import cs_CZ from './languages/cs_CZ';
68
import de_DE from './languages/de_DE';
79
import en_US from './languages/en_US';
@@ -20,7 +22,8 @@ describe('i18n service', () => {
2022
beforeEach(() => {
2123
TestBed.configureTestingModule({
2224
declarations: [NzI18nTestComponent],
23-
imports: [NzI18nModule]
25+
imports: [NzI18nModule],
26+
providers: [provideNzI18n(DEFAULT_LAN)]
2427
}).compileComponents();
2528
});
2629

@@ -42,6 +45,11 @@ describe('i18n service', () => {
4245
console.log(i18nEN, i18nDE, i18nCS, i18nKA);
4346
});
4447

48+
it('should be provide interface be right', () => {
49+
fixture = TestBed.createComponent(NzI18nTestComponent);
50+
expect(fixture.componentInstance.locale === DEFAULT_LAN).toBe(true);
51+
});
52+
4553
it('should be auto default zh_CN', () => {
4654
expect(testComponent.locale.locale).toBe(DEFAULT_LAN.locale);
4755
});
@@ -79,11 +87,9 @@ https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/CONTRIBUTING.md`
7987
template: ''
8088
})
8189
export class NzI18nTestComponent implements OnDestroy {
82-
locale!: NzI18nInterface;
83-
8490
private localeSubscription: Subscription;
8591

86-
constructor(private nzI18nService: NzI18nService) {
92+
constructor(private nzI18nService: NzI18nService, @Inject(NZ_I18N) public locale: NzI18nInterface) {
8793
this.localeSubscription = this.nzI18nService.localeChange.subscribe(locale => {
8894
this.updateLocale(locale);
8995
});

components/i18n/nz-i18n.token.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
44
*/
55

6-
import { InjectionToken } from '@angular/core';
6+
import { InjectionToken, makeEnvironmentProviders, EnvironmentProviders } from '@angular/core';
77

88
import { DateLocale, NzI18nInterface } from './nz-i18n.interface';
99

1010
export const NZ_I18N = new InjectionToken<NzI18nInterface>('nz-i18n');
1111

12+
export function provideNzI18n(config: NzI18nInterface): EnvironmentProviders {
13+
return makeEnvironmentProviders([{ provide: NZ_I18N, useValue: config }]);
14+
}
15+
1216
/** Locale for date operations, should import from date-fns, see example: https://github.com/date-fns/date-fns/blob/v1.30.1/src/locale/zh_cn/index.js */
1317
export const NZ_DATE_LOCALE = new InjectionToken<DateLocale>('nz-date-locale');

docs/animations.en-US.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,15 @@ Add the following configuration while invoking components' services.
6767

6868
### Turn Off The Wave Effect
6969

70-
Some components use dynamic styles to support wave effects, so their styles are unable to be override directly. Instead, you can set the provider `NZ_WAVE_GLOBAL_CONFIG` or use
70+
Some components use dynamic styles to support wave effects, so their styles are unable to be override directly. Instead, you can use `provideNzWave` or use
7171
`NoopAnimationsModule` to turn off the wave effects.
7272

7373
```ts
74+
import { provideNzWave } from 'ng-zorro-antd/core/wave';
75+
7476
@NgModule({
75-
...
76-
providers : [
77-
...,
78-
{
79-
provide: NZ_WAVE_GLOBAL_CONFIG, useValue: {
80-
disabled: true
81-
}
82-
}
83-
]
77+
providers: [
78+
provideNzWave({ disabled: true })
79+
]
8480
})
8581
```

docs/animations.zh-CN.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,15 @@ import { NzNoAnimationModule } from 'ng-zorro-antd/core/no-animation';
6565

6666
### 关闭波浪效果
6767

68-
部分组件(如:Button)为了支持波纹效果,使用了动态样式,因此无法直接使用样式覆盖。但是你可以通过设置提供商 `NZ_WAVE_GLOBAL_CONFIG`
68+
部分组件(如:Button)为了支持波纹效果,使用了动态样式,因此无法直接使用样式覆盖。但是你可以通过 `provideNzWave`
6969
或者使用 `NoopAnimationsModule` 来关闭波浪效果。
7070

7171
```ts
72+
import { provideNzWave } from 'ng-zorro-antd/core/wave';
73+
7274
@NgModule({
73-
...
74-
providers : [
75-
...,
76-
{
77-
provide: NZ_WAVE_GLOBAL_CONFIG, useValue: {
78-
disabled: true
79-
}
80-
}
75+
providers: [
76+
provideNzWave({ disabled: true })
8177
]
8278
})
8379
```

docs/global-config.en-US.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ We add a **global configuration** support to many components. You can define the
77

88
## How to Use?
99

10-
In order to provide default configurations in certain components, please pass an object that implements the interface `NzConfig` through the injection token `NZ_CONFIG` in the root injector. For example:
10+
In order to provide default configurations in certain components, please use `provideNzConfig` function. object providing implements interface `NzConfig`For example:
1111

1212
```typescript
13-
import { NzConfig, NZ_CONFIG } from 'ng-zorro-antd/core/config';
13+
import { NzConfig, provideNzConfig } from 'ng-zorro-antd/core/config';
1414

1515
const ngZorroConfig: NzConfig = {
1616
message: { nzTop: 120 },
@@ -23,7 +23,7 @@ const ngZorroConfig: NzConfig = {
2323
CommonModule
2424
],
2525
providers: [
26-
{ provide: NZ_CONFIG, useValue: ngZorroConfig }
26+
provideNzConfig(ngZorroConfig)
2727
],
2828
bootstrap: [AppComponent]
2929
})

docs/global-config.zh-CN.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ title: 全局配置项
77

88
## 如何使用
99

10-
想要为某些组件提供默认配置项,请在根注入器中根据注入令牌 `NZ_CONFIG` 提供一个符合 `NzConfig` 接口的对象例如:
10+
想要为某些组件提供默认配置项, 可以使用 `provideNzConfig` 函数,传入一个符合 `NzConfig` 接口的对象例如:
1111

1212
```typescript
13-
import { NzConfig, NZ_CONFIG } from 'ng-zorro-antd/core/config';
13+
import { NzConfig, provideNzConfig } from 'ng-zorro-antd/core/config';
1414

1515
const ngZorroConfig: NzConfig = {
1616
// 注意组件名称没有 nz 前缀
@@ -24,7 +24,7 @@ const ngZorroConfig: NzConfig = {
2424
CommonModule
2525
],
2626
providers: [
27-
{ provide: NZ_CONFIG, useValue: ngZorroConfig }
27+
provideNzConfig(ngZorroConfig)
2828
],
2929
bootstrap: [AppComponent]
3030
})

0 commit comments

Comments
 (0)