@@ -16,8 +16,9 @@ export default function createBinding ( generator, node, attribute, current, loc
16
16
const handler = current . getUniqueName ( `${ local . name } ChangeHandler` ) ;
17
17
18
18
const isMultipleSelect = node . name === 'select' && node . attributes . find ( attr => attr . name . toLowerCase ( ) === 'multiple' ) ; // TODO use getStaticAttributeValue
19
+ const type = getStaticAttributeValue ( node , 'type' ) ;
19
20
const bindingGroup = attribute . name === 'group' ? getBindingGroup ( generator , current , attribute , keypath ) : null ;
20
- const value = getBindingValue ( generator , local , node , attribute , isMultipleSelect , bindingGroup ) ;
21
+ const value = getBindingValue ( generator , local , node , attribute , isMultipleSelect , bindingGroup , type ) ;
21
22
const eventName = getBindingEventName ( node ) ;
22
23
23
24
let setter = getSetter ( { current, name, context : '__svelte' , attribute, dependencies, snippet, value } ) ;
@@ -26,7 +27,7 @@ export default function createBinding ( generator, node, attribute, current, loc
26
27
// <select> special case
27
28
if ( node . name === 'select' ) {
28
29
if ( ! isMultipleSelect ) {
29
- setter = `var selectedOption = ${ local . name } .selectedOptions[0] || ${ local . name } .options[0];\n` + setter ;
30
+ setter = `var selectedOption = ${ local . name } .selectedOptions[0] || ${ local . name } .options[0];\n${ setter } ` ;
30
31
}
31
32
32
33
const value = generator . current . getUniqueName ( 'value' ) ;
@@ -54,27 +55,28 @@ export default function createBinding ( generator, node, attribute, current, loc
54
55
55
56
// <input type='checkbox|radio' bind:group='selected'> special case
56
57
else if ( attribute . name === 'group' ) {
57
- const type = getStaticAttributeValue ( node , 'type' ) ;
58
+ if ( type === 'radio' ) {
59
+ setter = deindent `
60
+ if ( !${ local . name } .checked ) return;
61
+ ${ setter }
62
+ component._bindingGroups[${ bindingGroup } ].forEach( function ( input ) {
63
+ input.checked = false;
64
+ });` ;
65
+ }
58
66
59
- if ( type === 'checkbox' ) {
60
- local . init . addLine (
61
- `component._bindingGroups[${ bindingGroup } ].push( ${ local . name } );`
62
- ) ;
67
+ const condition = type === 'checkbox' ?
68
+ `~${ snippet } .indexOf( ${ local . name } .__value )` :
69
+ `${ local . name } .__value === ${ snippet } ` ;
63
70
64
- local . teardown . addBlock (
65
- `component._bindingGroups[${ bindingGroup } ].splice( component._bindingGroups[ ${ bindingGroup } ].indexOf( ${ local . name } ), 1 );`
66
- ) ;
71
+ local . init . addLine (
72
+ `component._bindingGroups[${ bindingGroup } ].push( ${ local . name } );`
73
+ ) ;
67
74
68
- updateElement = `${ local . name } .checked = ~${ snippet } .indexOf( ${ local . name } .__value );` ;
69
- }
70
-
71
- else if ( type === 'radio' ) {
72
- throw new Error ( 'TODO' ) ;
73
- }
75
+ local . teardown . addBlock (
76
+ `component._bindingGroups[${ bindingGroup } ].splice( component._bindingGroups[${ bindingGroup } ].indexOf( ${ local . name } ), 1 );`
77
+ ) ;
74
78
75
- else {
76
- throw new Error ( `Unexpected bind:group` ) ; // TODO catch this in validation with a better error
77
- }
79
+ updateElement = `${ local . name } .checked = ${ condition } ;` ;
78
80
}
79
81
80
82
// everything else
@@ -122,7 +124,7 @@ function getBindingEventName ( node ) {
122
124
return 'change' ;
123
125
}
124
126
125
- function getBindingValue ( generator , local , node , attribute , isMultipleSelect , bindingGroup ) {
127
+ function getBindingValue ( generator , local , node , attribute , isMultipleSelect , bindingGroup , type ) {
126
128
// <select multiple bind:value='selected>
127
129
if ( isMultipleSelect ) {
128
130
return `[].map.call( ${ local . name } .selectedOptions, function ( option ) { return option.__value; })` ;
@@ -135,7 +137,11 @@ function getBindingValue ( generator, local, node, attribute, isMultipleSelect,
135
137
136
138
// <input type='checkbox' bind:group='foo'>
137
139
if ( attribute . name === 'group' ) {
138
- return `${ generator . helper ( 'getBindingGroupValue' ) } ( component._bindingGroups[${ bindingGroup } ] )` ;
140
+ if ( type === 'checkbox' ) {
141
+ return `${ generator . helper ( 'getBindingGroupValue' ) } ( component._bindingGroups[${ bindingGroup } ] )` ;
142
+ }
143
+
144
+ return `${ local . name } .__value` ;
139
145
}
140
146
141
147
// everything else
0 commit comments