Draft-js-plugins: Linkify and react-router

Created on 30 May 2016  路  3Comments  路  Source: draft-js-plugins/draft-js-plugins

How do you integrate the linkify plugin with react-router's <Link> component? Ideally, I'd like to have relative links use <Link> (routes in the app), and external links to use <a>.

enhancement question

Most helpful comment

I think we are considering allowing the use of your own component for the Linkify plugin. Take a look at issue https://github.com/draft-js-plugins/draft-js-plugins/pull/271

All 3 comments

I think we are considering allowing the use of your own component for the Linkify plugin. Take a look at issue https://github.com/draft-js-plugins/draft-js-plugins/pull/271

@jamesfzhang @adrianmcli Took a bit of digging but here's what I got:

import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import { fromJS } from 'immutable';
import _ from 'lodash';

function getMentionLink (arrays) {
    const mention = _.chain(arrays).map((array) => {
        const object = {};
        const key = array[0];
        const value = array[1];

        object[key] = value;

        return object;
    }).flatten().find('link').value();

    return mention.link;
}

const mentionComponent = ({ entityKey, mention, className, decoratedText }) => (
    <Link className={className} to={getMentionLink(mention._root.entries)}>{decoratedText}</Link>
);

function UserSuggestions ({ firstName, lastName, objectId, photo }) {
    const name = `${firstName} ${lastName}`;
    const link = `/profile/${objectId}`;
    const avatar = photo;

    return { name, link, avatar };
}

const mentionSuggestions = fromJS(props.users.map(UserSuggestions));
const mentionPlugin = createMentionPlugin({ mentionComponent });

custom mention components are supported, please refer to the docs.

I'm closing this issue, if you think this issue is still relevant please let us know why and we'll consider reopening it. Thank you for contributing 鉁岋笍

Was this page helpful?
0 / 5 - 0 ratings

Related issues

berdof picture berdof  路  5Comments

sessionboy picture sessionboy  路  3Comments

bojoyzhou picture bojoyzhou  路  3Comments

firashamila33 picture firashamila33  路  4Comments

albertomr86 picture albertomr86  路  4Comments