Skip to content

Commit 8598fa4

Browse files
committed
feat(flags): signals
1 parent 5a0c579 commit 8598fa4

File tree

1 file changed

+39
-45
lines changed

1 file changed

+39
-45
lines changed
Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, HostBinding, Input, ViewEncapsulation } from '@angular/core'
1+
import { Component, computed, input, ViewEncapsulation } from '@angular/core'
22
import { FlagDatabase, FlagDatabaseKey } from '../database'
33
import { FlagFormat, FlagFormatEnum, FlagSize, FlagSizeAlias } from '../types'
44

@@ -9,76 +9,70 @@ const availableCodes = new Set(Object.values(FlagDatabase))
99
selector: 'nxt-flag',
1010
templateUrl: './flag.component.html',
1111
styleUrls: ['./flag.component.scss'],
12-
encapsulation: ViewEncapsulation.Emulated
12+
encapsulation: ViewEncapsulation.Emulated,
13+
host: {
14+
'[style.width.px]': '_size()',
15+
'[style.height.px]': '_height()',
16+
'[style.borderRadius]': '_radius()',
17+
'[style.backgroundImage]': '_image()',
18+
'[style.display]': '_display()'
19+
}
1320
})
1421
export class FlagComponent {
1522

16-
private _code?: string
1723
/** ISO 3166-1-alpha-2 country code */
18-
@Input() set country(val: FlagDatabaseKey) {
24+
readonly country = input<FlagDatabaseKey>()
25+
26+
private readonly _code = computed(() => {
27+
const val = this.country()
1928
const lc: FlagDatabaseKey | undefined = (val && val.toLowerCase() as any) || undefined
2029
if (lc && lc in FlagDatabase) {
21-
this._code = FlagDatabase[lc]
30+
return FlagDatabase[lc]
2231
} else if (lc && availableCodes.has(lc)) {
23-
this._code = lc
32+
return lc
2433
} else {
25-
this._code = undefined
34+
return undefined
2635
}
27-
}
36+
})
2837

29-
private _format: FlagFormat = FlagFormatEnum.None
3038
/** Flag format */
31-
@Input() set format(val: FlagFormat) {
32-
this._format = availableFormats.has(val)
39+
readonly format = input<FlagFormat>(FlagFormatEnum.None)
40+
41+
private readonly _format = computed(() => {
42+
const val = this.format()
43+
return availableFormats.has(val)
3344
? val
3445
: FlagFormatEnum.None
35-
}
46+
})
3647

37-
private _size = 48
3848
/** Flag width, either value in pixels or FlagSizeAlias */
39-
@Input() set size(val: number | FlagSizeAlias) {
49+
readonly size = input<number | FlagSizeAlias>(48)
50+
51+
/** @internal */
52+
readonly _size = computed(() => {
53+
const val = this.size()
4054
if (typeof val == 'string' && val.toLowerCase() in FlagSize) {
41-
this._size = FlagSize[val.toLowerCase() as (FlagSizeAlias)]
55+
return FlagSize[val.toLowerCase() as (FlagSizeAlias)]
4256
} else {
43-
this._size = Number.isInteger(val) && Number(val) > 0
57+
return Number.isInteger(val) && Number(val) > 0
4458
? Number(val)
4559
: 48
4660
}
47-
}
61+
})
4862

4963
/** @internal */
50-
@HostBinding('style.width.px')
51-
get width() {
52-
return this._size
53-
}
54-
55-
/** @internal */
56-
@HostBinding('style.height.px')
57-
get height() {
58-
return this._format == FlagFormatEnum.None
59-
? Math.floor(this._size / 1.5)
60-
: this._size
61-
}
64+
readonly _height = computed(() => this._format() == FlagFormatEnum.None
65+
? Math.floor(this._size() / 1.5)
66+
: this._size())
6267

6368
/** @internal */
64-
@HostBinding('style.borderRadius')
65-
get radius() {
66-
return this._format == FlagFormatEnum.Round ? '100%' : '0%'
67-
}
69+
readonly _radius = computed(() => this._format() == FlagFormatEnum.Round ? '100%' : '0%')
6870

6971
/** @internal */
70-
@HostBinding('style.backgroundImage')
71-
get image() {
72-
return `url(assets/flags/${this._code}.svg)`
73-
}
72+
readonly _image = computed(() => `url(assets/flags/${this._code()}.svg)`)
7473

7574
/** @internal */
76-
@HostBinding('style.display')
77-
get display() {
78-
return this._code
79-
? ''
80-
: 'none'
81-
}
82-
83-
constructor() { }
75+
readonly _display = computed(() => this._code()
76+
? ''
77+
: 'none')
8478
}

0 commit comments

Comments
 (0)