2019-05-17 12:05:03 +08:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import omit from 'omit.js';
|
|
|
|
import * as React from 'react';
|
|
|
|
import RcMentions from 'rc-mentions';
|
|
|
|
import { MentionsProps as RcMentionsProps } from 'rc-mentions/lib/Mentions';
|
|
|
|
import Spin from '../spin';
|
|
|
|
import { ConfigConsumer, ConfigConsumerProps, RenderEmptyHandler } from '../config-provider';
|
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
const { Option } = RcMentions;
|
2019-05-17 12:05:03 +08:00
|
|
|
|
|
|
|
function loadingFilterOption() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type MentionPlacement = 'top' | 'bottom';
|
|
|
|
|
|
|
|
export interface OptionProps {
|
|
|
|
value: string;
|
|
|
|
children: React.ReactNode;
|
2019-06-24 11:29:58 +08:00
|
|
|
[key: string]: any;
|
2019-05-17 12:05:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface MentionProps extends RcMentionsProps {
|
|
|
|
loading?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MentionState {
|
|
|
|
focused: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface MentionsConfig {
|
|
|
|
prefix?: string | string[];
|
|
|
|
split?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface MentionsEntity {
|
|
|
|
prefix: string;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Mentions extends React.Component<MentionProps, MentionState> {
|
|
|
|
static Option = Option;
|
|
|
|
|
|
|
|
static getMentions = (value: string = '', config?: MentionsConfig): MentionsEntity[] => {
|
|
|
|
const { prefix = '@', split = ' ' } = config || {};
|
|
|
|
const prefixList: string[] = Array.isArray(prefix) ? prefix : [prefix];
|
|
|
|
|
|
|
|
return value
|
|
|
|
.split(split)
|
2019-06-16 20:51:47 +08:00
|
|
|
.map((str = ''): MentionsEntity | null => {
|
|
|
|
let hitPrefix: string | null = null;
|
2019-05-17 12:05:03 +08:00
|
|
|
|
2019-06-16 20:51:47 +08:00
|
|
|
prefixList.some(prefixStr => {
|
|
|
|
const startStr = str.slice(0, prefixStr.length);
|
|
|
|
if (startStr === prefixStr) {
|
|
|
|
hitPrefix = prefixStr;
|
|
|
|
return true;
|
2019-05-17 12:05:03 +08:00
|
|
|
}
|
2019-06-16 20:51:47 +08:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (hitPrefix !== null) {
|
|
|
|
return {
|
|
|
|
prefix: hitPrefix,
|
|
|
|
value: str.slice(hitPrefix!.length),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
2019-05-17 12:05:03 +08:00
|
|
|
.filter((entity): entity is MentionsEntity => !!entity && !!entity.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
focused: false,
|
|
|
|
};
|
|
|
|
|
2019-08-08 11:11:28 +08:00
|
|
|
private rcMentions: any;
|
|
|
|
|
2019-05-17 12:05:03 +08:00
|
|
|
onFocus: React.FocusEventHandler<HTMLTextAreaElement> = (...args) => {
|
|
|
|
const { onFocus } = this.props;
|
|
|
|
if (onFocus) {
|
|
|
|
onFocus(...args);
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
focused: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onBlur: React.FocusEventHandler<HTMLTextAreaElement> = (...args) => {
|
|
|
|
const { onBlur } = this.props;
|
|
|
|
if (onBlur) {
|
|
|
|
onBlur(...args);
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
focused: false,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
getNotFoundContent(renderEmpty: RenderEmptyHandler) {
|
|
|
|
const { notFoundContent } = this.props;
|
|
|
|
if (notFoundContent !== undefined) {
|
|
|
|
return notFoundContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderEmpty('Select');
|
|
|
|
}
|
|
|
|
|
|
|
|
getOptions = () => {
|
|
|
|
const { children, loading } = this.props;
|
|
|
|
if (loading) {
|
|
|
|
return (
|
2019-08-05 18:38:10 +08:00
|
|
|
<Option value="ANTD_SEARCHING" disabled>
|
2019-05-17 12:05:03 +08:00
|
|
|
<Spin size="small" />
|
|
|
|
</Option>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return children;
|
|
|
|
};
|
|
|
|
|
2019-05-27 14:43:15 +08:00
|
|
|
getFilterOption = (): any => {
|
2019-05-17 12:05:03 +08:00
|
|
|
const { filterOption, loading } = this.props;
|
|
|
|
if (loading) {
|
|
|
|
return loadingFilterOption;
|
|
|
|
}
|
|
|
|
return filterOption;
|
|
|
|
};
|
|
|
|
|
2019-08-08 11:11:28 +08:00
|
|
|
saveMentions = (node: typeof RcMentions) => {
|
|
|
|
this.rcMentions = node;
|
|
|
|
};
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
this.rcMentions.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
|
|
|
this.rcMentions.blur();
|
|
|
|
}
|
|
|
|
|
2020-04-09 12:39:10 +08:00
|
|
|
renderMentions = ({ getPrefixCls, renderEmpty, direction }: ConfigConsumerProps) => {
|
2019-05-17 12:05:03 +08:00
|
|
|
const { focused } = this.state;
|
|
|
|
const { prefixCls: customizePrefixCls, className, disabled, ...restProps } = this.props;
|
|
|
|
const prefixCls = getPrefixCls('mentions', customizePrefixCls);
|
|
|
|
const mentionsProps = omit(restProps, ['loading']);
|
|
|
|
|
|
|
|
const mergedClassName = classNames(className, {
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
[`${prefixCls}-focused`]: focused,
|
2020-04-09 12:39:10 +08:00
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
2019-05-17 12:05:03 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RcMentions
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
notFoundContent={this.getNotFoundContent(renderEmpty)}
|
|
|
|
className={mergedClassName}
|
|
|
|
disabled={disabled}
|
2020-04-09 13:18:49 +08:00
|
|
|
direction={direction}
|
2019-05-17 12:05:03 +08:00
|
|
|
{...mentionsProps}
|
|
|
|
filterOption={this.getFilterOption()}
|
|
|
|
onFocus={this.onFocus}
|
|
|
|
onBlur={this.onBlur}
|
2019-08-08 11:11:28 +08:00
|
|
|
ref={this.saveMentions}
|
2019-05-17 12:05:03 +08:00
|
|
|
>
|
|
|
|
{this.getOptions()}
|
|
|
|
</RcMentions>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <ConfigConsumer>{this.renderMentions}</ConfigConsumer>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Mentions;
|