signavio / react-mentions

@mention people in a textarea
https://react-mentions.vercel.app
Other
2.5k stars 575 forks source link

renderSuggestions based on group #731

Open ajay-pal1 opened 9 months ago

ajay-pal1 commented 9 months ago

Is there any way we can render the suggestions based on the group

JefHellemansBlockbax commented 6 months ago

For anyone reading this in the future, unfortunately there is no easy fix with just a prop, but I worked around it by passing this function as customSuggestionsContainer prop:

function renderSuggestions(children: ReactNode) {
    const ul = children as ReactElement;
    const suggestions = ul.props.children as ReactElement[];
    // groupBy is a helper function from lodash
    const groupedSuggestions = groupBy(suggestions, suggestion => suggestion.props.suggestion.group);
    return (
        <div className="suggestionList">
            {Object.entries(groupedSuggestions).map(([group, suggestions]) =>
                cloneElement(ul, { ...ul.props, key: group }, [
                    <li key={group} className="suggestionGroup">
                        {group}
                    </li>,
                    ...suggestions
                ])
            )}
        </div>
    );
}