Skip to content

Commit

Permalink
chore: micro host directive
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirpotekhin committed Oct 4, 2024
1 parent 78ad2e0 commit 54c6bbd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
10 changes: 3 additions & 7 deletions projects/core/components/textfield/textfield.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@ import type {TuiDataListHost} from '@taiga-ui/core/components/data-list';
import {tuiAsDataListHost} from '@taiga-ui/core/components/data-list';
import {TuiLabel} from '@taiga-ui/core/components/label';
import {
TUI_DROPDOWN_OPTIONS,
TuiDropdownDirective,
TuiDropdownFixed,
tuiDropdownOpen,
tuiDropdownOptionsProvider,
TuiWithDropdownOpen,
} from '@taiga-ui/core/directives/dropdown';
import {TuiWithIcons} from '@taiga-ui/core/directives/icons';
import {TUI_COMMON_ICONS} from '@taiga-ui/core/tokens';
import type {TuiSizeL, TuiSizeS} from '@taiga-ui/core/types';
import {tuiOverrideDefaultOptions} from '@taiga-ui/core/utils';
import type {PolymorpheusContent} from '@taiga-ui/polymorpheus';
import {PolymorpheusOutlet} from '@taiga-ui/polymorpheus';

Expand All @@ -50,8 +48,9 @@ import {TuiWithTextfieldDropdown} from './textfield-dropdown.directive';
styles: ['@import "@taiga-ui/core/styles/components/textfield.less";'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [tuiAsDataListHost(TuiTextfieldComponent), tuiDropdownOptionsProvider({})],
providers: [tuiAsDataListHost(TuiTextfieldComponent)],
hostDirectives: [
TuiDropdownFixed,
TuiDropdownDirective,
TuiWithDropdownOpen,
TuiWithTextfieldDropdown,
Expand Down Expand Up @@ -82,9 +81,6 @@ export class TuiTextfieldComponent<T> implements TuiDataListHost<T> {
protected readonly control?: NgControl;

protected readonly icons = inject(TUI_COMMON_ICONS);
protected readonly override = tuiOverrideDefaultOptions(TUI_DROPDOWN_OPTIONS, {
limitWidth: 'fixed',
});

protected computedFiller = computed(() => {
const value = this.directive?.nativeValue() || '';
Expand Down
21 changes: 21 additions & 0 deletions projects/core/directives/dropdown/dropdown-fixed.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {Directive, inject} from '@angular/core';
import {tuiOverrideOptions} from '@taiga-ui/core/utils/miscellaneous';

import {
TUI_DROPDOWN_DEFAULT_OPTIONS,
TUI_DROPDOWN_OPTIONS,
} from './dropdown-options.directive';

@Directive({
standalone: true,
})
export class TuiDropdownFixed {
constructor() {
const override = tuiOverrideOptions(
{limitWidth: 'fixed'},
TUI_DROPDOWN_DEFAULT_OPTIONS,
);

override(inject(TUI_DROPDOWN_OPTIONS), TUI_DROPDOWN_DEFAULT_OPTIONS);
}
}
1 change: 1 addition & 0 deletions projects/core/directives/dropdown/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export * from './dropdown-position-sided.directive';
export * from './dropdown-selection.directive';
export * from './dropdowns.component';
export * from './with-dropdown-open.directive';
export * from './dropdown-fixed.directive';
14 changes: 0 additions & 14 deletions projects/core/utils/miscellaneous/override-options.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import type {ProviderToken} from '@angular/core';
import {inject} from '@angular/core';

export function tuiOverrideOptions<T>(
override: Partial<T>,
fallback: T,
Expand All @@ -16,14 +13,3 @@ export function tuiOverrideOptions<T>(
return result;
};
}

export function tuiOverrideDefaultOptions<T>(
key: ProviderToken<T>,
options: Partial<T>,
): void {
const defaultOptions = inject(key, {self: true});

Object.keys(options).forEach((key) => {
(defaultOptions as Record<string, unknown>)[key] = options[key as keyof T];
});
}

0 comments on commit 54c6bbd

Please sign in to comment.