-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module:tabs): add nzCanDeactivate hook (#4476)
* feat(module:tabs): add and hooks * feat(module:tabs): fix bug with onpush * feat(module:tabs): remove canDeactivate and add test * feat(module:tabs): update demo * feat(module:tabs): modify api name * feat(module:tabs): remove console close #4432
- Loading branch information
Showing
12 changed files
with
235 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @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 { from, isObservable, Observable, of } from 'rxjs'; | ||
import { isPromise } from './is-promise'; | ||
|
||
export function wrapIntoObservable<T>(value: T | Promise<T> | Observable<T>): Observable<T> { | ||
if (isObservable(value)) { | ||
return value; | ||
} | ||
|
||
if (isPromise(value)) { | ||
// Use `Promise.resolve()` to wrap promise-like instances. | ||
return from(Promise.resolve(value)); | ||
} | ||
|
||
return of(value); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
order: 14 | ||
title: | ||
zh-CN: 标签守卫 | ||
en-US: Tab guard | ||
--- | ||
|
||
## zh-CN | ||
|
||
通过 `nzCanDeactivate` 决定一个 tab 是否可以被切换。 | ||
|
||
## en-US | ||
|
||
Via `nzCanDeactivate` to determine if a tab can be deactivated. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { NzModalService } from 'ng-zorro-antd/modal'; | ||
import { NzTabsCanDeactivateFn } from 'ng-zorro-antd/tabs'; | ||
import { Observable } from 'rxjs'; | ||
|
||
@Component({ | ||
selector: 'nz-demo-tabs-guard', | ||
template: ` | ||
<nz-tabset [nzCanDeactivate]="canDeactivate"> | ||
<nz-tab *ngFor="let tab of tabs" [nzTitle]="'Tab' + tab"> Content of tab {{ tab }} </nz-tab> | ||
</nz-tabset> | ||
`, | ||
changeDetection: ChangeDetectionStrategy.OnPush | ||
}) | ||
export class NzDemoTabsGuardComponent { | ||
tabs = [1, 2, 3, 4]; | ||
constructor(private modal: NzModalService) {} | ||
|
||
canDeactivate: NzTabsCanDeactivateFn = (fromIndex: number, toIndex: number) => { | ||
switch (fromIndex) { | ||
case 0: | ||
return toIndex === 1; | ||
case 1: | ||
return Promise.resolve(toIndex === 2); | ||
case 2: | ||
return this.confirm(); | ||
default: | ||
return true; | ||
} | ||
}; | ||
|
||
private confirm(): Observable<boolean> { | ||
return new Observable(observer => { | ||
this.modal.confirm({ | ||
nzTitle: 'Are you sure you want to leave this tab?', | ||
nzOnOk: () => { | ||
observer.next(true); | ||
observer.complete(); | ||
}, | ||
nzOnCancel: () => { | ||
observer.next(false); | ||
observer.complete(); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters