Skip to content

Commit

Permalink
Fixed #1800 - Add forceSelection to AutoComplete
Browse files Browse the repository at this point in the history
  • Loading branch information
mertsincan committed Feb 4, 2021
1 parent 13cb339 commit bbbb258
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/components/autocomplete/AutoComplete.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface AutoCompleteProps {
type?: string;
suggestions?: any[];
field?: string;
forceSelection?: boolean;
scrollHeight?: string;
dropdown?: boolean;
dropdownMode?: string;
Expand Down
39 changes: 36 additions & 3 deletions src/components/autocomplete/AutoComplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class AutoComplete extends Component {
type: 'text',
suggestions: null,
field: null,
forceSelection: false,
scrollHeight: '200px',
dropdown: false,
dropdownMode: 'blank',
Expand Down Expand Up @@ -68,6 +69,7 @@ export class AutoComplete extends Component {
type: PropTypes.string,
suggestions: PropTypes.array,
field: PropTypes.string,
forceSelection: PropTypes.bool,
scrollHeight: PropTypes.string,
dropdown: PropTypes.bool,
dropdownMode: PropTypes.string,
Expand Down Expand Up @@ -186,7 +188,7 @@ export class AutoComplete extends Component {
}
}

selectItem(event, option) {
selectItem(event, option, preventInputFocus) {
if (this.props.multiple) {
this.inputEl.value = '';
if (!this.isSelected(option)) {
Expand All @@ -206,8 +208,10 @@ export class AutoComplete extends Component {
})
}

this.inputEl.focus();
this.hideOverlay();
if (!preventInputFocus) {
this.inputEl.focus();
this.hideOverlay();
}
}

updateModel(event, value) {
Expand Down Expand Up @@ -420,10 +424,39 @@ export class AutoComplete extends Component {
});
}

forceItemSelection(event) {
let valid = false;
let inputValue = event.target.value.trim();

if (this.props.suggestions) {
for (let item of this.props.suggestions) {
let itemValue = this.props.field ? ObjectUtils.resolveFieldData(item, this.props.field) : item;
if (itemValue && inputValue === itemValue.trim()) {
valid = true;
this.selectItem(event, item, true);
break;
}
}
}

if (!valid) {
this.inputEl.value = '';
this.updateModel(event, null);

if (this.props.onClear) {
this.props.onClear(event);
}
}
}

onInputBlur(event) {
event.persist();

this.setState({ focused: false }, () => {
if (this.props.forceSelection) {
this.forceItemSelection(event);
}

if (this.props.onBlur) {
this.props.onBlur(event);
}
Expand Down

0 comments on commit bbbb258

Please sign in to comment.