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';
|
2022-10-19 00:22:39 +08:00
|
|
|
import type {
|
2023-01-20 11:03:50 +08:00
|
|
|
DataDrivenOptionProps as MentionsOptionProps,
|
2022-10-19 00:22:39 +08:00
|
|
|
MentionsProps as RcMentionsProps,
|
|
|
|
MentionsRef as RcMentionsRef,
|
|
|
|
} from 'rc-mentions/lib/Mentions';
|
2020-11-21 15:40:06 +08:00
|
|
|
import { composeRef } from 'rc-util/lib/ref';
|
2022-03-25 14:26:09 +08:00
|
|
|
// eslint-disable-next-line import/no-named-as-default
|
2023-05-06 15:49:37 +08:00
|
|
|
import * as React from 'react';
|
2023-01-20 11:03:50 +08:00
|
|
|
import genPurePanel from '../_util/PurePanel';
|
2022-04-06 21:49:30 +08:00
|
|
|
import type { InputStatus } from '../_util/statusUtils';
|
|
|
|
import { getMergedStatus, getStatusClassNames } from '../_util/statusUtils';
|
2022-11-22 16:47:28 +08:00
|
|
|
import warning from '../_util/warning';
|
2023-03-30 14:55:34 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
|
|
|
import { FormItemInputContext } from '../form/context';
|
|
|
|
import Spin from '../spin';
|
2019-05-17 12:05:03 +08:00
|
|
|
|
2022-06-21 19:40:22 +08:00
|
|
|
import useStyle from './style';
|
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';
|
|
|
|
|
2022-12-25 18:37:57 +08:00
|
|
|
export type { DataDrivenOptionProps as MentionsOptionProps } from 'rc-mentions/lib/Mentions';
|
2022-11-21 21:34:23 +08:00
|
|
|
|
2019-05-17 12:05:03 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-11 14:18:13 +08:00
|
|
|
export interface MentionProps extends Omit<RcMentionsProps, 'suffix'> {
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName?: string;
|
2019-05-17 12:05:03 +08:00
|
|
|
loading?: boolean;
|
2022-02-16 21:14:05 +08:00
|
|
|
status?: InputStatus;
|
2022-11-21 21:34:23 +08:00
|
|
|
options?: MentionsOptionProps[];
|
2022-08-19 17:40:22 +08:00
|
|
|
popupClassName?: string;
|
2019-05-17 12:05:03 +08:00
|
|
|
}
|
|
|
|
|
2022-10-19 00:22:39 +08:00
|
|
|
export interface MentionsRef extends RcMentionsRef {}
|
|
|
|
|
2019-05-17 12:05:03 +08:00
|
|
|
interface MentionsConfig {
|
|
|
|
prefix?: string | string[];
|
|
|
|
split?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface MentionsEntity {
|
|
|
|
prefix: string;
|
|
|
|
value: string;
|
|
|
|
}
|
|
|
|
|
2022-11-19 16:56:23 +08:00
|
|
|
type CompoundedComponent = React.ForwardRefExoticComponent<
|
|
|
|
MentionProps & React.RefAttributes<MentionsRef>
|
|
|
|
> & {
|
2020-05-26 22:29:27 +08:00
|
|
|
Option: typeof Option;
|
2022-07-12 20:46:27 +08:00
|
|
|
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
|
2020-05-26 22:29:27 +08:00
|
|
|
getMentions: (value: string, config?: MentionsConfig) => MentionsEntity[];
|
2022-11-19 16:56:23 +08:00
|
|
|
};
|
2019-08-08 11:11:28 +08:00
|
|
|
|
2022-10-19 00:22:39 +08:00
|
|
|
const InternalMentions: React.ForwardRefRenderFunction<MentionsRef, MentionProps> = (
|
2020-05-26 22:29:27 +08:00
|
|
|
{
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2020-05-26 22:29:27 +08:00
|
|
|
disabled,
|
|
|
|
loading,
|
|
|
|
filterOption,
|
|
|
|
children,
|
|
|
|
notFoundContent,
|
2022-11-21 21:34:23 +08:00
|
|
|
options,
|
2022-02-16 21:14:05 +08:00
|
|
|
status: customStatus,
|
2022-08-19 17:40:22 +08:00
|
|
|
popupClassName,
|
2023-06-27 10:51:23 +08:00
|
|
|
style,
|
2020-05-26 22:29:27 +08:00
|
|
|
...restProps
|
|
|
|
},
|
|
|
|
ref,
|
|
|
|
) => {
|
|
|
|
const [focused, setFocused] = React.useState(false);
|
2023-03-30 14:55:34 +08:00
|
|
|
const innerRef = React.useRef<MentionsRef>(null);
|
2020-05-26 22:29:27 +08:00
|
|
|
const mergedRef = composeRef(ref, innerRef);
|
2022-11-21 21:34:23 +08:00
|
|
|
|
|
|
|
// =================== Warning =====================
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning(
|
2022-11-22 16:47:28 +08:00
|
|
|
!children,
|
2022-11-21 21:34:23 +08:00
|
|
|
'Mentions',
|
|
|
|
'`Mentions.Option` is deprecated. Please use `options` instead.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-06-27 10:51:23 +08:00
|
|
|
const {
|
|
|
|
getPrefixCls,
|
|
|
|
renderEmpty,
|
|
|
|
direction,
|
|
|
|
mentions: contextMentions,
|
|
|
|
} = React.useContext(ConfigContext);
|
2022-03-25 17:48:12 +08:00
|
|
|
const {
|
|
|
|
status: contextStatus,
|
|
|
|
hasFeedback,
|
|
|
|
feedbackIcon,
|
|
|
|
} = React.useContext(FormItemInputContext);
|
2022-02-16 21:14:05 +08:00
|
|
|
const mergedStatus = getMergedStatus(contextStatus, customStatus);
|
2020-05-26 22:29:27 +08:00
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
2023-01-09 10:04:35 +08:00
|
|
|
const notFoundContentEle = React.useMemo<React.ReactNode>(() => {
|
2019-05-17 12:05:03 +08:00
|
|
|
if (notFoundContent !== undefined) {
|
|
|
|
return notFoundContent;
|
|
|
|
}
|
2023-01-09 10:04:35 +08:00
|
|
|
return renderEmpty?.('Select') || <DefaultRenderEmpty componentName="Select" />;
|
|
|
|
}, [notFoundContent, renderEmpty]);
|
2019-05-17 12:05:03 +08:00
|
|
|
|
2023-03-30 14:55:34 +08:00
|
|
|
const mentionOptions = React.useMemo<React.ReactNode>(() => {
|
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;
|
2023-03-30 14:55:34 +08:00
|
|
|
}, [loading, children]);
|
2019-05-17 12:05:03 +08:00
|
|
|
|
2022-11-22 16:47:28 +08:00
|
|
|
const mergedOptions = loading
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
value: 'ANTD_SEARCHING',
|
|
|
|
disabled: true,
|
|
|
|
label: <Spin size="small" />,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: options;
|
2019-05-17 12:05:03 +08:00
|
|
|
|
2023-01-09 10:04:35 +08:00
|
|
|
const mentionsfilterOption = loading ? loadingFilterOption : filterOption;
|
2019-05-17 12:05:03 +08:00
|
|
|
|
2020-05-26 22:29:27 +08:00
|
|
|
const prefixCls = getPrefixCls('mentions', customizePrefixCls);
|
|
|
|
|
2022-03-21 10:59:42 +08:00
|
|
|
// Style
|
2022-04-06 21:49:30 +08:00
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2022-03-21 10:59:42 +08:00
|
|
|
|
2020-09-06 13:07:39 +08:00
|
|
|
const mergedClassName = classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
[`${prefixCls}-focused`]: focused,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
2022-02-16 21:14:05 +08:00
|
|
|
getStatusClassNames(prefixCls, mergedStatus),
|
2023-06-27 10:51:23 +08:00
|
|
|
contextMentions?.className,
|
2022-02-16 21:14:05 +08:00
|
|
|
!hasFeedback && className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2022-03-21 10:59:42 +08:00
|
|
|
hashId,
|
2020-09-06 13:07:39 +08:00
|
|
|
);
|
2020-05-26 22:29:27 +08:00
|
|
|
|
2022-02-16 21:14:05 +08:00
|
|
|
const mentions = (
|
2020-05-26 22:29:27 +08:00
|
|
|
<RcMentions
|
|
|
|
prefixCls={prefixCls}
|
2023-01-09 10:04:35 +08:00
|
|
|
notFoundContent={notFoundContentEle}
|
2020-05-26 22:29:27 +08:00
|
|
|
className={mergedClassName}
|
|
|
|
disabled={disabled}
|
|
|
|
direction={direction}
|
2023-06-27 10:51:23 +08:00
|
|
|
style={{ ...contextMentions?.style, ...style }}
|
2020-05-26 22:29:27 +08:00
|
|
|
{...restProps}
|
2023-01-09 10:04:35 +08:00
|
|
|
filterOption={mentionsfilterOption}
|
2020-05-26 22:29:27 +08:00
|
|
|
onFocus={onFocus}
|
|
|
|
onBlur={onBlur}
|
2023-01-20 11:03:50 +08:00
|
|
|
dropdownClassName={classNames(popupClassName, rootClassName, hashId)}
|
2023-03-30 14:55:34 +08:00
|
|
|
ref={mergedRef}
|
2022-11-21 21:34:23 +08:00
|
|
|
options={mergedOptions}
|
2023-01-11 14:18:13 +08:00
|
|
|
suffix={hasFeedback && feedbackIcon}
|
2023-03-30 14:55:34 +08:00
|
|
|
classes={{ affixWrapper: classNames(hashId, className) }}
|
2020-05-26 22:29:27 +08:00
|
|
|
>
|
2023-03-30 14:55:34 +08:00
|
|
|
{mentionOptions}
|
2020-05-26 22:29:27 +08:00
|
|
|
</RcMentions>
|
|
|
|
);
|
2022-02-16 21:14:05 +08:00
|
|
|
|
2022-03-21 10:59:42 +08:00
|
|
|
return wrapSSR(mentions);
|
2020-05-26 22:29:27 +08:00
|
|
|
};
|
|
|
|
|
2022-10-19 00:22:39 +08:00
|
|
|
const Mentions = React.forwardRef<MentionsRef, MentionProps>(
|
|
|
|
InternalMentions,
|
|
|
|
) as CompoundedComponent;
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Mentions.displayName = 'Mentions';
|
|
|
|
}
|
2020-05-26 22:29:27 +08:00
|
|
|
Mentions.Option = Option;
|
|
|
|
|
2022-07-12 20:46:27 +08:00
|
|
|
// We don't care debug panel
|
2023-06-07 21:59:21 +08:00
|
|
|
/* istanbul ignore next */
|
2022-07-12 20:46:27 +08:00
|
|
|
const PurePanel = genPurePanel(Mentions, 'mentions');
|
|
|
|
Mentions._InternalPanelDoNotUseOrYouWillBeFired = PurePanel;
|
|
|
|
|
2021-11-26 12:18:21 +08:00
|
|
|
Mentions.getMentions = (value: string = '', config: MentionsConfig = {}): MentionsEntity[] => {
|
|
|
|
const { prefix = '@', split = ' ' } = config;
|
2020-05-26 22:29:27 +08:00
|
|
|
const prefixList: string[] = Array.isArray(prefix) ? prefix : [prefix];
|
|
|
|
|
|
|
|
return value
|
|
|
|
.split(split)
|
|
|
|
.map((str = ''): MentionsEntity | null => {
|
|
|
|
let hitPrefix: string | null = null;
|
|
|
|
|
2022-11-21 21:34:23 +08:00
|
|
|
prefixList.some((prefixStr) => {
|
2020-05-26 22:29:27 +08:00
|
|
|
const startStr = str.slice(0, prefixStr.length);
|
|
|
|
if (startStr === prefixStr) {
|
|
|
|
hitPrefix = prefixStr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (hitPrefix !== null) {
|
|
|
|
return {
|
|
|
|
prefix: hitPrefix,
|
2021-05-27 15:18:59 +08:00
|
|
|
value: str.slice((hitPrefix as string).length),
|
2020-05-26 22:29:27 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
.filter((entity): entity is MentionsEntity => !!entity && !!entity.value);
|
|
|
|
};
|
2019-05-17 12:05:03 +08:00
|
|
|
|
|
|
|
export default Mentions;
|