-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModal.tsx
More file actions
65 lines (59 loc) · 1.6 KB
/
Copy pathModal.tsx
File metadata and controls
65 lines (59 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import * as React from 'react'
import {useEditor} from 'slate-react'
import useFetchSuggestions from './useFetchSuggestions'
import Loader from './Loader'
import Suggestion, {SuggestionType} from './Suggestion'
import './styles.css'
import {Editor} from 'slate'
export type FetchFN = (mentionAt: string) => Promise<SuggestionType[]>
export type Props = {
fetchSuggestion: FetchFN
}
const Modal: React.FC<Props & {mentionAt: string}> = (props) => {
const editor = useEditor()
const mentionPathRef = React.useMemo(() => {
if (editor.selection) {
return Editor.rangeRef(
editor,
{
anchor: editor.selection.anchor,
focus: editor.selection.focus,
type: 'mention-ref',
},
{
affinity: 'inward',
}
)
}
}, [editor])
console.log({mentionPathRef})
const [loading, suggestions] = useFetchSuggestions(
props.fetchSuggestion,
props.mentionAt
)
const renderSuggestions = React.useCallback(() => {
if (loading) {
return <Loader />
}
if (!loading && suggestions.length === 0) {
return <span className="mention-modal__not-found">not found...</span>
}
return (
<React.Fragment>
{suggestions.map((suggestion) => (
<Suggestion
key={`suggestion-for-${suggestion.label}`}
mentionAt={props.mentionAt}
{...suggestion}
/>
))}
</React.Fragment>
)
}, [suggestions, loading])
return (
<div contentEditable={false} className="mention-modal">
{renderSuggestions()}
</div>
)
}
export default Modal