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' ;
5
3
import { 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 } ;
6
10
7
11
@Directive ( {
8
- selector : '[cCol]'
12
+ selector : '[cCol]' ,
13
+ host : {
14
+ '[class]' : 'hostClasses()'
15
+ }
9
16
} )
10
- export class ColDirective implements ICol {
17
+ export class ColDirective {
11
18
static ngAcceptInputType_cCol : BooleanInput | NumberInput ;
12
19
static ngAcceptInputType_xs : BooleanInput | NumberInput ;
13
20
static ngAcceptInputType_sm : BooleanInput | NumberInput ;
@@ -18,142 +25,111 @@ export class ColDirective implements ICol {
18
25
19
26
/**
20
27
* The number of columns/offset/order on extra small devices (<576px).
21
- * @type { 'auto' | number | boolean }
28
+ * @return { 'auto' | number | boolean }
22
29
*/
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 } ) ;
35
32
36
33
/**
37
34
* The number of columns/offset/order on small devices (<768px).
38
- * @type { 'auto' | number | boolean }
35
+ * @return { 'auto' | number | boolean }
39
36
*/
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 } ) ;
48
38
49
39
/**
50
40
* The number of columns/offset/order on medium devices (<992px).
51
- * @type { 'auto' | number | boolean }
41
+ * @return { 'auto' | number | boolean }
52
42
*/
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 } ) ;
61
44
62
45
/**
63
46
* The number of columns/offset/order on large devices (<1200px).
64
- * @type { 'auto' | number | boolean }
47
+ * @return { 'auto' | number | boolean }
65
48
*/
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 } ) ;
74
50
75
51
/**
76
52
* The number of columns/offset/order on X-Large devices (<1400px).
77
- * @type { 'auto' | number | boolean }
53
+ * @return { 'auto' | number | boolean }
78
54
*/
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 } ) ;
87
56
88
57
/**
89
58
* The number of columns/offset/order on XX-Large devices (≥1400px).
90
- * @type { 'auto' | number | boolean }
59
+ * @return { 'auto' | number | boolean }
91
60
*/
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
+ } ) ;
100
73
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 > ( ) ;
105
76
106
- @HostBinding ( 'class' )
107
- get hostClasses ( ) : any {
108
- const classes : any = {
77
+ readonly hostClasses = computed ( ( ) => {
78
+ const classes : Record < string , boolean > = {
109
79
col : true
110
80
} ;
111
81
82
+ const breakpoints = this . breakpoints ( ) ;
83
+ const offsetInput = this . offset ( ) ;
84
+ const orderInput = this . order ( ) ;
85
+
112
86
Object . keys ( BreakpointInfix ) . forEach ( ( breakpoint ) => {
113
- // @ts -ignore
114
- const value : number | string | boolean = this [ breakpoint ] ;
87
+ const value = breakpoints [ breakpoint ] ;
115
88
const infix = breakpoint === 'xs' ? '' : `-${ breakpoint } ` ;
116
89
classes [ `col${ infix } ` ] = value === true ;
117
90
classes [ `col${ infix } -${ value } ` ] = typeof value === 'number' || typeof value === 'string' ;
118
91
} ) ;
119
92
120
- if ( typeof this . offset === 'object' ) {
121
- const offset = { ...this . offset } ;
93
+ if ( typeof offsetInput === 'object' ) {
94
+ const offset = { ...offsetInput } ;
122
95
Object . entries ( offset ) . forEach ( ( entry ) => {
123
96
const [ breakpoint , value ] = [ ...entry ] ;
124
97
const infix = breakpoint === 'xs' ? '' : `-${ breakpoint } ` ;
125
98
classes [ `offset${ infix } -${ value } ` ] = value >= 0 && value <= 11 ;
126
99
} ) ;
127
100
} 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 ;
129
103
}
130
104
131
- if ( typeof this . order === 'object' ) {
132
- const order = { ...this . order } ;
105
+ if ( typeof orderInput === 'object' ) {
106
+ const order = { ...orderInput } ;
133
107
Object . entries ( order ) . forEach ( ( entry ) => {
134
108
const [ breakpoint , value ] = [ ...entry ] ;
135
109
const infix = breakpoint === 'xs' ? '' : `-${ breakpoint } ` ;
136
- classes [ `order${ infix } -${ value } ` ] = value ;
110
+ classes [ `order${ infix } -${ value } ` ] = ! ! value ;
137
111
} ) ;
138
112
} else {
139
- classes [ `order-${ this . order } ` ] = ! ! this . order ;
113
+ const order = orderInput ;
114
+ classes [ `order-${ order } ` ] = ! ! order ;
140
115
}
141
116
142
117
// 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
+ } ) ;
146
122
147
123
coerceInput ( value : BooleanInput | NumberInput ) {
148
124
if ( value === 'auto' ) {
149
125
return value ;
150
126
}
151
127
if ( value === '' || value === undefined || value === null ) {
152
- return coerceBooleanProperty ( value ) ;
128
+ return booleanAttribute ( value ) ;
153
129
}
154
130
if ( typeof value === 'boolean' ) {
155
131
return value ;
156
132
}
157
- return coerceNumberProperty ( value ) ;
133
+ return numberAttribute ( value ) ;
158
134
}
159
135
}
0 commit comments