Skip to content

Commit

Permalink
feat(list): Default parameters (autoselect, multiply)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkramarov committed May 22, 2018
1 parent 697aa5a commit c471ac6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
7 changes: 5 additions & 2 deletions src/lib/list/list-selection.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,10 @@ describe('McListSelection with forms', () => {

@Component({
template: `
<mc-list-selection id="selection-list-1" (selectionChange)="onValueChange($event)">
<mc-list-selection id="selection-list-1"
auto-select="false"
no-unselect="false"
(selectionChange)="onValueChange($event)">
<mc-list-option checkboxPosition="before" disabled="true" value="inbox">
Inbox (disabled selection-option)
</mc-list-option>
Expand Down Expand Up @@ -929,7 +932,7 @@ class SelectionListWithTabindexBinding {

@Component({
template: `
<mc-list-selection [(ngModel)]="selectedOptions">
<mc-list-selection [(ngModel)]="selectedOptions" auto-select="false">
<mc-list-option value="opt1">Option 1</mc-list-option>
<mc-list-option value="opt2">Option 2</mc-list-option>
<mc-list-option value="opt3" *ngIf="renderLastOption">Option 3</mc-list-option>
Expand Down
34 changes: 23 additions & 11 deletions src/lib/list/list-selection.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class McListOption implements AfterContentInit, OnDestroy, OnInit, IFocus
}

_handleClick() {
if (this.disabled || this.listSelection.selectOnFocus) { return; }
if (this.disabled || this.listSelection.autoSelect) { return; }

this.toggle();
// Emit a change event if the selected state of the option changed through user interaction.
Expand Down Expand Up @@ -250,11 +250,13 @@ export class McListSelection extends _McListSelectionMixinBase implements
// The option components contained within this selection-list.
@ContentChildren(McListOption) options: QueryList<McListOption>;

@Input() horizontal: boolean = false;
@Input() multiple: boolean = false;
@Input() selectOnFocus: boolean = false;
tabIndex: number;

autoSelect: boolean;
noUnselect: boolean;
multiple: boolean;

@Input() tabIndex: number = 0;
@Input() horizontal: boolean = false;

// Emits a change event whenever the selected state of an option changes.
@Output() readonly selectionChange: EventEmitter<McListSelectionChange> =
Expand All @@ -270,16 +272,24 @@ export class McListSelection extends _McListSelectionMixinBase implements
// непонятна целесообразность сего
private _modelChanges = Subscription.EMPTY;

constructor(private _element: ElementRef, @Attribute('tabindex') tabIndex: string) {
constructor(
private _element: ElementRef,
@Attribute('tabindex') tabIndex: string,
@Attribute('auto-select') autoSelect: string,
@Attribute('no-unselect') noUnselect: string,
@Attribute('multiple') multiple: string
) {
super();

this.autoSelect = autoSelect === null ? true : toBoolean(autoSelect);
this.multiple = multiple === null ? true : toBoolean(multiple);
this.noUnselect = noUnselect === null ? true : toBoolean(noUnselect);

this.tabIndex = parseInt(tabIndex) || 0;
}

ngAfterContentInit(): void {
this.horizontal = toBoolean(this.horizontal);
this.multiple = toBoolean(this.multiple);
this.selectOnFocus = toBoolean(this.selectOnFocus);

this._keyManager = new FocusKeyManager<McListOption>(this.options)
.withTypeAhead()
Expand Down Expand Up @@ -331,11 +341,11 @@ export class McListSelection extends _McListSelectionMixinBase implements
}

// Sets the focused option of the selection-list.
setFocusedOption(option: McListOption) {
setFocusedOption(option: McListOption): void {
this._keyManager.updateActiveItemIndex(this._getOptionIndex(option));

if (this.selectOnFocus) {
if (!this.multiple) { this.options.forEach((item) => item.setSelected(false)); }
if (this.autoSelect) {
this.options.forEach((item) => item.setSelected(false));

option.setSelected(true);

Expand Down Expand Up @@ -376,6 +386,8 @@ export class McListSelection extends _McListSelectionMixinBase implements

// Toggles the selected state of the currently focused option.
toggleFocusedOption(): void {
if (this.noUnselect && this.selectedOptions.selected.length === 1) { return; }

const focusedIndex = this._keyManager.activeItemIndex;

if (focusedIndex != null && this._isValidIndex(focusedIndex)) {
Expand Down

0 comments on commit c471ac6

Please sign in to comment.