Skip to content

Commit

Permalink
feat(toggle): allow configuration of default options
Browse files Browse the repository at this point in the history
  • Loading branch information
Dima Karimov committed Aug 20, 2021
1 parent b5af611 commit 52eec97
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 35 deletions.
13 changes: 13 additions & 0 deletions projects/demo/src/modules/components/toggle/examples/2/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<form [formGroup]="testForm">
<div>
<tui-toggle formControlName="testValue1" size="m" [showIcons]="true">
</tui-toggle>
<tui-toggle
class="tui-space_left-1"
formControlName="testValue2"
size="l"
[showIcons]="true"
>
</tui-toggle>
</div>
</form>
40 changes: 40 additions & 0 deletions projects/demo/src/modules/components/toggle/examples/2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {Component} from '@angular/core';
import {FormControl, FormGroup} from '@angular/forms';
import {
ToggleOptions,
TUI_TOGGLE_DEFAULT_OPTIONS,
TUI_TOGGLE_OPTIONS,
} from '@taiga-ui/kit';
import {changeDetection} from '../../../../../change-detection-strategy';
import {encapsulation} from '../../../../../view-encapsulation';

const options: ToggleOptions = {
icons: {
toggleOff: ({$implicit}) =>
$implicit === 'm' ? 'tuiIconChevronRight' : 'tuiIconChevronRightLarge',
toggleOn: ({$implicit}) =>
$implicit === 'm' ? 'tuiIconChevronLeft' : 'tuiIconChevronLeftLarge',
},
};

@Component({
selector: 'tui-toggle-example-2',
templateUrl: './index.html',
changeDetection,
encapsulation,
providers: [
{
provide: TUI_TOGGLE_OPTIONS,
useValue: {
...TUI_TOGGLE_DEFAULT_OPTIONS,
...options,
},
},
],
})
export class TuiToggleExample2 {
testForm = new FormGroup({
testValue1: new FormControl(true),
testValue2: new FormControl(false),
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
ToggleOptions,
TUI_TOGGLE_DEFAULT_OPTIONS,
TUI_TOGGLE_OPTIONS,
} from '@taiga-ui/kit';

...
const options: ToggleOptions = {
icons: {
toggleOff: ({$implicit}) =>
$implicit === 'm' ? 'tuiIconChevronRight' : 'tuiIconChevronRightLarge',
toggleOn: ({$implicit}) =>
$implicit === 'm' ? 'tuiIconChevronLeft' : 'tuiIconChevronLeftLarge',
},
};

@NgModule({
providers: [{
provide: TUI_TOGGLE_OPTIONS,
useValue: {
...TUI_TOGGLE_DEFAULT_OPTIONS,
...options,
}
}],
...
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {default as example1Html} from '!!raw-loader!./examples/1/index.html';
import {default as example1Ts} from '!!raw-loader!./examples/1/index.ts';
import {default as example2Html} from '!!raw-loader!./examples/2/index.html';
import {default as example2Ts} from '!!raw-loader!./examples/2/index.ts';

import {default as exampleDeclareForm} from '!!raw-loader!./examples/import/declare-form.txt';
import {default as exampleDefineOptions} from '!!raw-loader!./examples/import/define-options.txt';
import {default as exampleImportModule} from '!!raw-loader!./examples/import/import-module.txt';
import {default as exampleInsertTemplate} from '!!raw-loader!./examples/import/insert-template.txt';

Expand All @@ -28,12 +31,18 @@ export class ExampleTuiToggleComponent extends AbstractExampleTuiInteractive {
readonly exampleDeclareForm = exampleDeclareForm;
readonly exampleImportModule = exampleImportModule;
readonly exampleInsertTemplate = exampleInsertTemplate;
readonly exampleDefineOptions = exampleDefineOptions;

readonly example1: FrontEndExample = {
TypeScript: example1Ts,
HTML: example1Html,
};

readonly example2: FrontEndExample = {
TypeScript: example2Ts,
HTML: example2Html,
};

showIcons = false;

showLoader = false;
Expand Down
3 changes: 2 additions & 1 deletion projects/demo/src/modules/components/toggle/toggle.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {generateRoutes, TuiAddonDocModule} from '@taiga-ui/addon-doc';
import {TuiToggleModule} from '@taiga-ui/kit';
import {InheritedDocumentationModule} from '../abstract/inherited-documentation/inherited-documentation.module';
import {TuiToggleExample1} from './examples/1';
import {TuiToggleExample2} from './examples/2';
import {ExampleTuiToggleComponent} from './toggle.component';

@NgModule({
Expand All @@ -18,7 +19,7 @@ import {ExampleTuiToggleComponent} from './toggle.component';
CommonModule,
RouterModule.forChild(generateRoutes(ExampleTuiToggleComponent)),
],
declarations: [ExampleTuiToggleComponent, TuiToggleExample1],
declarations: [ExampleTuiToggleComponent, TuiToggleExample1, TuiToggleExample2],
exports: [ExampleTuiToggleComponent],
})
export class ExampleTuiToggleModule {}
22 changes: 22 additions & 0 deletions projects/demo/src/modules/components/toggle/toggle.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
>
<tui-toggle-example-1></tui-toggle-example-1>
</tui-doc-example>

<tui-doc-example
id="options"
i18n-heading
heading="options"
[content]="example2"
>
<tui-toggle-example-2></tui-toggle-example-2>
</tui-doc-example>
</ng-template>

<ng-template pageTab>
Expand Down Expand Up @@ -112,6 +121,19 @@
[code]="exampleInsertTemplate"
></tui-doc-code>
</li>

<li>
<p i18n>
Optionally use the
<code>TUI_TOGGLE_OPTIONS</code> injection token to override
the default options for the component.
</p>

<tui-doc-code
filename="myComponent.module.ts"
[code]="exampleDefineOptions"
></tui-doc-code>
</li>
</ol>
</ng-template>
</tui-doc-page>
1 change: 1 addition & 0 deletions projects/kit/components/toggle/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './toggle.component';
export * from './toggle.module';
export * from './toggle-options';
28 changes: 28 additions & 0 deletions projects/kit/components/toggle/toggle-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {InjectionToken} from '@angular/core';
import {TuiContextWithImplicit} from '@taiga-ui/cdk';
import {TuiSizeL} from '@taiga-ui/core';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';

export interface ToggleOptions {
readonly icons: Readonly<{
toggleOff: PolymorpheusContent<TuiContextWithImplicit<TuiSizeL>>;
toggleOn: PolymorpheusContent<TuiContextWithImplicit<TuiSizeL>>;
}>;
}

/** Default values for the toggle options. */
export const TUI_TOGGLE_DEFAULT_OPTIONS: ToggleOptions = {
icons: {
toggleOff: ({$implicit}) =>
$implicit === 'm' ? 'tuiIconToggleOff' : 'tuiIconToggleOffLarge',
toggleOn: ({$implicit}) =>
$implicit === 'm' ? 'tuiIconToggleOn' : 'tuiIconToggleOnLarge',
},
};

export const TUI_TOGGLE_OPTIONS = new InjectionToken<ToggleOptions>(
'Default parameters for toggle component',
{
factory: () => TUI_TOGGLE_DEFAULT_OPTIONS,
},
);
31 changes: 23 additions & 8 deletions projects/kit/components/toggle/toggle.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ import {
AbstractTuiControl,
isNativeFocused,
TUI_FOCUSABLE_ITEM_ACCESSOR,
TuiContextWithImplicit,
tuiDefaultProp,
TuiFocusableElementAccessor,
TuiNativeFocusableElement,
tuiPure,
} from '@taiga-ui/cdk';
import {
TuiAppearance,
Expand All @@ -27,6 +29,8 @@ import {
TuiSizeL,
TuiSizeXS,
} from '@taiga-ui/core';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';
import {ToggleOptions, TUI_TOGGLE_OPTIONS} from './toggle-options';

@Component({
selector: 'tui-toggle',
Expand Down Expand Up @@ -72,10 +76,29 @@ export class TuiToggleComponent
@Optional()
@Inject(TuiModeDirective)
private readonly modeDirective: TuiModeDirective | null,
@Inject(TUI_TOGGLE_OPTIONS)
public readonly options: ToggleOptions,
) {
super(control, changeDetectorRef);
}

get iconOn(): PolymorpheusContent<TuiContextWithImplicit<TuiSizeL>> {
return this.options.icons.toggleOn;
}

get iconOff(): PolymorpheusContent<TuiContextWithImplicit<TuiSizeL>> {
return this.options.icons.toggleOff;
}

get context(): TuiContextWithImplicit<TuiSizeL> {
return this.getContext(this.size);
}

@tuiPure
private getContext($implicit: TuiSizeL): TuiContextWithImplicit<TuiSizeL> {
return {$implicit};
}

get nativeFocusableElement(): TuiNativeFocusableElement | null {
return this.focusableElement ? this.focusableElement.nativeElement : null;
}
Expand All @@ -99,14 +122,6 @@ export class TuiToggleComponent
return this.value;
}

get iconOn(): string {
return this.sizeM ? 'tuiIconToggleOn' : 'tuiIconToggleOnLarge';
}

get iconOff(): string {
return this.sizeM ? 'tuiIconToggleOff' : 'tuiIconToggleOffLarge';
}

get loaderSize(): TuiSizeXS {
return this.sizeM ? 'xs' : 's';
}
Expand Down
2 changes: 2 additions & 0 deletions projects/kit/components/toggle/toggle.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
TuiPressedModule,
} from '@taiga-ui/cdk';
import {TuiLoaderModule, TuiSvgModule, TuiWrapperModule} from '@taiga-ui/core';
import {PolymorpheusModule} from '@tinkoff/ng-polymorpheus';
import {TuiToggleComponent} from './toggle.component';

@NgModule({
Expand All @@ -23,6 +24,7 @@ import {TuiToggleComponent} from './toggle.component';
TuiWrapperModule,
TuiSvgModule,
TuiLoaderModule,
PolymorpheusModule,
],
declarations: [TuiToggleComponent],
exports: [TuiToggleComponent],
Expand Down
4 changes: 4 additions & 0 deletions projects/kit/components/toggle/toggle.style.less
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@
}
}

.icon-wrapper {
display: flex;
}

.icon {
opacity: 0.8;

Expand Down
71 changes: 45 additions & 26 deletions projects/kit/components/toggle/toggle.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,52 @@
[invalid]="computedInvalid"
>
<div class="toggle">
<tui-loader
*ngIf="showLoader"
class="loader"
[size]="loaderSize"
[inheritColor]="true"
[showLoader]="checked"
></tui-loader>
<tui-svg
*ngIf="showIcons && !showLoader"
class="icon"
automation-id="tui-toggle__check-icon"
[src]="iconOn"
></tui-svg>
<span
polymorpheus-outlet
class="icon-wrapper"
[content]="iconOn"
[context]="context"
>
<ng-template let-icon>
<tui-loader
*ngIf="showLoader"
class="loader"
[size]="loaderSize"
[inheritColor]="true"
[showLoader]="checked"
></tui-loader>

<tui-svg
*ngIf="showIcons && !showLoader"
class="icon"
automation-id="tui-toggle__check-icon"
[src]="icon"
></tui-svg>
</ng-template>
</span>
<div class="circle"></div>
<tui-loader
*ngIf="showLoader"
class="loader"
[size]="loaderSize"
[inheritColor]="true"
[showLoader]="!checked"
></tui-loader>
<tui-svg
*ngIf="showIcons && !showLoader"
class="icon icon_off"
automation-id="tui-toggle__cancel-icon"
[src]="iconOff"
></tui-svg>
<span
polymorpheus-outlet
class="icon-wrapper"
[content]="iconOff"
[context]="context"
>
<ng-template let-icon>
<tui-loader
*ngIf="showLoader"
class="loader"
[size]="loaderSize"
[inheritColor]="true"
[showLoader]="!checked"
></tui-loader>
<tui-svg
*ngIf="showIcons && !showLoader"
class="icon icon_off"
automation-id="tui-toggle__cancel-icon"
[src]="icon"
></tui-svg>
</ng-template>
</span>
</div>
</tui-wrapper>
<input
Expand Down

0 comments on commit 52eec97

Please sign in to comment.