@@ -6,36 +6,44 @@ import { YbLocality } from './yb-locality';
6
6
import { YbStreet } from './yb-street' ;
7
7
import { YbExtended } from './yb-extended' ;
8
8
import { Observable , merge , Subscription } from 'rxjs' ;
9
- import { startWith , mapTo , filter } from 'rxjs/operators' ;
9
+ import { startWith , mapTo , filter , map } from 'rxjs/operators' ;
10
+
11
+ export type YbAddressAutocompleteMode = 'change' | 'keyup' | 'off' ;
10
12
11
13
@Directive ( {
12
14
selector : '[ybAddress]' ,
13
15
exportAs : 'ybAddress' ,
14
16
} )
15
17
export class YbAddress implements OnDestroy , AfterContentInit {
16
18
17
- @Input ( 'ybAutocompleteMode' ) autocompleteMode : 'change' | 'keyup' | 'off' = 'keyup' ;
19
+ @Input ( 'ybAutocompleteMode' ) autocompleteMode : YbAddressAutocompleteMode = 'keyup' ;
18
20
19
- @ContentChildren ( YbPostalCode , { descendants : true } ) postalCodes : QueryList < YbPostalCode > ;
21
+ @ContentChildren ( YbPostalCode , { descendants : true } ) postalCodes ! : QueryList < YbPostalCode > ;
20
22
21
- @ContentChild ( YbRegion ) region : YbRegion ;
23
+ @ContentChild ( YbRegion ) region : YbRegion | null = null ;
22
24
23
- @ContentChild ( YbLocality ) locality : YbLocality ;
25
+ @ContentChild ( YbLocality ) locality : YbLocality | null = null ;
24
26
25
- @ContentChild ( YbStreet ) street : YbStreet ;
27
+ @ContentChild ( YbStreet ) street : YbStreet | null = null ;
26
28
27
- @ContentChild ( YbExtended ) extended : YbExtended ;
29
+ @ContentChild ( YbExtended ) extended : YbExtended | null = null ;
28
30
29
- private _changeSubscription : Subscription ;
31
+ private _changeSubscription : Subscription | null = null ;
30
32
31
- private _postalCodeValueChangeSubscription : Subscription ;
33
+ private _postalCodeValueChangeSubscription : Subscription | null = null ;
32
34
33
35
get postalCodeValueChanges ( ) : Observable < string > {
34
- return merge ( ...this . postalCodes . map ( p => p . _onChange ) ) ;
36
+ return merge ( ...this . postalCodes . map ( p => p . _onChange ) )
37
+ . pipe (
38
+ map ( v => v || '' ) ,
39
+ ) ;
35
40
}
36
41
37
42
get postalCodeKeyup ( ) : Observable < string > {
38
- return merge ( ...this . postalCodes . map ( p => p . _onKeyup ) ) ;
43
+ return merge ( ...this . postalCodes . map ( p => p . _onKeyup ) )
44
+ . pipe (
45
+ map ( v => v || '' ) ,
46
+ ) ;
39
47
}
40
48
41
49
constructor (
@@ -65,16 +73,12 @@ export class YbAddress implements OnDestroy, AfterContentInit {
65
73
66
74
this . _addressManager . getAddress ( postalCode )
67
75
. subscribe ( addr => {
68
- // 一旦すべてリセット
69
- if ( this . region ) { this . region . value = '' ; }
70
- if ( this . locality ) { this . locality . value = '' ; }
71
- if ( this . street ) { this . street . value = '' ; }
72
- if ( this . extended ) { this . extended . value = '' ; }
73
-
74
- if ( this . region ) { this . region . value += this . _addressManager . getRegion ( addr ) ; }
75
- if ( this . locality ) { this . locality . value += this . _addressManager . getLocality ( addr ) ; }
76
- if ( this . street ) { this . street . value += this . _addressManager . getStreet ( addr ) ; }
77
- if ( this . extended ) { this . extended . value += this . _addressManager . getExtended ( addr ) ; }
76
+ if ( addr ) {
77
+ if ( this . region ) { this . region . value = this . _addressManager . getRegion ( addr ) ; }
78
+ if ( this . locality ) { this . locality . value = this . _addressManager . getLocality ( addr ) ; }
79
+ if ( this . street ) { this . street . value = this . _addressManager . getStreet ( addr ) ; }
80
+ if ( this . extended ) { this . extended . value = this . _addressManager . getExtended ( addr ) ; }
81
+ }
78
82
} ) ;
79
83
}
80
84
@@ -91,12 +95,12 @@ export class YbAddress implements OnDestroy, AfterContentInit {
91
95
}
92
96
93
97
private _listenToPostalCode ( ) {
94
- this . _postalCodeValueChangeSubscription = merge < typeof YbAddress [ 'prototype' ] [ 'autocompleteMode' ] > (
98
+ this . _postalCodeValueChangeSubscription = merge < YbAddressAutocompleteMode > (
95
99
this . postalCodeValueChanges . pipe ( mapTo ( 'change' ) ) ,
96
100
this . postalCodeKeyup . pipe ( mapTo ( 'keyup' ) )
97
101
)
98
102
. pipe ( filter ( mode => mode === this . autocompleteMode ) )
99
- . subscribe ( ( mode ) => {
103
+ . subscribe ( ( _ ) => {
100
104
this . complete ( ) ;
101
105
} ) ;
102
106
}
0 commit comments