-
Notifications
You must be signed in to change notification settings - Fork 64
Added function render function option #96
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,8 @@ const propTypes = { | |
offsetX: PropTypes.number, | ||
offsetY: PropTypes.number, | ||
passThroughEnter: PropTypes.bool, | ||
renderOptionsList: PropTypes.func, | ||
expandUp: PropTypes.bool | ||
}; | ||
|
||
const defaultProps = { | ||
|
@@ -72,6 +74,8 @@ const defaultProps = { | |
offsetY: 0, | ||
value: null, | ||
passThroughEnter: false, | ||
renderOptionsList: (markUp, plaintext)=> markUp, | ||
expandUp: false | ||
}; | ||
|
||
class AutocompleteTextField extends React.Component { | ||
|
@@ -429,6 +433,8 @@ class AutocompleteTextField extends React.Component { | |
value, | ||
} = this.state; | ||
|
||
const {renderOptionsList, expandUp} = this.props; | ||
|
||
if (!helperVisible) { | ||
return null; | ||
} | ||
|
@@ -447,8 +453,11 @@ class AutocompleteTextField extends React.Component { | |
|
||
const optionNumber = maxOptions === 0 ? options.length : maxOptions; | ||
|
||
let height = offsetY; | ||
|
||
const helperOptions = options.slice(0, optionNumber).map((val, idx) => { | ||
const highlightStart = val.toLowerCase().indexOf(value.substr(matchStart, matchLength).toLowerCase()); | ||
height -= 52; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why 52? Magic constant here is confusing |
||
|
||
return ( | ||
<li | ||
|
@@ -457,15 +466,18 @@ class AutocompleteTextField extends React.Component { | |
onClick={() => { this.handleSelection(idx); }} | ||
onMouseEnter={() => { this.setState({ selection: idx }); }} | ||
> | ||
{val.slice(0, highlightStart)} | ||
<strong>{val.substr(highlightStart, matchLength)}</strong> | ||
{val.slice(highlightStart + matchLength)} | ||
|
||
{renderOptionsList(<>{val.slice(0, highlightStart)}<strong>{val.substr(highlightStart, matchLength)}</strong>{val.slice(highlightStart + matchLength)}</>, val)} | ||
</li> | ||
); | ||
}); | ||
|
||
if (!expandUp) { | ||
height = top + offsetY; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The way logic is spreaded for height evaluation if confusing. Something like below would be cleaner from my perspective:
|
||
} | ||
|
||
return ( | ||
<ul className="react-autocomplete-input" style={{ left: left + offsetX, top: top + offsetY }}> | ||
<ul className="react-autocomplete-input" style={{ left: left + offsetX, top: expandUp }}> | ||
{helperOptions} | ||
</ul> | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
(markUp, plaintext) => markUp,