2016-09-21 11:54:53 +08:00
|
|
|
import React from 'react';
|
2016-07-20 18:56:44 +08:00
|
|
|
import RcMention, { Nav, toString, toEditorState, getMentions } from 'rc-editor-mention';
|
2016-09-28 16:55:14 +08:00
|
|
|
import classNames from 'classnames';
|
2016-11-14 13:20:00 +08:00
|
|
|
import Icon from '../icon';
|
2016-07-04 10:31:27 +08:00
|
|
|
|
|
|
|
export interface MentionProps {
|
|
|
|
prefixCls: string;
|
2016-07-18 17:58:55 +08:00
|
|
|
suggestionStyle?: Object;
|
2016-07-04 10:31:27 +08:00
|
|
|
suggestions?: Array<any>;
|
|
|
|
onSearchChange?: Function;
|
|
|
|
onChange?: Function;
|
|
|
|
notFoundContent?: any;
|
|
|
|
loading?: Boolean;
|
2016-07-05 11:00:52 +08:00
|
|
|
style?: Object;
|
2016-09-19 10:17:07 +08:00
|
|
|
defaultValue?: any;
|
|
|
|
value?: any;
|
2016-07-18 17:58:55 +08:00
|
|
|
className?: string;
|
2016-07-04 10:31:27 +08:00
|
|
|
multiLines?: Boolean;
|
2016-09-28 16:55:14 +08:00
|
|
|
prefix?: string;
|
2016-09-30 11:26:27 +08:00
|
|
|
placeholder?: string;
|
2016-10-31 15:39:03 +08:00
|
|
|
getSuggestionContainer?: Function;
|
2016-12-09 13:03:09 +08:00
|
|
|
onFocus?: Function;
|
|
|
|
onBlur?: Function;
|
2016-07-04 10:31:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface MentionState {
|
|
|
|
suggestions?: Array<any>;
|
|
|
|
focus?: Boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Mention extends React.Component<MentionProps, MentionState> {
|
|
|
|
static Nav = Nav;
|
2016-07-19 12:31:03 +08:00
|
|
|
static toString = toString;
|
2016-07-19 13:39:05 +08:00
|
|
|
static toEditorState = toEditorState;
|
2016-07-20 18:56:44 +08:00
|
|
|
static getMentions = getMentions;
|
2016-07-04 10:31:27 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-mention',
|
|
|
|
notFoundContent: '无匹配结果,轻敲空格完成输入',
|
|
|
|
loading: false,
|
|
|
|
multiLines: false,
|
|
|
|
};
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
suggestions: props.suggestions,
|
|
|
|
focus: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
this.setState({
|
|
|
|
suggestions: nextProps.suggestions,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-14 12:21:58 +08:00
|
|
|
onSearchChange = (value) => {
|
2016-07-04 10:31:27 +08:00
|
|
|
if (this.props.onSearchChange) {
|
|
|
|
return this.props.onSearchChange(value);
|
|
|
|
}
|
|
|
|
return this.defaultSearchChange(value);
|
|
|
|
}
|
|
|
|
|
2016-11-14 12:21:58 +08:00
|
|
|
onChange = (editorState) => {
|
2016-07-04 10:31:27 +08:00
|
|
|
if (this.props.onChange) {
|
2016-07-19 12:31:03 +08:00
|
|
|
this.props.onChange(editorState);
|
2016-07-04 10:31:27 +08:00
|
|
|
}
|
|
|
|
}
|
2016-07-18 17:58:55 +08:00
|
|
|
|
2016-07-04 10:31:27 +08:00
|
|
|
defaultSearchChange(value: String): void {
|
|
|
|
const searchValue = value.toLowerCase();
|
2016-10-24 12:04:26 +08:00
|
|
|
const filteredSuggestions = (this.props.suggestions || []).filter(
|
2016-07-04 10:31:27 +08:00
|
|
|
suggestion => suggestion.toLowerCase().indexOf(searchValue) !== -1
|
|
|
|
);
|
|
|
|
this.setState({
|
|
|
|
suggestions: filteredSuggestions,
|
|
|
|
});
|
|
|
|
}
|
2016-12-09 13:03:09 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 10:31:27 +08:00
|
|
|
render() {
|
2016-11-14 12:21:58 +08:00
|
|
|
const { className = '', prefixCls, loading } = this.props;
|
2016-07-04 10:31:27 +08:00
|
|
|
const { suggestions, focus } = this.state;
|
2016-11-30 10:20:23 +08:00
|
|
|
const cls = classNames(className, {
|
2016-11-14 12:21:58 +08:00
|
|
|
[`${prefixCls}-active`]: focus,
|
2016-07-04 10:31:27 +08:00
|
|
|
});
|
|
|
|
|
2016-11-14 12:21:58 +08:00
|
|
|
const notFoundContent = loading
|
|
|
|
? <Icon type="loading" />
|
|
|
|
: this.props.notFoundContent;
|
2016-07-18 17:58:55 +08:00
|
|
|
|
2016-11-14 12:21:58 +08:00
|
|
|
return (
|
|
|
|
<RcMention
|
2016-07-04 10:31:27 +08:00
|
|
|
{...this.props}
|
|
|
|
className={cls}
|
2016-11-14 12:21:58 +08:00
|
|
|
onSearchChange={this.onSearchChange}
|
|
|
|
onChange={this.onChange}
|
2016-12-09 13:03:09 +08:00
|
|
|
onFocus={this.onFocus}
|
|
|
|
onBlur={this.onBlur}
|
2016-07-18 17:58:55 +08:00
|
|
|
suggestions={suggestions}
|
2016-07-04 10:31:27 +08:00
|
|
|
notFoundContent={notFoundContent}
|
2016-11-14 12:21:58 +08:00
|
|
|
/>
|
|
|
|
);
|
2016-07-04 10:31:27 +08:00
|
|
|
}
|
|
|
|
}
|