Skip to content

Commit

Permalink
Merge pull request #124 from tjphopkins/master
Browse files Browse the repository at this point in the history
Allow for custom suggestion component
  • Loading branch information
i-like-robots committed Jul 3, 2018
2 parents 23f7ba3 + db5ed04 commit c85baf5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
10 changes: 8 additions & 2 deletions lib/ReactTags.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ class ReactTags extends React.Component {
id={this.props.id}
ref={(c) => { this.suggestions = c }}
expanded={expanded}
addTag={this.addTag.bind(this)} />
addTag={this.addTag.bind(this)}
suggestionComponent={this.props.suggestionComponent} />
</div>
</div>
)
Expand All @@ -228,7 +229,8 @@ ReactTags.defaultProps = {
maxSuggestionsLength: 6,
allowNew: false,
allowBackspace: true,
tagComponent: null
tagComponent: null,
suggestionComponent: null
}

ReactTags.propTypes = {
Expand All @@ -251,6 +253,10 @@ ReactTags.propTypes = {
tagComponent: PropTypes.oneOfType([
PropTypes.func,
PropTypes.element
]),
suggestionComponent: PropTypes.oneOfType([
PropTypes.func,
PropTypes.element
])
}

Expand Down
8 changes: 7 additions & 1 deletion lib/Suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ function markIt (name, query) {
return name.replace(regexp, '<mark>$&</mark>')
}

const DefaultSuggestionComponent = ({ item, query }) => (
<span dangerouslySetInnerHTML={{ __html: markIt(item.name, query) }} />
)

class Suggestions extends React.Component {
onMouseDown (item, e) {
// focus is shifted on mouse down but calling preventDefault prevents this
Expand All @@ -18,6 +22,8 @@ class Suggestions extends React.Component {
return null
}

const SuggestionComponent = this.props.suggestionComponent || DefaultSuggestionComponent

const options = this.props.options.map((item, index) => {
const key = `${this.props.id}-${index}`
const classNames = []
Expand All @@ -38,7 +44,7 @@ class Suggestions extends React.Component {
className={classNames.join(' ')}
aria-disabled={item.disabled === true}
onMouseDown={this.onMouseDown.bind(this, item)}>
<span dangerouslySetInnerHTML={{ __html: markIt(item.name, this.props.query) }} />
<SuggestionComponent item={item} query={this.props.query} />
</li>
)
})
Expand Down
19 changes: 19 additions & 0 deletions spec/ReactTags.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,25 @@ describe('React Tags', () => {
expect(input.value).toEqual('')
expect(document.activeElement).toEqual(input)
})

it('can render a custom suggestion component when provided', () => {
const CustomSuggestion = ({ item, query }) => (
React.createElement(
'div', { className: 'custom-suggestion' }, item.name)
)

createInstance({
tags: [],
suggestions: fixture,
suggestionComponent: CustomSuggestion
})

type(query)

expect($('ul[role="listbox"]')).toBeTruthy()

expect($$('.custom-suggestion').length).toEqual(3)
})
})

describe('tags', () => {
Expand Down

0 comments on commit c85baf5

Please sign in to comment.