1+ import React from "react" ;
2+ import { InputSuggestion , TextInput , TextInputProps } from "../form" ;
3+ import { InputSyntaxSegment } from "../form/Input.syntax.hook" ;
4+ import { Badge } from "../badge/Badge" ;
5+ import { Text } from "../text/Text" ;
6+ import { useService , useStore } from "../../utils" ;
7+ import { DUserReactiveService } from "./DUser.service" ;
8+ import { MenuItem } from "../menu/Menu" ;
9+
10+ export interface DUserInputProps extends TextInputProps {
11+
12+ }
13+
14+ export const DUserInput : React . FC < DUserInputProps > = ( props ) => {
15+
16+ const { ...rest } = props
17+
18+ const userService = useService ( DUserReactiveService )
19+ const userStore = useStore ( DUserReactiveService )
20+ const suggestions : InputSuggestion [ ] = React . useMemo ( ( ) => {
21+ return userService . values ( ) . map ( user => ( {
22+ value : user . username || "" ,
23+ children : user . username ,
24+ insertMode : "insert" ,
25+ valueData : user
26+ } ) )
27+ } , [ userStore ] )
28+
29+ const transformSyntax = (
30+ _ ?: string | null ,
31+ appliedParts : ( InputSuggestion | any ) [ ] = [ ] ,
32+ ) : InputSyntaxSegment [ ] => {
33+
34+ let cursor = 0
35+
36+ return appliedParts . map ( ( part : string | InputSuggestion , index ) => {
37+ if ( typeof part === "object" ) {
38+ const segment = {
39+ type : "block" ,
40+ start : cursor ,
41+ end : cursor + part . value . length ,
42+ visualLength : 1 ,
43+ content : < Badge color = { "info" } border >
44+ < Text style = { { color : "inherit" } } >
45+ @{ part . value }
46+ </ Text >
47+ </ Badge > ,
48+ }
49+ cursor += part . value . length
50+ return segment
51+ }
52+ const textString = part ?? ""
53+ if ( ! textString . length ) return
54+
55+ if ( index == appliedParts . length - 1 ) {
56+ const segment = {
57+ type : "text" ,
58+ start : cursor ,
59+ end : cursor + textString . length ,
60+ visualLength : textString . length ,
61+ content : textString ,
62+ }
63+ cursor += textString . length
64+ return segment
65+ }
66+ cursor += textString . length
67+ return { }
68+ } ) as InputSyntaxSegment [ ]
69+ }
70+
71+ return < TextInput placeholder = { "Enter users" } suggestionsEmptyState = { < MenuItem > < Text > No user found</ Text > </ MenuItem > } filterSuggestionsByLastToken enforceUniqueSuggestions transformSyntax = { transformSyntax } { ...rest } suggestions = { suggestions } />
72+ }
0 commit comments