ant-design/components/mention/index.tsx
vagusX 149729e524
use ant design icons 4.0 (#18217)
* feat: use @ant-design/icons@4.0

* feat: use createFromIconfontCN to make site works

* feat: update doc for Icon

* feat: use icon in component Alert

* feat: use icon in component Avatar

* feat: use icon in component Breadcrumb

* feat: use icon in component Button

* feat: use icon in component Cascader

* feat: use icon in component Collapse

* feat: use icon in component Datepicker

* feat: use icon in component Dropdown

* feat: use icon in component Form

* feat: use icon in component Input

* feat: use icon in component InputNumber

* feat: use icon in component Layout

* feat: use icon in component Mention

* feat: use icon in component Message

* feat: use icon in component Modal

* feat: use icon in component Notification

* feat: use icon in component PageHeader

* feat: use icon in component Pagination

* feat: use icon in component Popconfirm

* feat: use icon in component Progress

* feat: use icon in component Rate

* feat: use icon in component Result

* feat: use icon in component Select

* feat: use icon in component Step

* feat: use icon in component Switch

* feat: use icon in component Table

* feat: use icon in component Tab

* feat: use icon in component Tag

* feat: handle rest component which using Icon

* fix: remove unused vars

* feat: use latest alpha ant design icons

* fix: failed test in uploadlist.test.js

* test: update snapshot for icons

* doc: add Icon for site

* doc: use @ant-design/icons in site

* chore: use latest icons

* fix: tslint issue

* fix: test cases

* fix: types for react

* fix: lint rules for import orders

* fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render

* fix: eslint error in demo/**.md

* inject antd icons

* update snapshot

* fix site

* doc: update docs

* fix: code snippets icon in site

* feat: use latest @ant-design/icons

* fix: icon props in message
2019-08-13 14:07:17 +08:00

173 lines
4.4 KiB
TypeScript

import * as React from 'react';
import RcMention, { Nav, toString, toEditorState, getMentions } from 'rc-editor-mention';
import { polyfill } from 'react-lifecycles-compat';
import classNames from 'classnames';
import { Loading } from '@ant-design/icons';
import warning from '../_util/warning';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
export type MentionPlacement = 'top' | 'bottom';
type SuggestionItme = React.ReactElement<{ value?: string }> | string;
export interface MentionProps {
prefixCls?: string;
suggestionStyle?: React.CSSProperties;
defaultSuggestions?: Array<SuggestionItme>;
suggestions?: Array<React.ReactElement<any>>;
onSearchChange?: (value: string, trigger: string) => any;
onChange?: (contentState: any) => void;
notFoundContent?: any;
loading?: boolean;
style?: React.CSSProperties;
defaultValue?: any;
value?: any;
className?: string;
multiLines?: boolean;
prefix?: string | string[];
placeholder?: string;
getSuggestionContainer?: (triggerNode: Element) => HTMLElement;
onFocus?: React.FocusEventHandler<HTMLElement>;
onBlur?: React.FocusEventHandler<HTMLElement>;
onSelect?: (suggestion: string, data?: any) => void;
readOnly?: boolean;
disabled?: boolean;
placement?: MentionPlacement;
}
export interface MentionState {
filteredSuggestions?: Array<any>;
focus?: Boolean;
}
class Mention extends React.Component<MentionProps, MentionState> {
static getMentions = getMentions;
static defaultProps = {
notFoundContent: 'No matches found',
loading: false,
multiLines: false,
placement: 'bottom' as MentionPlacement,
};
static Nav = Nav;
static toString = toString;
static toContentState = toEditorState;
private mentionEle: any;
constructor(props: MentionProps) {
super(props);
this.state = {
filteredSuggestions: props.defaultSuggestions,
focus: false,
};
warning(
false,
'Mention',
'Mention component is deprecated. Please use Mentions component instead.',
);
}
mentionRef = (ele: any) => {
this.mentionEle = ele;
};
onSearchChange = (value: string, prefix: string) => {
if (this.props.onSearchChange) {
return this.props.onSearchChange(value, prefix);
}
return this.defaultSearchChange(value);
};
onChange = (editorState: any) => {
if (this.props.onChange) {
this.props.onChange(editorState);
}
};
onFocus = (ev: React.FocusEvent<HTMLElement>) => {
this.setState({
focus: true,
});
if (this.props.onFocus) {
this.props.onFocus(ev);
}
};
onBlur = (ev: React.FocusEvent<HTMLElement>) => {
this.setState({
focus: false,
});
if (this.props.onBlur) {
this.props.onBlur(ev);
}
};
focus = () => {
this.mentionEle._editor.focusEditor();
};
defaultSearchChange(value: string): void {
const searchValue = value.toLowerCase();
const filteredSuggestions = (this.props.defaultSuggestions || []).filter(suggestion => {
if (typeof suggestion === 'string') {
return suggestion.toLowerCase().indexOf(searchValue) !== -1;
}
if (suggestion.type && suggestion.type === Nav) {
return suggestion.props.value
? suggestion.props.value.toLowerCase().indexOf(searchValue) !== -1
: true;
}
return false;
});
this.setState({
filteredSuggestions,
});
}
renderMention = ({ getPrefixCls }: ConfigConsumerProps) => {
const {
prefixCls: customizePrefixCls,
className = '',
loading,
placement,
suggestions,
} = this.props;
const { filteredSuggestions, focus } = this.state;
const prefixCls = getPrefixCls('mention', customizePrefixCls);
const cls = classNames(className, {
[`${prefixCls}-active`]: focus,
[`${prefixCls}-placement-top`]: placement === 'top',
});
const notFoundContent = loading ? <Loading /> : this.props.notFoundContent;
return (
<RcMention
{...this.props}
prefixCls={prefixCls}
className={cls}
ref={this.mentionRef}
onSearchChange={this.onSearchChange}
onChange={this.onChange}
onFocus={this.onFocus}
onBlur={this.onBlur}
suggestions={suggestions || filteredSuggestions}
notFoundContent={notFoundContent}
/>
);
};
render() {
return <ConfigConsumer>{this.renderMention}</ConfigConsumer>;
}
}
polyfill(Mention);
export default Mention;