Skip to content

Commit

Permalink
Custom mention renderer (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeHedman authored Mar 10, 2023
2 parents ed2c88f + df4385c commit 22ac62a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Each data source is configured using a `Mention` component, which has the follow
| regex | RegExp | automatically derived from `markup` pattern | Allows providing a custom regular expression for parsing your markup and extracting the placeholder interpolations (optional) | |
| onAdd | function (id, display, startPos, endPos) | empty function | Callback invoked when a suggestion has been added (optional) |
| appendSpaceOnAdd | boolean | `false` | Append a space when a suggestion has been added (optional) |
| render | function (display) | `null` | Allows customizing how mention value is rendered (optional) |

If a function is passed as the `data` prop, that function will be called with the current search query as first, and a callback function as second argument. The callback can be used to provide results asynchronously, e.g., after fetch requests. (It can even be called multiple times to update the list of suggestions.)

Expand Down
41 changes: 41 additions & 0 deletions demo/src/examples/CustomMentionRenderer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import React from 'react'

import { Mention, MentionsInput } from '../../../src'

import { provideExampleValue } from './higher-order'
import defaultStyle from './defaultStyle'
import defaultMentionStyle from './defaultMentionStyle'

const style = {
fontWeight: 'bold',
backgroundColor: '#cee4e5',
}

function CustomMentionRenderer({ value, data, onChange, onAdd }) {
return (
<div className="single-line">
<h3>Custom mention renderer</h3>

<MentionsInput
singleLine
value={value}
onChange={onChange}
style={defaultStyle}
placeholder={"Mention people using '@'"}
a11ySuggestionsListLabel={"Suggested mentions"}
>
<Mention
data={data}
onAdd={onAdd}
style={defaultMentionStyle}
render={(display) => (
<span style={style}>{display}</span>
)} />
</MentionsInput>
</div>
)
}

const asExample = provideExampleValue('')

export default asExample(CustomMentionRenderer)
7 changes: 6 additions & 1 deletion src/Mention.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ const defaultStyle = {
fontWeight: 'inherit',
}

const Mention = ({ display, style, className, classNames }) => {
const Mention = ({ display, style, className, classNames, render }) => {
if(render) {
return render(display);
}
const styles = useStyles(defaultStyle, { style, className, classNames })
return <strong {...styles}>{display}</strong>
}
Expand Down Expand Up @@ -40,6 +43,7 @@ Mention.propTypes = {
allowSpaceInQuery: PropTypes.bool,

isLoading: PropTypes.bool,
render: PropTypes.func
}

Mention.defaultProps = {
Expand All @@ -53,6 +57,7 @@ Mention.defaultProps = {
renderSuggestion: null,
isLoading: false,
appendSpaceOnAdd: false,
render: null
}

export default Mention

0 comments on commit 22ac62a

Please sign in to comment.