-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
/
index.tsx
144 lines (134 loc) · 3.57 KB
/
index.tsx
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import React from 'react';
import RcMention, { Nav, toString, toEditorState, getMentions } from 'rc-editor-mention';
import classNames from 'classnames';
import shallowequal from 'shallowequal';
import Icon from '../icon';
export interface MentionProps {
prefixCls?: string;
suggestionStyle?: React.CSSProperties;
suggestions?: Array<any>;
onSearchChange?: Function;
onChange?: Function;
notFoundContent?: any;
loading?: Boolean;
style?: React.CSSProperties;
defaultValue?: any;
value?: any;
className?: string;
multiLines?: Boolean;
prefix?: string;
placeholder?: string;
getSuggestionContainer?: (triggerNode: Element) => HTMLElement;
onFocus?: Function;
onBlur?: Function;
readOnly?: boolean;
disabled?: boolean;
}
export interface MentionState {
suggestions?: Array<any>;
focus?: Boolean;
}
export default class Mention extends React.Component<MentionProps, MentionState> {
static getMentions = getMentions;
static defaultProps = {
prefixCls: 'ant-mention',
notFoundContent: '无匹配结果,轻敲空格完成输入',
loading: false,
multiLines: false,
};
static Nav = Nav;
static toString = toString;
static toContentState = toEditorState;
static toEditorState = text => {
console.warn('Mention.toEditorState is deprecated. Use toContentState instead.');
return toEditorState(text);
}
private mentionEle: any;
constructor(props) {
super(props);
this.state = {
suggestions: props.suggestions,
focus: false,
};
}
componentWillReceiveProps(nextProps: MentionProps) {
const { suggestions } = nextProps;
if (!shallowequal(suggestions, this.props.suggestions)) {
this.setState({
suggestions,
});
}
}
onSearchChange = (value, prefix) => {
if (this.props.onSearchChange) {
return this.props.onSearchChange(value, prefix);
}
return this.defaultSearchChange(value);
}
onChange = (editorState) => {
if (this.props.onChange) {
this.props.onChange(editorState);
}
}
defaultSearchChange(value: String): void {
const searchValue = value.toLowerCase();
const filteredSuggestions = (this.props.suggestions || []).filter(
suggestion => {
if (suggestion.type && suggestion.type === Nav) {
return suggestion.props.value ?
suggestion.props.value.toLowerCase().indexOf(searchValue) !== -1
: true;
}
return suggestion.toLowerCase().indexOf(searchValue) !== -1;
},
);
this.setState({
suggestions: filteredSuggestions,
});
}
onFocus = (ev) => {
this.setState({
focus: true,
});
if (this.props.onFocus) {
this.props.onFocus(ev);
}
}
onBlur = (ev) => {
this.setState({
focus: false,
});
if (this.props.onBlur) {
this.props.onBlur(ev);
}
}
focus = () => {
this.mentionEle._editor.focus();
}
mentionRef = ele => {
this.mentionEle = ele;
}
render() {
const { className = '', prefixCls, loading } = this.props;
const { suggestions, focus } = this.state;
const cls = classNames(className, {
[`${prefixCls}-active`]: focus,
});
const notFoundContent = loading
? <Icon type="loading" />
: this.props.notFoundContent;
return (
<RcMention
{...this.props}
className={cls}
ref={this.mentionRef}
onSearchChange={this.onSearchChange}
onChange={this.onChange}
onFocus={this.onFocus}
onBlur={this.onBlur}
suggestions={suggestions}
notFoundContent={notFoundContent}
/>
);
}
}