Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cdk): implement tuiIsPresent pipe #3002

Merged
merged 3 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions projects/cdk/pipes/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from '@taiga-ui/cdk/pipes/filter';
export * from '@taiga-ui/cdk/pipes/is-present';
export * from '@taiga-ui/cdk/pipes/keys';
export * from '@taiga-ui/cdk/pipes/mapper';
2 changes: 2 additions & 0 deletions projects/cdk/pipes/is-present/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './is-present.module';
export * from './is-present.pipe';
9 changes: 9 additions & 0 deletions projects/cdk/pipes/is-present/is-present.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {NgModule} from '@angular/core';

import {TuiIsPresentPipe} from './is-present.pipe';

@NgModule({
declarations: [TuiIsPresentPipe],
exports: [TuiIsPresentPipe],
})
export class TuiIsPresentPipeModule {}
9 changes: 9 additions & 0 deletions projects/cdk/pipes/is-present/is-present.pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Pipe, PipeTransform} from '@angular/core';
import {tuiIsPresent} from '@taiga-ui/cdk/utils';

@Pipe({name: `tuiIsPresent`})
export class TuiIsPresentPipe implements PipeTransform {
transform<T>(value?: T | null): value is T {
return tuiIsPresent(value);
}
}
5 changes: 5 additions & 0 deletions projects/cdk/pipes/is-present/ng-package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lib": {
"entryFile": "index.ts"
}
}
9 changes: 9 additions & 0 deletions projects/demo/src/modules/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1429,6 +1429,15 @@ export const ROUTES = [
title: `FormatPhone`,
},
},
{
path: `pipes/is-present`,
loadChildren: async () =>
(await import(`../pipes/is-present/is-present.module`))
.ExampleTuiIsPresentModule,
data: {
title: `IsPresent`,
},
},
{
path: `pipes/mapper`,
loadChildren: async () =>
Expand Down
6 changes: 6 additions & 0 deletions projects/demo/src/modules/app/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,12 @@ export const pages: TuiDocPages = [
keywords: `format, форматирование, преобразование, phone, телефон, пайп, pipe`,
route: `/pipes/format-phone`,
},
{
section: $localize`Tools`,
title: `IsPresent`,
keywords: `present, существует, ngif, if, пайп, pipe, null, undefined, nil`,
route: `/pipes/is-present`,
},
{
section: $localize`Tools`,
title: `Mapper`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {Component} from '@angular/core';
import {FormControl} from '@angular/forms';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import {tuiIsPresent} from '@taiga-ui/cdk';

@Component({
selector: `tui-is-present-example1`,
templateUrl: `./template.html`,
styleUrls: [`./style.less`],
changeDetection,
encapsulation,
})
export class TuiIsPresentExample1 {
splincode marked this conversation as resolved.
Show resolved Hide resolved
readonly items = [`String`, `null`, `undefined`];
readonly value = new FormControl(null);

get isPresent(): boolean {
const {value} = this.value;
const objectedValue = this.objectifyValue(value ?? ``);

return tuiIsPresent(objectedValue);
}

private objectifyValue(value: string): string | null | undefined {
if (value === `null`) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Looks like a job for switch :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want to keep this example or completely replace it with the one above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and replaced it since the new example is more useful 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@waterplea please check if it's not too complicated

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Thank you!

return null;
}

if (value === `undefined`) {
return undefined;
}

return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:host {
display: block;
max-width: 20rem;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{ isPresent }} = isPresent(value);

<tui-select
class="tui-space_top-2"
[formControl]="value"
>
value
<tui-data-list-wrapper
*tuiDataList
[items]="items"
></tui-data-list-wrapper>
</tui-select>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
```ts
import {TuiIsPresentPipeModule} from '@taiga-ui/cdk';

// ...

@NgModule({
imports: [
// ...
TuiIsPresentPipeModule,
],
// ...
})
export class MyModule {}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```html
<div *ngIf="item | tuiIsPresent">{{item}}</div>
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {Component} from '@angular/core';
import {changeDetection} from '@demo/emulate/change-detection';
import {TuiDocExample} from '@taiga-ui/addon-doc';

@Component({
selector: `example-tui-is-present`,
templateUrl: `./is-present.template.html`,
changeDetection,
})
export class ExampleTuiIsPresentComponent {
readonly exampleModule = import(`./examples/import/import-module.md?raw`);
readonly exampleHtml = import(`./examples/import/insert-template.md?raw`);

readonly example1: TuiDocExample = {
TypeScript: import(`./examples/1/component.ts?raw`),
HTML: import(`./examples/1/template.html?raw`),
};
}
27 changes: 27 additions & 0 deletions projects/demo/src/modules/pipes/is-present/is-present.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core';
import {ReactiveFormsModule} from '@angular/forms';
import {RouterModule} from '@angular/router';
import {TuiAddonDocModule, tuiGenerateRoutes} from '@taiga-ui/addon-doc';
import {TuiIsPresentPipeModule} from '@taiga-ui/cdk';
import {TuiLinkModule} from '@taiga-ui/core';
import {TuiDataListWrapperModule, TuiSelectModule} from '@taiga-ui/kit';

import {TuiIsPresentExample1} from './examples/1/component';
import {ExampleTuiIsPresentComponent} from './is-present.component';

@NgModule({
imports: [
CommonModule,
TuiAddonDocModule,
RouterModule.forChild(tuiGenerateRoutes(ExampleTuiIsPresentComponent)),
ReactiveFormsModule,
TuiIsPresentPipeModule,
TuiLinkModule,
TuiSelectModule,
TuiDataListWrapperModule,
],
declarations: [ExampleTuiIsPresentComponent, TuiIsPresentExample1],
exports: [ExampleTuiIsPresentComponent],
})
export class ExampleTuiIsPresentModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<tui-doc-page
header="IsPresent"
package="CDK"
type="pipes"
>
<ng-template
i18n
pageTab
>
Pipe wrapper for
<a
tuiLink
routerLink="/utils/miscellaneous"
fragment="isPresent"
>
<code>IsPresent</code>
</a>
function

<tui-doc-example
id="base"
i18n-heading
heading="Basic"
[content]="example1"
>
<tui-is-present-example1></tui-is-present-example1>
</tui-doc-example>
</ng-template>

<ng-template pageTab="Setup">
<ol class="b-demo-steps">
<li>
<p i18n>
Import
<code>TuiIsPresentPipeModule</code>
into a module where you want to use our pipe
</p>

<tui-doc-code
filename="myComponent.module.ts"
[code]="exampleModule"
></tui-doc-code>
</li>

<li>
<p i18n>Use pipe in template:</p>

<tui-doc-code
filename="myComponent.template.html"
[code]="exampleHtml"
></tui-doc-code>
</li>
</ol>
</ng-template>
</tui-doc-page>