You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.5 KiB

import * as React from 'react';
import RcMention, { Nav, toString } from 'rc-editor-mention';
import classnames from 'classnames';
export interface MentionProps {
prefixCls: string;
8 years ago
suggestionStyle?: Object;
suggestions?: Array<any>;
onSearchChange?: Function;
onChange?: Function;
notFoundContent?: any;
loading?: Boolean;
style?: Object;
8 years ago
defaultValue?: string;
className?: string;
multiLines?: Boolean;
}
export interface MentionState {
suggestions?: Array<any>;
focus?: Boolean;
}
export default class Mention extends React.Component<MentionProps, MentionState> {
static Nav = Nav;
static toString = toString;
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);
}
onChange(editorState) {
if (this.props.onChange) {
this.props.onChange(editorState);
}
}
8 years ago
defaultSearchChange(value: String): void {
const searchValue = value.toLowerCase();
8 years ago
const filteredSuggestions = this.props.suggestions.filter(
suggestion => suggestion.toLowerCase().indexOf(searchValue) !== -1
);
this.setState({
suggestions: filteredSuggestions,
});
}
render() {
8 years ago
const { className, prefixCls, style, multiLines, defaultValue } = this.props;
let { notFoundContent } = this.props;
8 years ago
const { suggestions, focus } = this.state;
const cls = classnames({
[className]: !!className,
['active']: focus,
});
if (this.props.loading) {
notFoundContent = <i className="anticon anticon-loading"></i>;
}
8 years ago
return <RcMention
{...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})}
8 years ago
suggestions={suggestions}
notFoundContent={notFoundContent}
/>;
}
}