Skip to content
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

feat(addon-table): add token TUI_TABLE_PAGINATION_OPTIONS with property sizeOptionContent #1787

Merged
merged 2 commits into from
May 20, 2022
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
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './table-pagination.component';
export * from './table-pagination.module';
export * from './table-pagination-options';
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {InjectionToken, ValueProvider} from '@angular/core';
import {TuiContextWithImplicit} from '@taiga-ui/cdk';
import {PolymorpheusContent} from '@tinkoff/ng-polymorpheus';

export interface TuiTablePaginationOptions {
readonly sizeOptionContent: PolymorpheusContent<
TuiContextWithImplicit<number> & {total: number}
>;
}

function defaultSizeOptionContent({$implicit}: TuiContextWithImplicit<number>): string {
return `${$implicit}`;
}

export const TUI_TABLE_PAGINATION_DEFAULT_OPTIONS: TuiTablePaginationOptions = {
sizeOptionContent: defaultSizeOptionContent,
};

export const TUI_TABLE_PAGINATION_OPTIONS = new InjectionToken(
'Default parameters for TablePagination component',
{factory: () => TUI_TABLE_PAGINATION_DEFAULT_OPTIONS},
);

export function tuiTablePaginationOptionsProvider(
options: Partial<TuiTablePaginationOptions>,
): ValueProvider {
return {
provide: TUI_TABLE_PAGINATION_OPTIONS,
useValue: {...TUI_TABLE_PAGINATION_DEFAULT_OPTIONS, ...options},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import {tuiDefaultProp} from '@taiga-ui/cdk';
import {TUI_SPIN_TEXTS} from '@taiga-ui/core';
import {Observable} from 'rxjs';

import {
TUI_TABLE_PAGINATION_OPTIONS,
TuiTablePaginationOptions,
} from './table-pagination-options';

// @dynamic
@Component({
selector: 'tui-table-pagination',
Expand Down Expand Up @@ -39,6 +44,7 @@ export class TuiTablePaginationComponent {
@Inject(TUI_SPIN_TEXTS) readonly spinTexts$: Observable<[string, string]>,
@Inject(TUI_TABLE_PAGINATION_TEXTS)
readonly texts$: Observable<Record<'pages' | 'linesPerPage' | 'of', string>>,
@Inject(TUI_TABLE_PAGINATION_OPTIONS) readonly options: TuiTablePaginationOptions,
) {}

get pages(): number {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TuiLinkModule,
TuiSvgModule,
} from '@taiga-ui/core';
import {PolymorpheusModule} from '@tinkoff/ng-polymorpheus';

import {TuiTablePaginationComponent} from './table-pagination.component';

Expand All @@ -18,6 +19,7 @@ import {TuiTablePaginationComponent} from './table-pagination.component';
TuiHostedDropdownModule,
TuiDataListModule,
TuiSvgModule,
PolymorpheusModule,
],
declarations: [TuiTablePaginationComponent],
exports: [TuiTablePaginationComponent],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
class="t-item"
(click)="onItem(item)"
>
{{ item }}
<span
polymorpheus-outlet
[content]="options.sizeOptionContent"
[context]="{$implicit: item, total: total}"
></span>
<tui-svg
*ngIf="item === size; else fakeIcon"
src="tuiIconCheckLarge"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ describe('TablePagination', () => {
);
});
});

it('Custom size-option content', () => {
cy.tuiVisit('/components/table-pagination');

initializeAliases('#custom-size-option-content');

cy.get('@linesPerPageSelect').click();
waitForCheckmarkIcon();
cy.get('tui-dropdown-box').matchImageSnapshot('2-[sizeOptionContent]-dropdown');
});
});

function initializeAliases(selectorToTablePagination: string): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<tui-table-pagination
[total]="total"
[items]="sizeOptions"
></tui-table-pagination>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {Component} from '@angular/core';
import {changeDetection} from '@demo/emulate/change-detection';
import {encapsulation} from '@demo/emulate/encapsulation';
import {
TuiTablePaginationOptions,
tuiTablePaginationOptionsProvider,
} from '@taiga-ui/addon-table';

const customOptionContent: TuiTablePaginationOptions['sizeOptionContent'] = ({
$implicit,
total,
}) => {
switch ($implicit) {
case 10:
return 'Ten';
case total:
return 'Show all rows';
default:
return $implicit;
}
};

@Component({
selector: 'tui-table-pagination-example-2',
templateUrl: './index.html',
providers: [
tuiTablePaginationOptionsProvider({sizeOptionContent: customOptionContent}),
],
changeDetection,
encapsulation,
})
export class TuiTablePaginationExample2 {
total = 350;
sizeOptions = [10, 50, 100, this.total];
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export class ExampleTuiTablePaginationComponent {
TypeScript: import('!!raw-loader!./examples/1/index.ts'),
};

readonly example2: TuiDocExample = {
TypeScript: import('!!raw-loader!./examples/2/index.ts'),
HTML: import('!!raw-loader!./examples/2/index.html'),
};

readonly exampleModule = import('!!raw-loader!./examples/import/import-module.md');
readonly exampleHtml = import('!!raw-loader!./examples/import/insert-template.md');
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {generateRoutes, TuiAddonDocModule} from '@taiga-ui/addon-doc';
import {TuiTablePaginationModule} from '@taiga-ui/addon-table';

import {TuiTablePaginationExample1} from './examples/1';
import {TuiTablePaginationExample2} from './examples/2';
import {ExampleTuiTablePaginationComponent} from './table-pagination.component';

@NgModule({
Expand All @@ -14,7 +15,11 @@ import {ExampleTuiTablePaginationComponent} from './table-pagination.component';
TuiAddonDocModule,
RouterModule.forChild(generateRoutes(ExampleTuiTablePaginationComponent)),
],
declarations: [ExampleTuiTablePaginationComponent, TuiTablePaginationExample1],
declarations: [
ExampleTuiTablePaginationComponent,
TuiTablePaginationExample1,
TuiTablePaginationExample2,
],
exports: [ExampleTuiTablePaginationComponent],
})
export class ExampleTuiTablePaginationModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@
>
<tui-table-pagination-example-1></tui-table-pagination-example-1>
</tui-doc-example>

<tui-doc-example
id="custom-size-option-content"
i18n-heading
heading="Custom size-option content"
[description]="tokenDescription"
[content]="example2"
>
<ng-template #tokenDescription>
You can customize the component via DI-token
<code>TUI_TABLE_PAGINATION_OPTIONS</code>
.

<p class="tui-space_bottom-0">
Use function
<code>tuiTablePaginationOptionsProvider</code>
to provide new value of this token.
</p>
</ng-template>
<tui-table-pagination-example-2></tui-table-pagination-example-2>
</tui-doc-example>
</ng-template>

<ng-template pageTab>
Expand Down