@@ -17,8 +17,11 @@ import { ToggleSwitchEventDetail } from '../../utils/interfaces/interaction.inte
17
17
export class B2bToggleSwitchComponent {
18
18
@Element ( ) host : HTMLB2bToggleSwitchElement ;
19
19
20
- /** The label of the toggle button. This is required */
21
- @Prop ( ) label ! : string ;
20
+ /** An optional label for the toggle switch. */
21
+ @Prop ( ) label ?: string ;
22
+
23
+ /**The toggle name. Use this if the toggle switch is used in a form group. */
24
+ @Prop ( ) name ?: string ;
22
25
23
26
/** The alignment of the toggle switch label. */
24
27
@Prop ( ) labelPosition ?: 'left' | 'right' = 'left' ;
@@ -27,13 +30,14 @@ export class B2bToggleSwitchComponent {
27
30
@Prop ( ) disabled = false ;
28
31
29
32
/** Whether or not the toggle button is currently on or off. Per default it is off. */
30
- @Prop ( ) state = false ;
33
+ @Prop ( { mutable : true } ) state = false ;
31
34
32
35
/** Emits the toggle switch value when it's state changes. */
33
36
@Event ( { eventName : 'b2b-change' } )
34
37
b2bChange : EventEmitter < ToggleSwitchEventDetail > ;
35
38
36
39
private emitDetail = ( ) => {
40
+ console . log ( this . state ) ;
37
41
this . b2bChange . emit ( {
38
42
value : this . state ,
39
43
} ) ;
@@ -53,43 +57,44 @@ export class B2bToggleSwitchComponent {
53
57
this . emitDetail ( ) ;
54
58
} ;
55
59
60
+ private onKeyDown = ( ev : KeyboardEvent ) => {
61
+ if ( ev . key != 'Enter' || this . disabled ) {
62
+ return ;
63
+ } else {
64
+ this . state = ! this . state ;
65
+ }
66
+ this . emitDetail ( ) ;
67
+ } ;
68
+
56
69
render ( ) {
57
70
return (
58
71
< Host >
59
72
< div
60
73
class = { {
61
74
'b2b-toggle' : true ,
75
+ [ `b2b-toggle__${ this . labelPosition } ` ] : true ,
62
76
'b2b-toggle--disabled' : this . disabled ,
77
+ 'b2b-toggle--checked' : this . state ,
63
78
} } >
64
- < div
65
- class = { {
66
- [ `b2b-toggle__${ this . labelPosition } ` ] : true ,
67
- 'b2b-toggle__icon--show' : this . state ,
68
- } } >
69
- < div onClick = { this . onClick } >
79
+ < label class = "b2b-toggle__label" htmlFor = "toggle" >
80
+ < span
81
+ class = "b2b-toggle__switch"
82
+ role = "switch"
83
+ tabIndex = { 0 }
84
+ onKeyDown = { this . onKeyDown }
85
+ onClick = { this . onClick } >
70
86
< svg
71
87
class = { {
72
88
'b2b-toggle__icon' : true ,
89
+ 'b2b-toggle__icon--show' : this . state ,
73
90
} }
74
91
xmlns = "http://www.w3.org/2000/svg"
75
92
viewBox = "0 0 30 30" >
76
93
< path d = "M11.798 25.082c-.341 0-.681-.13-.942-.389l-7.132-7.115a1.334 1.334 0 0 1 1.884-1.888l6.19 6.175L26.391 7.307a1.334 1.334 0 0 1 1.884 1.888L12.74 24.693c-.26.259-.601.389-.942.389z" />
77
94
</ svg >
78
- < input
79
- class = "b2b-toggle__input"
80
- id = "toggle"
81
- type = "checkbox"
82
- checked = { this . state }
83
- disabled = { this . disabled }
84
- />
85
- < label class = "b2b-toggle__label" htmlFor = "toggle" >
86
- < span class = "b2b-toggle__switch" />
87
- </ label >
88
- </ div >
89
- < div class = { { [ `b2b-toggle__text-${ this . labelPosition } ` ] : true } } >
90
- { this . label }
91
- </ div >
92
- </ div >
95
+ </ span >
96
+ { this . label && < span class = "b2b-toggle__text" > { this . label } </ span > }
97
+ </ label >
93
98
</ div >
94
99
</ Host >
95
100
) ;
0 commit comments