Answer the question
In order to leave comments, you need to log in
How to implement the selection of the found fragment?
For example, here - https://enkidevs.github.io/react-search-input/
It is
necessary that the search phrase in the found elements be wrapped in a tag <span>{searchTerm}</span>
, for example:
Answer the question
In order to leave comments, you need to log in
Working example (jsfiddle)
The main logic is in the SearchResults component:
class SearchResults extends React.PureComponent {
render() {
const { target, results } = this.props
return (
<div className="SearchResults">
{_
.chain(results)
.map(word => word.split(target))
.map(parts => _.reduce(
parts,
(res, part) => res.length > 0
? [ ...res, <span className="highlight">{target}</span>, part ]
: [ part ]
,
[]
))
.map(word => <span className="word">{word}</span>)
.value()
}
</div>
)
}
}
.map(parts => _.reduce(
parts,
(res, part) => res.length > 0
? [ ...res, <span className="highlight">{target}</span>, part ]
: [ part ]
,
[]
))
.map(word => <span className="word">{word}</span>)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question