Skip to content

Commit

Permalink
#7 added stringify and transformer (prizm)
Browse files Browse the repository at this point in the history
  • Loading branch information
Качалкова Кристина Романовна committed Apr 27, 2024
1 parent 4a63e6f commit 81eed5c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
</prizm-input-layout>

<prizm-input-layout label="Критерий">
<prizm-input-select #input formControlName="criterion" [items]="items"
[prizmHintCanShow]="true"></prizm-input-select>
<prizm-input-select #input formControlName="criterion" [stringify]="stringify" [items]="items"
[transformer]="transformer" [prizmHintCanShow]="true"></prizm-input-select>
<ng-template [control]="input" prizmInputStatusText></ng-template>
</prizm-input-layout>
</form>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Inject, OnInit, Output } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MAT_DATE_LOCALE } from '@angular/material/core';
import { PrizmSelectStringify, PrizmSelectValueTransformver } from '@prizm-ui/components';
import 'moment/locale/ru';
import { Subject, debounceTime, distinctUntilChanged, takeUntil } from 'rxjs';

Expand All @@ -13,12 +14,20 @@ import { Subject, debounceTime, distinctUntilChanged, takeUntil } from 'rxjs';

export class FilterFormComponent implements OnInit {

// readonly items = [
// 'По названию',
// 'По описанию',
// 'По местоположению',
// 'По дате',
// 'По автору'
// ];

readonly items = [
'По названию',
'По описанию',
'По местоположению',
'По дате',
'По автору'
{ key: 'name', title: 'По названию' },
{ key: 'description', title: 'По описанию' },
{ key: 'location', title: 'По местоположению' },
{ key: 'time', title: 'По дате' },
{ key: 'owner', title: 'По автору' },
];

filterForm: FormGroup;
Expand All @@ -34,34 +43,26 @@ export class FilterFormComponent implements OnInit {
criterion: new FormControl<'name' | 'description' | 'location' | 'time' | 'owner'>('name', [Validators.required])
});
}
readonly stringify: PrizmSelectStringify<{ key: string, title: string }> = (item: { key: string, title: string }) => {
if (!item) {

This comment has been minimized.

Copy link
@Anna-Prokhorova

Anna-Prokhorova May 2, 2024

Можно использовать тернарник.
Вообще с точки зрения скорости выполнения, разницы нет. Здесь скорее вопрос читаемости кода.
Обычно можно просто следовать правилу: если код помещается в одну строку - используем тернарный оператор, если в одну строку он уже не помещается - используем if-else.

return '';
}
return item.title
};
readonly transformer: PrizmSelectValueTransformver<{ key: string, title: string }> = (item) => {
if (!item) { return '' }

This comment has been minimized.

Copy link
@Anna-Prokhorova

Anna-Prokhorova May 2, 2024

И здесь соответственно тоже тернарник

return item.key
};

ngOnInit(): void {

this.filterForm.controls['search'].valueChanges
.pipe(
debounceTime(500),
distinctUntilChanged(),
takeUntil(this.destroy))
.subscribe((data) => {
if (this.filterForm.invalid) { return }

switch (this.filterForm.value.criterion) {
case 'По названию':
this.filterForm.value.criterion = 'name'
break;
case 'По описанию':
this.filterForm.value.criterion = 'description'
break;
case 'По местоположению':
this.filterForm.value.criterion = 'location'
break;
case 'По дате':
this.filterForm.value.criterion = 'time'
break;
case 'По автору':
this.filterForm.value.criterion = 'owner'
break;
default:
break;
}
this.filterEvent.emit({ search: data, criterion: this.filterForm.value.criterion })
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class UserFormComponent {
@Output() closeFormEvent = new EventEmitter();
private destroy: Subject<void> = new Subject();


constructor(
private fb: FormBuilder
) {
Expand Down

0 comments on commit 81eed5c

Please sign in to comment.