Skip to content

chore(docs): added tag input validation example (#DS-2939) #340

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

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
122 changes: 118 additions & 4 deletions packages/components-dev/tag/module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import { AfterViewInit, Component, ElementRef, NgModule, ViewChild, ViewEncapsulation } from '@angular/core';
import { FormsModule, ReactiveFormsModule, UntypedFormControl } from '@angular/forms';
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
ElementRef,
NgModule,
ViewChild,
ViewEncapsulation
} from '@angular/core';
import {
AbstractControl,
FormControl,
FormsModule,
ReactiveFormsModule,
UntypedFormControl,
ValidationErrors,
ValidatorFn,
Validators
} from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { COMMA, ENTER, SPACE, TAB } from '@koobiq/cdk/keycodes';
import { KbqAutocomplete, KbqAutocompleteModule, KbqAutocompleteSelectedEvent } from '@koobiq/components/autocomplete';
import { KbqComponentColors } from '@koobiq/components/core';
import { KBQ_VALIDATION, KbqComponentColors, KbqValidationOptions } from '@koobiq/components/core';
import { KbqFormFieldModule } from '@koobiq/components/form-field';
import { KbqIconModule } from '@koobiq/components/icon';
import { KbqInputModule } from '@koobiq/components/input';
import {
KBQ_TAGS_DEFAULT_OPTIONS,
KbqTag,
Expand Down Expand Up @@ -233,6 +251,101 @@ export class DemoComponent implements AfterViewInit {
})
export class TagInputDefaultOptionsOverrideComponent extends DemoComponent {}

const customMaxLengthValidator = (max: number): ValidatorFn => {
return ({ value }: AbstractControl): ValidationErrors | null => {
if (!value) {
return null;
}
return value.length <= max ? null : { customMaxLengthValidator: true };
};
};

@Component({
standalone: true,
imports: [
KbqFormFieldModule,
KbqInputModule,
KbqTagsModule,
ReactiveFormsModule,
KbqIconModule
],
providers: [
// Disables KbqValidateDirective
{
provide: KBQ_VALIDATION,
useValue: {
useValidation: false
} satisfies KbqValidationOptions
}
],
selector: 'tag-input-validation',
template: `
<kbq-form-field>
<kbq-tag-list
#inputTagList
[formControl]="formControl"
>
@for (tag of formControl.value; track tag) {
<kbq-tag
[value]="tag"
(removed)="removeTag(tag)"
>
{{ tag }}
<i
kbq-icon-button="kbq-xmark-s_16"
kbqTagRemove
></i>
</kbq-tag>
}

<input
[kbqTagInputFor]="inputTagList"
(kbqTagInputTokenEnd)="createTag($event)"
kbqInput
placeholder="New keyword..."
/>
</kbq-tag-list>

@if (formControl.invalid) {
<kbq-hint color="error">
@if (formControl.hasError('required')) {
Field is required
}

@if (formControl.hasError('customMaxLengthValidator')) {
Max keywords count is 3
}
</kbq-hint>
}
</kbq-form-field>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class TagInputValidation {
readonly formControl = new FormControl(
['Koobiq', 'Angular', 'Design'],
[Validators.required, customMaxLengthValidator(3)]
);

removeTag(tag: string): void {
const tags = this.formControl.value || [];
const index = tags.indexOf(tag);
if (index >= 0) {
tags.splice(index, 1);
this.formControl.setValue(tags);
}
}

createTag({ value, input }: KbqTagInputEvent): void {
if (value) {
const tags = this.formControl.value || [];
tags.push(value);
this.formControl.setValue(tags);
}
input.value = '';
}
}

@NgModule({
declarations: [DemoComponent, TagInputDefaultOptionsOverrideComponent],
imports: [
Expand All @@ -244,7 +357,8 @@ export class TagInputDefaultOptionsOverrideComponent extends DemoComponent {}
KbqAutocompleteModule,
KbqTagsModule,
KbqIconModule,
KbqTitleModule
KbqTitleModule,
TagInputValidation
],
bootstrap: [DemoComponent]
})
Expand Down
10 changes: 6 additions & 4 deletions packages/components-dev/tag/styles.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@use '../main';

.example-container {
app {
display: block;
width: 50%;
}

Expand All @@ -10,9 +11,10 @@
}

.dev-container {
width: 500px;

border: 1px solid red;

padding: 24px;
}

hr {
margin: 30px 0;
}
Loading