ant-design/components/mentions/index.tsx
dependabot[bot] 20d5502193
chore(deps-dev): bump eslint-config-airbnb from 18.2.1 to 19.0.0 (#32824)
* chore(deps-dev): bump eslint-config-airbnb from 18.2.1 to 19.0.0

Bumps [eslint-config-airbnb](https://github.com/airbnb/javascript) from 18.2.1 to 19.0.0.
- [Release notes](https://github.com/airbnb/javascript/releases)
- [Commits](https://github.com/airbnb/javascript/compare/eslint-config-airbnb-v18.2.1...eslint-config-airbnb-v19.0.0)

---
updated-dependencies:
- dependency-name: eslint-config-airbnb
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* chore: code style

* memoize-one

* add comment

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* fix lint

* improve useMemo deps

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: afc163 <afc163@gmail.com>
2021-11-26 12:18:21 +08:00

170 lines
4.0 KiB
TypeScript

import * as React from 'react';
import classNames from 'classnames';
import RcMentions from 'rc-mentions';
import { MentionsProps as RcMentionsProps } from 'rc-mentions/lib/Mentions';
import { composeRef } from 'rc-util/lib/ref';
import Spin from '../spin';
import { ConfigContext } from '../config-provider';
export const { Option } = RcMentions;
function loadingFilterOption() {
return true;
}
export type MentionPlacement = 'top' | 'bottom';
export interface OptionProps {
value: string;
children: React.ReactNode;
[key: string]: any;
}
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;
}
interface CompoundedComponent
extends React.ForwardRefExoticComponent<MentionProps & React.RefAttributes<HTMLElement>> {
Option: typeof Option;
getMentions: (value: string, config?: MentionsConfig) => MentionsEntity[];
}
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);
}
setFocused(true);
};
const onBlur: React.FocusEventHandler<HTMLTextAreaElement> = (...args) => {
if (restProps.onBlur) {
restProps.onBlur(...args);
}
setFocused(false);
};
const getNotFoundContent = () => {
if (notFoundContent !== undefined) {
return notFoundContent;
}
return renderEmpty('Select');
};
const getOptions = () => {
if (loading) {
return (
<Option value="ANTD_SEARCHING" disabled>
<Spin size="small" />
</Option>
);
}
return children;
};
const getFilterOption = (): any => {
if (loading) {
return loadingFilterOption;
}
return filterOption;
};
const prefixCls = getPrefixCls('mentions', customizePrefixCls);
const mergedClassName = classNames(
{
[`${prefixCls}-disabled`]: disabled,
[`${prefixCls}-focused`]: focused,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);
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 as string).length),
};
}
return null;
})
.filter((entity): entity is MentionsEntity => !!entity && !!entity.value);
};
export default Mentions;