-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
formControl for multi select #107
Comments
I ran into this issue as well, from what I can tell the underlining select is not being initialized correctly as a multiple select and the form control will pick which type of select it will be once the view is bound, it does not seem to update to multi-select if the select is changed by semantic-ui's javascript code. I have been able to fix this issue by adding a [attr.multiple] binding to the select. For example Current @Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: "sm-select",
template: `<div class="field" [ngClass]="{error: (!control?.valid && control?.touched) }">
<label *ngIf="label">{{label}}</label>
<select [formControl]="control" class="ui {{class}} dropdown" #select>
<option value="">{{placeholder}}</option>
<ng-content></ng-content>
</select>
</div>`
}) Altered too @Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: "sm-select",
// The ternary operator in the [attr.multiple] needs to return null so that the attribute is not added to none multi-select fields
template: `<div class="field" [ngClass]="{error: (!control?.valid && control?.touched) }">
<label *ngIf="label">{{label}}</label>
<select [attr.multiple]="class != null && class.indexOf('multiple') !== -1 ? true : null" [formControl]="control" class="ui {{class}} dropdown" #select>
<option value="">{{placeholder}}</option>
<ng-content></ng-content>
</select>
</div>`
}) However, if the attr.multiple binding adds the multiple after the init cycle of the view it will not update the formControl and the problem will persist. Hope this helps. UpdateThe solution I gave above will not work all the time. What probably should happen is 'sm-select' should be broken down into two different components, like what is being done in Angular2 SelectControls, one for the single version of the select and the other for the multiple version. For instance the components should act the same as they do now however they will have different component declarations. Example: Single Select @Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: "sm-select:not(.multiple)", // Selector changed to not include multiples
template: `<div class="field" [ngClass]="{error: (!control?.valid && control?.touched) }">
<label *ngIf="label">{{label}}</label>
<select [formControl]="control" class="ui {{class}} dropdown" #select>
<option value="">{{placeholder}}</option>
<ng-content></ng-content>
</select>
</div>`
}) Multi Select @Component({
changeDetection: ChangeDetectionStrategy.OnPush,
selector: "sm-select.multiple",
template: `<div class="field" [ngClass]="{error: (!control?.valid && control?.touched) }">
<label *ngIf="label">{{label}}</label>
<select multiple [formControl]="control" class="ui {{class}} dropdown" #select>
<option value="">{{placeholder}}</option>
<ng-content></ng-content>
</select>
</div>`
}) |
#181 simple change, forks for me. |
Do you know that
formControl
for multi select doesn't correctly bind more than one selected item?It get always the first selected item.
For example, if i have a multi select with [dog, cat, bird] and i select "cat" and "bird", in related
myForm.controls.select.value
i get just "cat" asstring
instead of "[cat, bird]" asarray
of string.Can you check it out?
The text was updated successfully, but these errors were encountered: