2016-07-04 10:31:27 +08:00
|
|
|
import * as React from 'react';
|
2016-07-19 12:31:03 +08:00
|
|
|
import RcMention, { Nav, toString } from 'rc-editor-mention';
|
2016-07-04 10:31:27 +08:00
|
|
|
import classnames from 'classnames';
|
|
|
|
|
|
|
|
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-07-18 17:58:55 +08:00
|
|
|
defaultValue?: string;
|
|
|
|
className?: string;
|
2016-07-04 10:31:27 +08:00
|
|
|
multiLines?: Boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04 10:31:27 +08:00
|
|
|
static defaultProps = {
|
|
|
|
prefixCls: 'ant-mention',
|
|
|
|
suggestions: [],
|
|
|
|
notFoundContent: '无匹配结果,轻敲空格完成输入',
|
|
|
|
loading: false,
|
|
|
|
multiLines: false,
|
|
|
|
};
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
suggestions: props.suggestions,
|
|
|
|
focus: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
this.setState({
|
|
|
|
suggestions: nextProps.suggestions,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onSearchChange(value) {
|
|
|
|
if (this.props.onSearchChange) {
|
|
|
|
return this.props.onSearchChange(value);
|
|
|
|
}
|
|
|
|
return this.defaultSearchChange(value);
|
|
|
|
}
|
|
|
|
|
2016-07-19 12:31:03 +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-07-18 17:58:55 +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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-07-18 17:58:55 +08:00
|
|
|
const { className, prefixCls, style, multiLines, defaultValue } = this.props;
|
2016-07-04 10:31:27 +08:00
|
|
|
let { notFoundContent } = this.props;
|
2016-07-18 17:58:55 +08:00
|
|
|
|
2016-07-04 10:31:27 +08:00
|
|
|
const { suggestions, focus } = this.state;
|
|
|
|
const cls = classnames({
|
|
|
|
[className]: !!className,
|
|
|
|
['active']: focus,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.props.loading) {
|
|
|
|
notFoundContent = <i className="anticon anticon-loading"></i>;
|
|
|
|
}
|
2016-07-18 17:58:55 +08:00
|
|
|
|
|
|
|
return <RcMention
|
2016-07-04 10:31:27 +08:00
|
|
|
{...this.props}
|
|
|
|
className={cls}
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
style={style}
|
|
|
|
defaultValue={defaultValue}
|
|
|
|
multiLines={multiLines}
|
|
|
|
onSearchChange={this.onSearchChange.bind(this)}
|
|
|
|
onChange={this.onChange.bind(this)}
|
|
|
|
onFocus={() => this.setState({focus: true})}
|
|
|
|
onBlur={() => this.setState({focus: false})}
|
2016-07-18 17:58:55 +08:00
|
|
|
suggestions={suggestions}
|
2016-07-04 10:31:27 +08:00
|
|
|
notFoundContent={notFoundContent}
|
|
|
|
/>;
|
|
|
|
}
|
|
|
|
}
|