@@ -7,42 +7,55 @@ class RadioButtonGroup {
7
7
return this . groups . get ( groupName ) ;
8
8
}
9
9
10
+ static getSelectedRadioFromGroup ( groupName ) {
11
+ return this . selectedRadios . get ( groupName ) ;
12
+ }
13
+
10
14
static removeGroup ( groupName ) {
15
+ this . selectedRadios . delete ( groupName ) ;
11
16
return this . groups . delete ( groupName ) ;
12
17
}
13
18
14
- static addToGroup ( control , groupName ) {
19
+ static addToGroup ( radioBtn , groupName ) {
15
20
if ( this . hasGroup ( groupName ) ) {
16
- this . getGroup ( groupName ) . push ( control ) ;
21
+ this . enforceSingleSelection ( radioBtn , groupName ) ;
22
+ this . getGroup ( groupName ) . push ( radioBtn ) ;
17
23
} else {
18
- this . createGroup ( control , groupName ) ;
24
+ this . createGroup ( radioBtn , groupName ) ;
19
25
}
20
26
}
21
27
22
- static removeFromGroup ( control , groupName ) {
28
+ static removeFromGroup ( radioBtn , groupName ) {
23
29
if ( ! this . hasGroup ( groupName ) ) {
24
30
return ;
25
31
}
26
32
27
33
const group = this . getGroup ( groupName ) ;
34
+ const selectedRadio = this . getSelectedRadioFromGroup ( groupName ) ;
28
35
29
- // Remove the control from the given group
30
- group . forEach ( ( _control , idx , arr ) => {
31
- if ( control . _id === _control . _id ) {
36
+ // Remove the radio button from the given group
37
+ group . forEach ( ( _radioBtn , idx , arr ) => {
38
+ if ( radioBtn . _id === _radioBtn . _id ) {
32
39
return arr . splice ( idx , 1 ) ;
33
40
}
34
41
} ) ;
35
42
43
+ if ( selectedRadio === radioBtn ) {
44
+ this . selectedRadios . set ( groupName , null ) ;
45
+ }
46
+
36
47
// Remove the group if it is empty
37
48
if ( ! group . length ) {
38
49
this . removeGroup ( groupName ) ;
39
50
}
40
51
}
41
52
42
- static createGroup ( control , groupName ) {
43
- if ( ! this . hasGroup ( groupName ) ) {
44
- this . groups . set ( groupName , [ control ] ) ;
53
+ static createGroup ( radioBtn , groupName ) {
54
+ if ( radioBtn . selected ) {
55
+ this . selectedRadios . set ( groupName , radioBtn ) ;
45
56
}
57
+
58
+ this . groups . set ( groupName , [ radioBtn ] ) ;
46
59
}
47
60
48
61
static selectNextItem ( item , groupName ) {
@@ -56,16 +69,7 @@ class RadioButtonGroup {
56
69
57
70
const nextItemToSelect = this . _nextSelectable ( currentItemPosition , group ) ;
58
71
59
- // de-select all the rest
60
- group . forEach ( radio => {
61
- if ( radio . _id !== nextItemToSelect . _id ) {
62
- radio . selected = false ;
63
- radio . fireEvent ( "select" , { selected : radio . selected } ) ;
64
- }
65
- } ) ;
66
-
67
- // select the next item
68
- this . _selectItem ( nextItemToSelect ) ;
72
+ this . updateSelectionInGroup ( nextItemToSelect , groupName ) ;
69
73
}
70
74
71
75
static selectPreviousItem ( item , groupName ) {
@@ -79,81 +83,103 @@ class RadioButtonGroup {
79
83
80
84
const previousItemToSelect = this . _previousSelectable ( currentItemPosition , group ) ;
81
85
82
- // de-select all the rest
83
- group . forEach ( radio => {
84
- if ( radio . _id !== previousItemToSelect . _id ) {
85
- radio . selected = false ;
86
- radio . fireEvent ( "select" , { selected : radio . selected } ) ;
87
- }
88
- } ) ;
89
-
90
- // select the next item
91
- this . _selectItem ( previousItemToSelect ) ;
86
+ this . updateSelectionInGroup ( previousItemToSelect , groupName ) ;
92
87
}
93
88
94
89
static selectItem ( item , groupName ) {
95
- const group = this . getGroup ( groupName ) ;
90
+ this . updateSelectionInGroup ( item , groupName ) ;
91
+ }
96
92
97
- // de-select all the rest
98
- group . forEach ( radio => {
99
- if ( radio . _id !== item . _id ) {
100
- radio . selected = false ;
101
- radio . fireEvent ( "select" , { selected : radio . selected } ) ;
102
- }
103
- } ) ;
93
+ static updateSelectionInGroup ( radioBtnToSelect , groupName ) {
94
+ const selectedRadio = this . getSelectedRadioFromGroup ( groupName ) ;
104
95
105
- this . _selectItem ( item ) ;
96
+ this . _deselectRadio ( selectedRadio ) ;
97
+ this . _selectRadio ( radioBtnToSelect ) ;
98
+ this . selectedRadios . set ( groupName , radioBtnToSelect ) ;
106
99
}
107
100
108
- static get groups ( ) {
109
- if ( ! this . _groups ) {
110
- this . _groups = new Map ( ) ;
101
+ static _deselectRadio ( radioBtn ) {
102
+ if ( radioBtn ) {
103
+ radioBtn . selected = false ;
111
104
}
112
- return this . _groups ;
113
105
}
114
106
115
- static _selectItem ( item ) {
116
- item . focus ( ) ;
117
- item . selected = true ;
118
- item . fireEvent ( "select" , { selected : item . selected } ) ;
107
+ static _selectRadio ( radioBtn ) {
108
+ if ( radioBtn ) {
109
+ radioBtn . focus ( ) ;
110
+ radioBtn . selected = true ;
111
+ radioBtn . _selected = true ;
112
+ radioBtn . fireEvent ( "select" ) ;
113
+ }
119
114
}
120
115
121
116
static _nextSelectable ( pos , group ) {
122
117
const groupLength = group . length ;
123
- let nextItemToSelect = null ;
118
+ let nextRadioToSelect = null ;
124
119
125
120
if ( pos === groupLength - 1 ) {
126
121
if ( ! group [ 0 ] . disabled ) {
127
- nextItemToSelect = group [ 0 ] ;
122
+ nextRadioToSelect = group [ 0 ] ;
128
123
} else {
129
124
return this . _nextSelectable ( 0 , group ) ;
130
125
}
131
126
} else if ( ! group [ ++ pos ] . disabled ) {
132
- nextItemToSelect = group [ pos ] ;
127
+ nextRadioToSelect = group [ pos ] ;
133
128
} else {
134
129
return this . _nextSelectable ( pos , group ) ;
135
130
}
136
131
137
- return nextItemToSelect ;
132
+ return nextRadioToSelect ;
138
133
}
139
134
140
135
static _previousSelectable ( pos , group ) {
141
136
const groupLength = group . length ;
142
- let previousSelectable = null ;
137
+ let previousRadioToSelect = null ;
143
138
144
139
if ( pos === 0 ) {
145
140
if ( ! group [ groupLength - 1 ] . disabled ) {
146
- previousSelectable = group [ groupLength - 1 ] ;
141
+ previousRadioToSelect = group [ groupLength - 1 ] ;
147
142
} else {
148
143
return this . _previousSelectable ( groupLength - 1 , group ) ;
149
144
}
150
145
} else if ( ! group [ -- pos ] . disabled ) {
151
- previousSelectable = group [ pos ] ;
146
+ previousRadioToSelect = group [ pos ] ;
152
147
} else {
153
148
return this . _previousSelectable ( pos , group ) ;
154
149
}
155
150
156
- return previousSelectable ;
151
+ return previousRadioToSelect ;
152
+ }
153
+
154
+ static enforceSingleSelection ( radioBtn , groupName ) {
155
+ const selectedRadio = this . getSelectedRadioFromGroup ( groupName ) ;
156
+
157
+ if ( ! selectedRadio ) {
158
+ return ;
159
+ }
160
+
161
+ if ( radioBtn . selected ) {
162
+ if ( radioBtn !== selectedRadio ) {
163
+ this . _deselectRadio ( selectedRadio ) ;
164
+ this . selectedRadios . set ( groupName , radioBtn ) ;
165
+ }
166
+ } else if ( radioBtn === selectedRadio ) {
167
+ this . selectedRadios . set ( groupName , null ) ;
168
+ }
169
+ }
170
+
171
+ static get groups ( ) {
172
+ if ( ! this . _groups ) {
173
+ this . _groups = new Map ( ) ;
174
+ }
175
+ return this . _groups ;
176
+ }
177
+
178
+ static get selectedRadios ( ) {
179
+ if ( ! this . _selectedRadios ) {
180
+ this . _selectedRadios = new Map ( ) ;
181
+ }
182
+ return this . _selectedRadios ;
157
183
}
158
184
}
159
185
0 commit comments