bvaughn / react-highlight-words

React component to highlight words within a larger body of text
https://bvaughn.github.io/react-highlight-words/
MIT License
2.16k stars 170 forks source link

Add `highlightWrapper` which accepts a function to wrap the highlighted text in. #33

Closed camden closed 7 years ago

camden commented 7 years ago

For example, we can pass in the following function:

({ children }) => {
  return (
    <div onClick={doSomething()}>{children}</div>
  );
}

This would enable custom behavior to be added to highlighted words.

bvaughn commented 7 years ago

Hi @camden. Sorry for somehow missing this PR. This isn't a super active project for me and...it happens.

I'm not a huge fan of the fact that this change would expand the API to define both a highlightTag (eg "div") and what is essentially a render-prop for highlights. These 2 strongly overlap.

I propose (and will go ahead and make this change for 0.9.0, which I'll be releasing momentarily) changing the highlightTag param to accept React elements in addition to strings. Then you can pass in:

const HighlightTag = ({ children, ...rest }) => (
  <div
    onClick={doSomething}
    {...rest /* className, style, etc */}
  >
    {children}
  </div>
)
camden commented 7 years ago

Looks great. Thanks @bvaughn !

bvaughn commented 7 years ago

You're welcome 😄