2023-09-11 17:28:04 +08:00
|
|
|
// eslint-disable-next-line import/no-named-as-default
|
|
|
|
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';
|
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';
|
2023-09-11 17:28:04 +08:00
|
|
|
|
2024-04-08 14:04:08 +08:00
|
|
|
import getAllowClear from '../_util/getAllowClear';
|
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';
|
2023-09-11 17:28:04 +08:00
|
|
|
import { devUseWarning } from '../_util/warning';
|
2023-03-30 14:55:34 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
|
|
|
import DefaultRenderEmpty from '../config-provider/defaultRenderEmpty';
|
2023-11-13 14:43:28 +08:00
|
|
|
import useCSSVarCls from '../config-provider/hooks/useCSSVarCls';
|
2024-04-08 14:04:08 +08:00
|
|
|
import { FormItemInputContext } from '../form/context';
|
2023-12-22 10:05:15 +08:00
|
|
|
import type { Variant } from '../form/hooks/useVariants';
|
|
|
|
import useVariant from '../form/hooks/useVariants';
|
2024-04-08 14:04:08 +08:00
|
|
|
import Spin from '../spin';
|
|
|
|
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;
|
2023-12-18 17:53:31 +08:00
|
|
|
/**
|
|
|
|
* @since 5.13.0
|
|
|
|
* @default "outlined"
|
|
|
|
*/
|
2023-12-22 10:05:15 +08:00
|
|
|
variant?: Variant;
|
2019-05-17 12:05:03 +08:00
|
|
|
}
|
|
|
|
|
2024-06-06 17:09:14 +08:00
|
|
|
export interface MentionsProps extends MentionProps {}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-09 16:49:47 +08:00
|
|
|
const InternalMentions = React.forwardRef<MentionsRef, MentionProps>((props, ref) => {
|
2023-07-11 11:20:11 +08:00
|
|
|
const {
|
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,
|
2023-12-25 10:37:26 +08:00
|
|
|
allowClear = false,
|
2022-08-19 17:40:22 +08:00
|
|
|
popupClassName,
|
2023-06-27 10:51:23 +08:00
|
|
|
style,
|
2023-12-18 17:53:31 +08:00
|
|
|
variant: customVariant,
|
2020-05-26 22:29:27 +08:00
|
|
|
...restProps
|
2023-07-11 11:20:11 +08:00
|
|
|
} = props;
|
2020-05-26 22:29:27 +08:00
|
|
|
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') {
|
2023-09-13 22:07:33 +08:00
|
|
|
const warning = devUseWarning('Mentions');
|
|
|
|
|
|
|
|
warning.deprecated(!children, 'Mentions.Option', 'options');
|
2022-11-21 21:34:23 +08:00
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2023-12-25 10:37:26 +08:00
|
|
|
const mergedAllowClear = getAllowClear(allowClear);
|
|
|
|
|
2022-03-21 10:59:42 +08:00
|
|
|
// Style
|
2023-11-13 14:43:28 +08:00
|
|
|
const rootCls = useCSSVarCls(prefixCls);
|
2023-12-14 14:58:53 +08:00
|
|
|
const [wrapCSSVar, hashId, cssVarCls] = useStyle(prefixCls, rootCls);
|
2022-03-21 10:59:42 +08:00
|
|
|
|
2023-12-22 10:05:15 +08:00
|
|
|
const [variant, enableVariantCls] = useVariant(customVariant);
|
2023-12-18 17:53:31 +08:00
|
|
|
|
|
|
|
// eslint-disable-next-line react/jsx-no-useless-fragment
|
|
|
|
const suffixNode = hasFeedback && <>{feedbackIcon}</>;
|
|
|
|
|
2020-09-06 13:07:39 +08:00
|
|
|
const mergedClassName = classNames(
|
2023-06-27 10:51:23 +08:00
|
|
|
contextMentions?.className,
|
2023-12-18 17:53:31 +08:00
|
|
|
className,
|
2023-01-20 11:03:50 +08:00
|
|
|
rootClassName,
|
2023-12-14 14:58:53 +08:00
|
|
|
cssVarCls,
|
2023-11-13 14:43:28 +08:00
|
|
|
rootCls,
|
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}
|
2023-12-25 10:37:26 +08:00
|
|
|
allowClear={mergedAllowClear}
|
2020-05-26 22:29:27 +08:00
|
|
|
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-12-14 14:58:53 +08:00
|
|
|
dropdownClassName={classNames(popupClassName, rootClassName, hashId, cssVarCls, rootCls)}
|
2023-03-30 14:55:34 +08:00
|
|
|
ref={mergedRef}
|
2022-11-21 21:34:23 +08:00
|
|
|
options={mergedOptions}
|
2023-12-18 17:53:31 +08:00
|
|
|
suffix={suffixNode}
|
|
|
|
classNames={{
|
|
|
|
mentions: classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-disabled`]: disabled,
|
|
|
|
[`${prefixCls}-focused`]: focused,
|
|
|
|
[`${prefixCls}-rtl`]: direction === 'rtl',
|
|
|
|
},
|
|
|
|
hashId,
|
|
|
|
),
|
|
|
|
variant: classNames(
|
|
|
|
{
|
|
|
|
[`${prefixCls}-${variant}`]: enableVariantCls,
|
|
|
|
},
|
|
|
|
getStatusClassNames(prefixCls, mergedStatus),
|
|
|
|
),
|
|
|
|
affixWrapper: hashId,
|
|
|
|
}}
|
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
|
|
|
|
2023-11-13 14:43:28 +08:00
|
|
|
return wrapCSSVar(mentions);
|
2024-04-09 16:49:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
type CompoundedComponent = typeof InternalMentions & {
|
|
|
|
Option: typeof Option;
|
|
|
|
_InternalPanelDoNotUseOrYouWillBeFired: typeof PurePanel;
|
|
|
|
getMentions: (value: string, config?: MentionsConfig) => MentionsEntity[];
|
2020-05-26 22:29:27 +08:00
|
|
|
};
|
|
|
|
|
2024-04-09 16:49:47 +08:00
|
|
|
const Mentions = InternalMentions as CompoundedComponent;
|
|
|
|
|
2022-06-21 10:24:52 +08:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Mentions.displayName = 'Mentions';
|
|
|
|
}
|
2024-04-09 16:49:47 +08:00
|
|
|
|
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;
|