Skip to content

Commit

Permalink
Fix select issue (#745)
Browse files Browse the repository at this point in the history
* Ensure data value is an array before filtering

During work in #707 to improve support for multi-select fields,
handling of comma separated strings was removed as it may have
been confusing if an option value had commas in it.

See 9894162

After this change, string values for single select elements are
filtered as if they were arrays. This causes only the first
character of a string to be compared to the select value. This in
turn results in all select fields set to first option because
nothing is seen as selected.

We can accept arrays, but also `split()` with no separator to turn
strings into an array so that everything is parsed the same.

* Use _.contains instead of filter/empty

* Split on comma rather than just casting to array
  • Loading branch information
mattheu authored Jun 20, 2017
1 parent 5b252df commit 0440cee
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions inc/templates/edit-form.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@
<# if ( 'options' in option && 'label' in option ) { #>
<optgroup label="{{ option.label }}">
<# _.each( option.options, function( optgroupOption ) { #>
<option value="{{ optgroupOption.value }}" <# if ( ! _.isEmpty( _.filter( data.value, function(val) { return val === optgroupOption.value; } ) ) ) { print('selected'); } #>>{{ optgroupOption.label }}</option>
<option value="{{ optgroupOption.value }}" <# if ( _.contains( _.isArray( data.value ) ? data.value : data.value.split(','), optgroupOption.value ) ) { print('selected'); } #>>{{ optgroupOption.label }}</option>
<# }); #>
</optgroup>
<# } else { #>
<option value="{{ option.value }}" <# if ( ! _.isEmpty( _.filter( data.value, function(val) { return val === option.value; } ) ) ) { print('selected'); } #>>{{ option.label }}</option>
<option value="{{ option.value }}" <# if ( _.contains( _.isArray( data.value ) ? data.value : data.value.split(','), option.value ) ) { print('selected'); } #>>{{ option.label }}</option>
<# } #>

<# }); #>
Expand Down

0 comments on commit 0440cee

Please sign in to comment.