Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help integrating @draft-js-plugins/mention #440

Open
vmc08 opened this issue Mar 30, 2021 · 2 comments
Open

Need help integrating @draft-js-plugins/mention #440

vmc08 opened this issue Mar 30, 2021 · 2 comments
Labels
question Further information is requested

Comments

@vmc08
Copy link

vmc08 commented Mar 30, 2021

Hi, thanks for creating this great library but I need help integrating @draft-js-plugins/mention.

Library versions, if this helps

"@draft-js-plugins/editor": "^4.1.0",
"@draft-js-plugins/mention": "^4.3.1",
"draft-js": "^0.10.5",
"draftail": "^1.3.0",

have to use 0.10.5 for draft-js so draftail will work as expected (also stated in draftail doc)

Issue

I just followed the documentation in the mentions plugin but this is the result.

draftail-issue

Editor Component

import React, { useState, useCallback, memo } from 'react'
import { DraftailEditor } from 'draftail'
import { defaultSuggestionsFilter } from '@draft-js-plugins/mention';
import { MentionData } from '@draft-js-plugins/mention/lib';

import usePlugins from './hooks/usePlugins'
import { StyledEditorWrapper } from './styled'

import "draft-js/dist/Draft.css"
import "draftail/dist/draftail.css"
import '@draft-js-plugins/mention/lib/plugin.css'

interface IDraftail {
  mentionSuggestions?: MentionData[]
}

const Draftail: React.FC<IDraftail> = memo(({
  mentionSuggestions = [],
}) => {
  const [openState, setOpenState] = useState(false)
  const [suggestions, setSuggestions] = useState<MentionData[]>(mentionSuggestions)

  const onMentionSearchChange = useCallback(({ value }: { value: string }) => {
    const filteredMentions = defaultSuggestionsFilter(value, mentionSuggestions)
    setSuggestions(filteredMentions);
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [mentionSuggestions]);

  const { plugins, MentionSuggestions } = usePlugins()

  return (
    <StyledEditorWrapper id="identifi-editor">
      <DraftailEditor
        plugins={plugins}
      />
      {Boolean(mentionSuggestions.length) && (
        <MentionSuggestions
          open={openState}
          onOpenChange={nextOpenState => setOpenState(nextOpenState)}
          suggestions={suggestions}
          onSearchChange={onMentionSearchChange}
          onAddMention={(_: MentionData) => setOpenState(false)}
        />
      )}
    </StyledEditorWrapper>
  )
})

export default Draftail

Plugins Hook

import { useMemo } from 'react'
import createMentionPlugin from '@draft-js-plugins/mention';

export default () => {
  const pluginsObject = useMemo(() => {
    const mentionPlugin = createMentionPlugin()
    const { MentionSuggestions } = mentionPlugin
    const plugins = [
      mentionPlugin,
    ]
    return {
      plugins,
      MentionSuggestions,
    }
  }, [])

  return pluginsObject
}

I'm noob at draft-js, I hope you guys can point out what I'm missing or what I did wrong.

@vmc08 vmc08 added the question Further information is requested label Mar 30, 2021
@thibaudcolas
Copy link
Collaborator

@vmc08 I think the first thing that’s off is that you’re using "@draft-js-plugins/editor": "^4.1.0", whereas internally Draftail uses "draft-js-plugins-editor": "^2.1.1",. There’s going to be a lot of things that are potentially off under the hood with such a wide discrepancy in version ranges. You also shouldn’t need to install the plugins editor package directly, since that’s a dependency of Draftail.

So I would recommend trying with a version of the mentions plugin that matches the editor’s version. I haven’t used the mentions plugin myself otherwise, so can’t comment to how it’s meant to be used or what to expect of it.

@vmc08
Copy link
Author

vmc08 commented Apr 14, 2021

Hi @thibaudcolas , thanks for your reply. I'll try to do that. Will give update of the result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants