signavio / react-mentions

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

renderSuggestions based on group #731

Open ajay-pal1 opened 5 months ago

ajay-pal1 commented 5 months ago

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

JefHellemansBlockbax commented 2 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>
    );
}