1- import { Directive , HostBinding , Input } from '@angular/core' ;
2- import { BooleanInput , coerceBooleanProperty , coerceNumberProperty , NumberInput } from '@angular/cdk/coercion' ;
3-
4- import { ColOrder , ICol } from './col.type' ;
1+ import { booleanAttribute , computed , Directive , input , numberAttribute } from '@angular/core' ;
2+ import { BooleanInput , NumberInput } from '@angular/cdk/coercion' ;
53import { BreakpointInfix } from '../coreui.types' ;
4+ import { ColOrder } from './col.type' ;
5+
6+ export type ColOffsetType = number | { xs ?: number ; sm ?: number ; md ?: number ; lg ?: number ; xl ?: number ; xxl ?: number } ;
7+ export type ColOrderType =
8+ | ColOrder
9+ | { xs ?: ColOrder ; sm ?: ColOrder ; md ?: ColOrder ; lg ?: ColOrder ; xl ?: ColOrder ; xxl ?: ColOrder } ;
610
711@Directive ( {
8- selector : '[cCol]'
12+ selector : '[cCol]' ,
13+ host : {
14+ '[class]' : 'hostClasses()'
15+ }
916} )
10- export class ColDirective implements ICol {
17+ export class ColDirective {
1118 static ngAcceptInputType_cCol : BooleanInput | NumberInput ;
1219 static ngAcceptInputType_xs : BooleanInput | NumberInput ;
1320 static ngAcceptInputType_sm : BooleanInput | NumberInput ;
@@ -18,142 +25,111 @@ export class ColDirective implements ICol {
1825
1926 /**
2027 * The number of columns/offset/order on extra small devices (<576px).
21- * @type { 'auto' | number | boolean }
28+ * @return { 'auto' | number | boolean }
2229 */
23- @Input ( )
24- set cCol ( value : BooleanInput | NumberInput ) {
25- this . xs = this . xs || this . coerceInput ( value ) ;
26- }
27- @Input ( )
28- set xs ( value ) {
29- this . _xs = this . coerceInput ( value ) ;
30- }
31- get xs ( ) : BooleanInput | NumberInput {
32- return this . _xs ;
33- }
34- private _xs : BooleanInput | NumberInput = false ;
30+ readonly cCol = input ( false , { transform : this . coerceInput } ) ;
31+ readonly xs = input ( false , { transform : this . coerceInput } ) ;
3532
3633 /**
3734 * The number of columns/offset/order on small devices (<768px).
38- * @type { 'auto' | number | boolean }
35+ * @return { 'auto' | number | boolean }
3936 */
40- @Input ( )
41- set sm ( value ) {
42- this . _sm = this . coerceInput ( value ) ;
43- }
44- get sm ( ) : BooleanInput | NumberInput {
45- return this . _sm ;
46- }
47- private _sm : BooleanInput | NumberInput = false ;
37+ readonly sm = input ( false , { transform : this . coerceInput } ) ;
4838
4939 /**
5040 * The number of columns/offset/order on medium devices (<992px).
51- * @type { 'auto' | number | boolean }
41+ * @return { 'auto' | number | boolean }
5242 */
53- @Input ( )
54- set md ( value ) {
55- this . _md = this . coerceInput ( value ) ;
56- }
57- get md ( ) : BooleanInput | NumberInput {
58- return this . _md ;
59- }
60- private _md : BooleanInput | NumberInput = false ;
43+ readonly md = input ( false , { transform : this . coerceInput } ) ;
6144
6245 /**
6346 * The number of columns/offset/order on large devices (<1200px).
64- * @type { 'auto' | number | boolean }
47+ * @return { 'auto' | number | boolean }
6548 */
66- @Input ( )
67- set lg ( value ) {
68- this . _lg = this . coerceInput ( value ) ;
69- }
70- get lg ( ) : BooleanInput | NumberInput {
71- return this . _lg ;
72- }
73- private _lg : BooleanInput | NumberInput = false ;
49+ readonly lg = input ( false , { transform : this . coerceInput } ) ;
7450
7551 /**
7652 * The number of columns/offset/order on X-Large devices (<1400px).
77- * @type { 'auto' | number | boolean }
53+ * @return { 'auto' | number | boolean }
7854 */
79- @Input ( )
80- set xl ( value ) {
81- this . _xl = this . coerceInput ( value ) ;
82- }
83- get xl ( ) : BooleanInput | NumberInput {
84- return this . _xl ;
85- }
86- private _xl : BooleanInput | NumberInput = false ;
55+ readonly xl = input ( false , { transform : this . coerceInput } ) ;
8756
8857 /**
8958 * The number of columns/offset/order on XX-Large devices (≥1400px).
90- * @type { 'auto' | number | boolean }
59+ * @return { 'auto' | number | boolean }
9160 */
92- @Input ( )
93- set xxl ( value ) {
94- this . _xxl = this . coerceInput ( value ) ;
95- }
96- get xxl ( ) : BooleanInput | NumberInput {
97- return this . _xxl ;
98- }
99- private _xxl : BooleanInput | NumberInput = false ;
61+ readonly xxl = input ( false , { transform : this . coerceInput } ) ;
62+
63+ readonly breakpoints = computed ( ( ) => {
64+ return {
65+ xs : this . xs ( ) || this . cCol ( ) ,
66+ sm : this . sm ( ) ,
67+ md : this . md ( ) ,
68+ lg : this . lg ( ) ,
69+ xl : this . xl ( ) ,
70+ xxl : this . xxl ( )
71+ } as Record < string , any > ;
72+ } ) ;
10073
101- @Input ( ) offset ?: number | { xs ?: number ; sm ?: number ; md ?: number ; lg ?: number ; xl ?: number ; xxl ?: number } ;
102- @Input ( ) order ?:
103- | ColOrder
104- | { xs ?: ColOrder ; sm ?: ColOrder ; md ?: ColOrder ; lg ?: ColOrder ; xl ?: ColOrder ; xxl ?: ColOrder } ;
74+ readonly offset = input < ColOffsetType > ( ) ;
75+ readonly order = input < ColOrderType > ( ) ;
10576
106- @HostBinding ( 'class' )
107- get hostClasses ( ) : any {
108- const classes : any = {
77+ readonly hostClasses = computed ( ( ) => {
78+ const classes : Record < string , boolean > = {
10979 col : true
11080 } ;
11181
82+ const breakpoints = this . breakpoints ( ) ;
83+ const offsetInput = this . offset ( ) ;
84+ const orderInput = this . order ( ) ;
85+
11286 Object . keys ( BreakpointInfix ) . forEach ( ( breakpoint ) => {
113- // @ts -ignore
114- const value : number | string | boolean = this [ breakpoint ] ;
87+ const value = breakpoints [ breakpoint ] ;
11588 const infix = breakpoint === 'xs' ? '' : `-${ breakpoint } ` ;
11689 classes [ `col${ infix } ` ] = value === true ;
11790 classes [ `col${ infix } -${ value } ` ] = typeof value === 'number' || typeof value === 'string' ;
11891 } ) ;
11992
120- if ( typeof this . offset === 'object' ) {
121- const offset = { ...this . offset } ;
93+ if ( typeof offsetInput === 'object' ) {
94+ const offset = { ...offsetInput } ;
12295 Object . entries ( offset ) . forEach ( ( entry ) => {
12396 const [ breakpoint , value ] = [ ...entry ] ;
12497 const infix = breakpoint === 'xs' ? '' : `-${ breakpoint } ` ;
12598 classes [ `offset${ infix } -${ value } ` ] = value >= 0 && value <= 11 ;
12699 } ) ;
127100 } else {
128- classes [ `offset-${ this . offset } ` ] = typeof this . offset === 'number' && this . offset > 0 && this . offset <= 11 ;
101+ const offset = numberAttribute ( offsetInput ) ;
102+ classes [ `offset-${ offset } ` ] = typeof offset === 'number' && offset > 0 && offset <= 11 ;
129103 }
130104
131- if ( typeof this . order === 'object' ) {
132- const order = { ...this . order } ;
105+ if ( typeof orderInput === 'object' ) {
106+ const order = { ...orderInput } ;
133107 Object . entries ( order ) . forEach ( ( entry ) => {
134108 const [ breakpoint , value ] = [ ...entry ] ;
135109 const infix = breakpoint === 'xs' ? '' : `-${ breakpoint } ` ;
136- classes [ `order${ infix } -${ value } ` ] = value ;
110+ classes [ `order${ infix } -${ value } ` ] = ! ! value ;
137111 } ) ;
138112 } else {
139- classes [ `order-${ this . order } ` ] = ! ! this . order ;
113+ const order = orderInput ;
114+ classes [ `order-${ order } ` ] = ! ! order ;
140115 }
141116
142117 // if there is no 'col' class, add one
143- classes . col = ! Object . entries ( classes ) . filter ( ( i ) => i [ 0 ] . startsWith ( 'col-' ) && i [ 1 ] ) . length || this . xs === true ;
144- return classes ;
145- }
118+ classes [ 'col' ] =
119+ ! Object . entries ( classes ) . filter ( ( i ) => i [ 0 ] . startsWith ( 'col-' ) && i [ 1 ] ) . length || breakpoints [ 'xs' ] === true ;
120+ return classes as Record < string , boolean > ;
121+ } ) ;
146122
147123 coerceInput ( value : BooleanInput | NumberInput ) {
148124 if ( value === 'auto' ) {
149125 return value ;
150126 }
151127 if ( value === '' || value === undefined || value === null ) {
152- return coerceBooleanProperty ( value ) ;
128+ return booleanAttribute ( value ) ;
153129 }
154130 if ( typeof value === 'boolean' ) {
155131 return value ;
156132 }
157- return coerceNumberProperty ( value ) ;
133+ return numberAttribute ( value ) ;
158134 }
159135}
0 commit comments