coreui-angular-ex
Version:
CoreUI Components Library for Angular
118 lines (101 loc) • 3.52 kB
text/typescript
import { booleanAttribute, Directive, ElementRef, HostBinding, Input, OnInit, Renderer2 } from '@angular/core';
import { Breakpoints, Colors } from '../coreui.types';
export class TableDirective implements OnInit {
constructor(
private renderer: Renderer2,
private hostElement: ElementRef
) { }
/**
* Set the vertical alignment.
* @type string
* @values 'bottom' | 'middle' | 'top'
*/
align?: 'bottom' | 'middle' | 'top';
/**
* Sets the border color of the component to one of CoreUI’s themed colors.
* @type Colors
*/
borderColor?: Colors;
/**
* Add borders on all sides of the table and cells.
* @type boolean
*/
bordered: string | boolean = false;
/**
* Remove borders on all sides of the table and cells.
* @type boolean
*/
borderless: string | boolean = false;
/**
* Put the `<caption>` on the top of the table.
* @values 'top'
*/
caption?: 'top';
/**
* Sets the color context of the component to one of CoreUI’s themed colors.
* @type Colors
*/
color?: Colors;
/**
* Enable a hover state on table rows within table body.
* @type boolean
*/
hover: string | boolean = false;
/**
* Make table responsive across all viewports or pick a maximum breakpoint with which to have a responsive table up to.
* @type: {boolean | 'sm' | 'md' | 'lg' | 'xl' | 'xxl'}
*/
responsive?: boolean | Omit<Breakpoints, 'xs'>;
/**
* Make table more compact by cutting all cell `padding` in half.
* @type boolean
*/
small: string | boolean = false;
/**
* Add zebra-striping to any table row within the table body.
* @type boolean
*/
striped: string | boolean = false;
/**
* Add zebra-striping to any table column.
* @type boolean
* @since 4.2.4
*/
stripedColumns: string | boolean = false;
get hostClasses(): any {
return {
table: true,
[`align-${this.align}`]: !!this.align,
[`caption-${this.caption}`]: !!this.caption,
[`border-${this.borderColor}`]: !!this.borderColor,
'table-bordered': this.bordered,
'table-borderless': this.borderless,
[`table-${this.color}`]: !!this.color,
'table-hover': this.hover,
'table-sm': this.small,
'table-striped': this.striped,
'table-striped-columns': this.stripedColumns
};
}
ngOnInit(): void {
this.setResponsiveWrapper();
}
// todo
setResponsiveWrapper(): void {
if (!!this.responsive) {
const nativeElement: HTMLElement = this.hostElement.nativeElement;
const wrapper = this.renderer.createElement('div');
const className = this.responsive === true ? 'table-responsive' : `table-responsive-${this.responsive}`;
this.renderer.addClass(wrapper, className);
const parentNode = this.renderer.parentNode(nativeElement);
this.renderer.appendChild(parentNode, wrapper);
this.renderer.insertBefore(parentNode, wrapper, nativeElement);
this.renderer.appendChild(wrapper, nativeElement);
}
}
}