2019-05-17 12:05:03 +08:00
|
|
|
import * as React from 'react';
|
2020-05-26 22:29:27 +08:00
|
|
|
import classNames from 'classnames';
|
2019-05-17 12:05:03 +08:00
|
|
|
import RcMentions from 'rc-mentions';
|
|
|
|
import { MentionsProps as RcMentionsProps } from 'rc-mentions/lib/Mentions';
|
|
|
|
import Spin from '../spin';
|
2020-05-26 22:29:27 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import { composeRef } from '../_util/ref';
|
2019-05-17 12:05:03 +08:00
|
|
|
|
2020-05-26 22:29:27 +08:00
|
|
|
export 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;
|
|
|
|
}
|
|
|
|
|
2020-05-26 22:29:27 +08:00
|
|
|
interface CompoundedComponent
|
|
|
|
extends React.ForwardRefExoticComponent<MentionProps & React.RefAttributes<HTMLElement>> {
|
|
|
|
Option: typeof Option;
|
|
|
|
getMentions: (value: string, config?: MentionsConfig) => MentionsEntity[];
|
|
|
|
}
|
2019-08-08 11:11:28 +08:00
|
|
|
|
2020-05-26 22:29:27 +08:00
|
|
|
const InternalMentions: React.ForwardRefRenderFunction<unknown, MentionProps> = (
|
|
|
|
{
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
|
|
|
disabled,
|
|
|
|
loading,
|
|
|
|
filterOption,
|
|
|
|
children,
|
|
|
|
notFoundContent,
|
|
|
|
...restProps
|
|
|
|
},
|
|
|
|
ref,
|
|
|
|
) => {
|
|
|
|
const [focused, setFocused] = React.useState(false);
|
|
|
|
const innerRef = React.useRef<HTMLElement>();
|
|
|
|
const mergedRef = composeRef(ref, innerRef);
|
|
|
|
const { getPrefixCls, renderEmpty, direction } = React.useContext(ConfigContext);
|
|
|
|
|
|
|
|
const onFocus: React.FocusEventHandler<HTMLTextAreaElement> = (...args) => {
|
|
|
|
if (restProps.onFocus) {
|
|
|
|
restProps.onFocus(...args);
|
2019-05-17 12:05:03 +08:00
|
|
|
}
|
2020-05-26 22:29:27 +08:00
|
|
|
setFocused(true);
|
2019-05-17 12:05:03 +08:00
|
|
|
};
|
|
|
|
|
2020-05-26 22:29:27 +08:00
|
|
|
const onBlur: React.FocusEventHandler<HTMLTextAreaElement> = (...args) => {
|
|
|
|
if (restProps.onBlur) {
|
|
|
|
restProps.onBlur(...args);
|
2019-05-17 12:05:03 +08:00
|
|
|
}
|
2020-05-26 22:29:27 +08:00
|
|
|
|
|
|
|
setFocused(false);
|
2019-05-17 12:05:03 +08:00
|
|
|
};
|
|
|
|
|
2020-05-26 22:29:27 +08:00
|
|
|
const getNotFoundContent = () => {
|
2019-05-17 12:05:03 +08:00
|
|
|
if (notFoundContent !== undefined) {
|
|
|
|
return notFoundContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderEmpty('Select');
|
2020-05-26 22:29:27 +08:00
|
|
|
};
|
2019-05-17 12:05:03 +08:00
|
|
|
|
2020-05-26 22:29:27 +08:00
|
|
|
const getOptions = () => {
|
2019-05-17 12:05:03 +08:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2020-05-26 22:29:27 +08:00
|
|
|
const getFilterOption = (): any => {
|
2019-05-17 12:05:03 +08:00
|
|
|
if (loading) {
|
|
|
|
return loadingFilterOption;
|
|
|
|
}
|
|
|
|
return filterOption;
|
|
|
|
};
|
|
|
|
|
2020-05-26 22:29:27 +08:00
|
|
|
const prefixCls = getPrefixCls('mentions', customizePrefixCls);
|
|
|
|
|
|
|
|
const mergedClassName = classNames(className, {
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
[`${prefixCls}-focused`]: focused,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
});
|
|
|
|
|
|
|
|
return (
|
|
|
|
<RcMentions
|
|
|
|
prefixCls={prefixCls}
|
|
|
|
notFoundContent={getNotFoundContent()}
|
|
|
|
className={mergedClassName}
|
|
|
|
disabled={disabled}
|
|
|
|
direction={direction}
|
|
|
|
{...restProps}
|
|
|
|
filterOption={getFilterOption()}
|
|
|
|
onFocus={onFocus}
|
|
|
|
onBlur={onBlur}
|
|
|
|
ref={mergedRef as any}
|
|
|
|
>
|
|
|
|
{getOptions()}
|
|
|
|
</RcMentions>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const Mentions = React.forwardRef<unknown, MentionProps>(InternalMentions) as CompoundedComponent;
|
|
|
|
Mentions.displayName = 'Mentions';
|
|
|
|
Mentions.Option = Option;
|
|
|
|
|
|
|
|
Mentions.getMentions = (value: string = '', config?: MentionsConfig): MentionsEntity[] => {
|
|
|
|
const { prefix = '@', split = ' ' } = config || {};
|
|
|
|
const prefixList: string[] = Array.isArray(prefix) ? prefix : [prefix];
|
|
|
|
|
|
|
|
return value
|
|
|
|
.split(split)
|
|
|
|
.map((str = ''): MentionsEntity | null => {
|
|
|
|
let hitPrefix: string | null = null;
|
|
|
|
|
|
|
|
prefixList.some(prefixStr => {
|
|
|
|
const startStr = str.slice(0, prefixStr.length);
|
|
|
|
if (startStr === prefixStr) {
|
|
|
|
hitPrefix = prefixStr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (hitPrefix !== null) {
|
|
|
|
return {
|
|
|
|
prefix: hitPrefix,
|
|
|
|
value: str.slice(hitPrefix!.length),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.filter((entity): entity is MentionsEntity => !!entity && !!entity.value);
|
|
|
|
};
|
2019-05-17 12:05:03 +08:00
|
|
|
|
|
|
|
export default Mentions;
|