From af77aaf9c2d7bd769f2557ad61e358f6d0413782 Mon Sep 17 00:00:00 2001 From: Michael M Date: Wed, 26 Jan 2022 13:42:17 +0400 Subject: [PATCH] feat(core): default options configuration for `PrimitiveTextfield` (#1249) --- .../components/primitive-textfield/index.ts | 1 + .../primitive-textfield-options.ts | 25 +++++++ .../primitive-textfield.component.ts | 19 +++-- .../primitive-textfield.template.html | 11 ++- .../primitive-textfield/examples/2/index.html | 39 +++++++++++ .../primitive-textfield/examples/2/index.ts | 69 +++++++++++++++++++ .../primitive-textfield.component.ts | 32 +++++---- .../primitive-textfield.module.ts | 10 ++- .../primitive-textfield.template.html | 11 +++ 9 files changed, 192 insertions(+), 25 deletions(-) create mode 100644 projects/core/components/primitive-textfield/primitive-textfield-options.ts create mode 100644 projects/demo/src/modules/components/primitive-textfield/examples/2/index.html create mode 100644 projects/demo/src/modules/components/primitive-textfield/examples/2/index.ts diff --git a/projects/core/components/primitive-textfield/index.ts b/projects/core/components/primitive-textfield/index.ts index 43d62cafb0de..6d43993ad00d 100644 --- a/projects/core/components/primitive-textfield/index.ts +++ b/projects/core/components/primitive-textfield/index.ts @@ -1,4 +1,5 @@ export * from './primitive-textfield.component'; export * from './primitive-textfield.module'; export * from './primitive-textfield.providers'; +export * from './primitive-textfield-options'; export * from './value-decoration/value-decoration.component'; diff --git a/projects/core/components/primitive-textfield/primitive-textfield-options.ts b/projects/core/components/primitive-textfield/primitive-textfield-options.ts new file mode 100644 index 000000000000..a2b4bd407dcc --- /dev/null +++ b/projects/core/components/primitive-textfield/primitive-textfield-options.ts @@ -0,0 +1,25 @@ +import {InjectionToken} from '@angular/core'; +import {TuiHorizontalDirection} from '@taiga-ui/core/types'; +import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus'; + +export interface TuiPrimitiveTextfieldOptions { + readonly iconAlign: TuiHorizontalDirection; + readonly iconCleaner: PolymorpheusContent; +} + +// TODO: remove in ivy compilation +export const TUI_PRIMITIVE_TEXTFIELD_ICON_CLEANER = 'tuiIconCloseLarge'; + +/** Default values for primitive textfield options */ +export const TUI_PRIMITIVE_TEXTFIELD_DEFAULT_OPTIONS: TuiPrimitiveTextfieldOptions = { + iconAlign: 'right', + iconCleaner: TUI_PRIMITIVE_TEXTFIELD_ICON_CLEANER, +}; + +export const TUI_PRIMITIVE_TEXTFIELD_OPTIONS = + new InjectionToken( + 'Default parameters for primitive textfield component', + { + factory: () => TUI_PRIMITIVE_TEXTFIELD_DEFAULT_OPTIONS, + }, + ); diff --git a/projects/core/components/primitive-textfield/primitive-textfield.component.ts b/projects/core/components/primitive-textfield/primitive-textfield.component.ts index 21abaa8c7f0e..6297932e7d7b 100644 --- a/projects/core/components/primitive-textfield/primitive-textfield.component.ts +++ b/projects/core/components/primitive-textfield/primitive-textfield.component.ts @@ -29,18 +29,17 @@ import { TuiTextfieldController, } from '@taiga-ui/core/directives/textfield-controller'; import {TUI_MODE, TUI_TEXTFIELD_APPEARANCE} from '@taiga-ui/core/tokens'; -import { - TuiBrightness, - TuiHorizontalDirection, - TuiSizeL, - TuiSizeS, -} from '@taiga-ui/core/types'; +import {TuiBrightness, TuiSizeL, TuiSizeS} from '@taiga-ui/core/types'; import {getBorder} from '@taiga-ui/core/utils/miscellaneous'; import {PolymorpheusContent, PolymorpheusOutletComponent} from '@tinkoff/ng-polymorpheus'; import {fromEvent, Observable} from 'rxjs'; import {map} from 'rxjs/operators'; import {TUI_PRIMITIVE_TEXTFIELD_PROVIDERS} from './primitive-textfield.providers'; +import { + TUI_PRIMITIVE_TEXTFIELD_OPTIONS, + TuiPrimitiveTextfieldOptions, +} from './primitive-textfield-options'; const ICON_PADDING = 1.75; const ICON_PADDING_S = 1.5; @@ -74,13 +73,17 @@ export class TuiPrimitiveTextfieldComponent @Input() @tuiDefaultProp() - iconAlign: TuiHorizontalDirection = 'right'; + iconAlign: TuiPrimitiveTextfieldOptions['iconAlign'] = this.options.iconAlign; // TODO: Remove null in 3.0 @Input() @tuiDefaultProp() iconContent: PolymorpheusContent | null = null; + @Input() + @tuiDefaultProp() + iconCleaner: TuiPrimitiveTextfieldOptions['iconCleaner'] = this.options.iconCleaner; + @Input() @HostBinding('class._readonly') @tuiDefaultProp() @@ -122,6 +125,8 @@ export class TuiPrimitiveTextfieldComponent readonly controller: TuiTextfieldController, @Inject(TUI_HINT_WATCHED_CONTROLLER) readonly hintController: TuiHintControllerDirective, + @Inject(TUI_PRIMITIVE_TEXTFIELD_OPTIONS) + readonly options: TuiPrimitiveTextfieldOptions, ) { super(); } diff --git a/projects/core/components/primitive-textfield/primitive-textfield.template.html b/projects/core/components/primitive-textfield/primitive-textfield.template.html index 673d1eca73ec..439a871ba64a 100644 --- a/projects/core/components/primitive-textfield/primitive-textfield.template.html +++ b/projects/core/components/primitive-textfield/primitive-textfield.template.html @@ -87,13 +87,18 @@ - + > + + + + + + + + + + + + diff --git a/projects/demo/src/modules/components/primitive-textfield/examples/2/index.ts b/projects/demo/src/modules/components/primitive-textfield/examples/2/index.ts new file mode 100644 index 000000000000..265269b13f59 --- /dev/null +++ b/projects/demo/src/modules/components/primitive-textfield/examples/2/index.ts @@ -0,0 +1,69 @@ +import { + ChangeDetectorRef, + Component, + Inject, + Optional, + Self, + ViewChild, +} from '@angular/core'; +import {NgControl} from '@angular/forms'; +import {changeDetection} from '@demo/emulate/change-detection'; +import {encapsulation} from '@demo/emulate/encapsulation'; +import {AbstractTuiControl, TuiNativeFocusableElement} from '@taiga-ui/cdk'; +import { + TUI_PRIMITIVE_TEXTFIELD_DEFAULT_OPTIONS, + TUI_PRIMITIVE_TEXTFIELD_OPTIONS, + TuiPrimitiveTextfieldComponent, +} from '@taiga-ui/core'; + +@Component({ + selector: 'tui-primitive-textfield-example-2', + templateUrl: './index.html', + changeDetection, + encapsulation, + providers: [ + { + provide: TUI_PRIMITIVE_TEXTFIELD_OPTIONS, // <-- You are looking for this token + useValue: { + ...TUI_PRIMITIVE_TEXTFIELD_DEFAULT_OPTIONS, + iconCleaner: 'tuiIconChevronUp', + }, + }, + ], +}) +export class TuiPrimitiveTextfieldExample2 extends AbstractTuiControl { + @ViewChild(TuiPrimitiveTextfieldComponent) + private readonly textfield?: TuiPrimitiveTextfieldComponent; + + constructor( + @Optional() + @Self() + @Inject(NgControl) + control: NgControl | null, + @Inject(ChangeDetectorRef) changeDetectorRef: ChangeDetectorRef, + ) { + super(control, changeDetectorRef); + } + + get nativeFocusableElement(): TuiNativeFocusableElement | null { + return this.computedDisabled || !this.textfield + ? null + : this.textfield.nativeFocusableElement; + } + + get focused(): boolean { + return !!this.textfield && this.textfield.focused; + } + + onValueChange(textValue: string) { + this.updateValue(textValue); + } + + onFocused(focused: boolean) { + this.updateFocused(focused); + } + + protected getFallbackValue(): string { + return ''; + } +} diff --git a/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.component.ts b/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.component.ts index d1eb469db583..2a42e7bb3d06 100644 --- a/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.component.ts +++ b/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.component.ts @@ -1,5 +1,6 @@ import {Component, forwardRef, ViewChild} from '@angular/core'; import {changeDetection} from '@demo/emulate/change-detection'; +import {TuiDocExample} from '@taiga-ui/addon-doc'; import {TuiAutofillFieldName, TuiInputModeT, TuiInputTypeT} from '@taiga-ui/cdk'; import { TuiDirection, @@ -10,13 +11,6 @@ import { } from '@taiga-ui/core'; import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus'; -import {default as example1Ts} from '!!raw-loader!./examples/1/component.ts'; -import {default as example1Less} from '!!raw-loader!./examples/1/style.less'; -import {default as example1Html} from '!!raw-loader!./examples/1/template.html'; -import {default as exampleImportModule} from '!!raw-loader!./examples/import/import-module.txt'; -import {default as exampleInsertTemplate} from '!!raw-loader!./examples/import/insert-template.txt'; - -import {FrontEndExample} from '../../interfaces/front-end-example'; import {ABSTRACT_PROPS_ACCESSOR} from '../abstract/inherited-documentation/abstract-props-accessor'; import {AbstractExampleTuiInteractive} from '../abstract/interactive'; @@ -45,14 +39,24 @@ export class ExampleTuiPrimitiveTextfieldComponent extends AbstractExampleTuiInt @ViewChild('interactiveContent') private readonly interactiveIcon: PolymorpheusContent = ''; - readonly example1: FrontEndExample = { - TypeScript: example1Ts, - HTML: example1Html, - LESS: example1Less, + readonly example1: TuiDocExample = { + TypeScript: import('!!raw-loader!./examples/1/component.ts'), + HTML: import('!!raw-loader!./examples/1/template.html'), + LESS: import('!!raw-loader!./examples/1/style.less'), + }; + + readonly example2: TuiDocExample = { + TypeScript: import('!!raw-loader!./examples/2/index.ts'), + HTML: import('!!raw-loader!./examples/2/index.html'), }; - readonly exampleImportModule = exampleImportModule; - readonly exampleInsertTemplate = exampleInsertTemplate; + readonly exampleImportModule = import( + '!!raw-loader!./examples/import/import-module.txt' + ); + + readonly exampleInsertTemplate = import( + '!!raw-loader!./examples/import/insert-template.txt' + ); readonly themes = ['Taiga UI', 'Bootstrap', 'Material']; theme = this.themes[0]; @@ -118,6 +122,8 @@ export class ExampleTuiPrimitiveTextfieldComponent extends AbstractExampleTuiInt password = ''; + example2Value = 'mail@example.com'; + value = ''; exampleText = ''; diff --git a/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.module.ts b/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.module.ts index 643e2e63217a..87f4c90cc274 100644 --- a/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.module.ts +++ b/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.module.ts @@ -1,6 +1,6 @@ import {CommonModule} from '@angular/common'; import {NgModule} from '@angular/core'; -import {FormsModule} from '@angular/forms'; +import {FormsModule, ReactiveFormsModule} from '@angular/forms'; import {RouterModule} from '@angular/router'; import {generateRoutes, TuiAddonDocModule} from '@taiga-ui/addon-doc'; import { @@ -22,12 +22,14 @@ import {HintControllerDocumentationModule} from '../abstract/hint-controller-doc import {InheritedDocumentationModule} from '../abstract/inherited-documentation/inherited-documentation.module'; import {TextfieldControllerDocumentationModule} from '../abstract/textfield-controller-documentation/textfield-controller-documentation.module'; import {TuiPrimitiveTextfieldExample1} from './examples/1/component'; +import {TuiPrimitiveTextfieldExample2} from './examples/2'; import {ExampleTuiPrimitiveTextfieldComponent} from './primitive-textfield.component'; @NgModule({ imports: [ CommonModule, FormsModule, + ReactiveFormsModule, InheritedDocumentationModule, HintControllerDocumentationModule, TextfieldControllerDocumentationModule, @@ -47,7 +49,11 @@ import {ExampleTuiPrimitiveTextfieldComponent} from './primitive-textfield.compo TuiAddonDocModule, RouterModule.forChild(generateRoutes(ExampleTuiPrimitiveTextfieldComponent)), ], - declarations: [ExampleTuiPrimitiveTextfieldComponent, TuiPrimitiveTextfieldExample1], + declarations: [ + ExampleTuiPrimitiveTextfieldComponent, + TuiPrimitiveTextfieldExample1, + TuiPrimitiveTextfieldExample2, + ], exports: [ExampleTuiPrimitiveTextfieldComponent], }) export class ExampleTuiPrimitiveTextfieldModule {} diff --git a/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.template.html b/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.template.html index c22eb4b70464..e3e12f7cf348 100644 --- a/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.template.html +++ b/projects/demo/src/modules/components/primitive-textfield/primitive-textfield.template.html @@ -26,6 +26,17 @@ Type a password + + + +