-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Çağatay Çivici
committed
Dec 4, 2017
1 parent
336ed9f
commit 5af65bd
Showing
2 changed files
with
73 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import ReactDOM from 'react-dom'; | ||
import ObjectUtils from '../utils/ObjectUtils'; | ||
|
||
export class AutoCompletePanel extends Component { | ||
|
||
static defaultProps = { | ||
suggestions: null, | ||
field: null, | ||
appendTo: null, | ||
itemTemplate: null, | ||
onItemClick: null, | ||
scrollHeight: '200px' | ||
} | ||
|
||
static propTypes = { | ||
suggestions: PropTypes.array, | ||
field: PropTypes.string, | ||
appendTo: PropTypes.any, | ||
itemTemplate: PropTypes.func, | ||
onItemClick: PropTypes.func, | ||
scrollHeight: PropTypes.string | ||
}; | ||
|
||
renderElement() { | ||
let items; | ||
|
||
if (this.props.suggestions) { | ||
items = this.props.suggestions.map((suggestion, index) => { | ||
let itemContent = this.props.itemTemplate ? this.props.itemTemplate(suggestion) : this.props.field ? ObjectUtils.resolveFieldData(suggestion, this.props.field) : suggestion; | ||
|
||
return ( | ||
<li key={index + '_item'} className="ui-autocomplete-list-item ui-corner-all" onClick={(e) => this.props.onItemClick(e, suggestion)}>{itemContent}</li> | ||
); | ||
}); | ||
} | ||
|
||
return ( | ||
<div ref={(el) => this.element = el} className="ui-autocomplete-panel ui-widget-content ui-corner-all ui-shadow" style={{ maxHeight: this.props.scrollHeight }}> | ||
<ul className="ui-autocomplete-items ui-autocomplete-list ui-widget-content ui-widget ui-corner-all ui-helper-reset"> | ||
{items} | ||
</ul> | ||
</div> | ||
); | ||
} | ||
|
||
render() { | ||
let element = this.renderElement(); | ||
|
||
if (this.props.appendTo) { | ||
return ReactDOM.createPortal(element, this.props.appendTo); | ||
} | ||
else { | ||
return element; | ||
} | ||
} | ||
} |