Skip to content
This repository has been archived by the owner on Oct 13, 2023. It is now read-only.

Implement forceSelection attribute #240

Open
wants to merge 9 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Add the following to `System.js` map configuration:
|overrideSuggested|If true will override suggested and set the model with the value in the input field.|boolean|No|false|
|openOnFocus|If true will open the dropdown and perform search when the input gets the focus.|boolean|No|false|
|fillHighlighted|If true will set the model with the value in the input field when item is highlighted.|boolean|No|true|
|forceSelection|If true will prevent from closing dropdown on Enter and Tab keys down if no selection was made.|boolean|No|true|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default should be false

|pause|Number of msec. to wait before searching.|number|No|250|
|placeholder|Placeholder text for the search field.|string|No||
|textNoResults|Text displayed when the search returned no results. if the string is falsy it won't be displayed|string|No|
Expand Down
4 changes: 2 additions & 2 deletions demo/completer-cmp-md.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="completer-holder" ctrCompleter>
<md-input-container class="completer-input">
<input mdInput ctrInput="clearSelected=clearSelected; overrideSuggested=overrideSuggested; fillHighlighted=fillHighlighted" [(ngModel)]="searchStr"
<input mdInput ctrInput="clearSelected=clearSelected; overrideSuggested=overrideSuggested; fillHighlighted=fillHighlighted; forceSelection=forceSelection" [(ngModel)]="searchStr"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'll be better to change one of the nativeCmp demos to show how it's working

[attr.name]="inputName" [attr.maxlength]="maxChars" [tabindex]="fieldTabindex" [disabled]="disableInput" (blur)="onBlur()" [placeholder]="placeholder"
autocomplete="off" autocorrect="off" autocapitalize="off">
</md-input-container>
Expand All @@ -25,4 +25,4 @@
</md-list-item>
</md-list>
</div>
</div>
</div>
1 change: 1 addition & 0 deletions src/components/completer-cmp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class CompleterCmp implements OnInit, ControlValueAccessor, AfterViewChec
@Input() public openOnFocus = false;
@Input() public initialValue: any;
@Input() public autoHighlight = false;
@Input() public forceSelection = false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forceSelection should also be used in the component html template [forceSelection]="forceSelection"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a confusion thinking completer-cmp-md.html was the template. I was rushing at that time ×)


@Output() public selected = new EventEmitter<CompleterItem>();
@Output() public highlighted = new EventEmitter<CompleterItem>();
Expand Down
9 changes: 7 additions & 2 deletions src/directives/ctr-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class CtrInput {
@Input("overrideSuggested") public overrideSuggested = false;
@Input("fillHighlighted") public fillHighlighted = true;
@Input("openOnFocus") public openOnFocus = false;
@Input("forceSelection") public forceSelection = false;

@Output() public ngModelChange: EventEmitter<any> = new EventEmitter();

Expand Down Expand Up @@ -99,7 +100,9 @@ export class CtrInput {
if (this.completer.hasHighlighted()) {
event.preventDefault();
}
this.handleSelection();
if (!this.forceSelection || this.completer._hasHighlighted) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

completer._hasHighlighted is a private member please use completer.hasHighlighted()

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the check can be done inside handleSelection which will prevent the code duplication here and in KEY_TAB

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not remember precisely what I was trying to do with the TAB, I guess maybe leaving the menu opened on TAB but this would prevent from switching to a next input in the form, which is pretty much what should happen. Otherwise you need to tqke your mouse to leave the field, and would be some kind of autofocus on TAB.

So I guess only ENTER key should be concerned.

this.handleSelection();
}
} else if (event.keyCode === KEY_DW) {
event.preventDefault();
this.completer.open();
Expand All @@ -108,7 +111,9 @@ export class CtrInput {
event.preventDefault();
this.completer.prevRow();
} else if (event.keyCode === KEY_TAB) {
this.handleSelection();
if (!this.forceSelection || this.completer._hasHighlighted) {
this.handleSelection();
}
} else if (event.keyCode === KEY_ES) {
// This is very specific to IE10/11 #272
// without this, IE clears the input text
Expand Down