2021-11-10 02:06:59 +00:00
|
|
|
import type { App, PropType, ExtractPropTypes } from 'vue';
|
2021-09-25 08:51:32 +00:00
|
|
|
import { watch, ref, onMounted, defineComponent, nextTick } from 'vue';
|
2020-08-31 08:53:19 +00:00
|
|
|
import classNames from '../_util/classNames';
|
2020-03-07 11:45:13 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2021-11-10 02:06:59 +00:00
|
|
|
import VcMentions, { Option } from '../vc-mentions';
|
2021-06-23 13:47:53 +00:00
|
|
|
import { mentionsProps as baseMentionsProps } from '../vc-mentions/src/mentionsProps';
|
2021-09-25 08:51:32 +00:00
|
|
|
import useConfigInject from '../_util/hooks/useConfigInject';
|
|
|
|
import { flattenChildren, getOptionProps } from '../_util/props-util';
|
|
|
|
import { useInjectFormItemContext } from '../form/FormItemContext';
|
|
|
|
import omit from '../_util/omit';
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2020-10-15 01:48:43 +00:00
|
|
|
interface MentionsConfig {
|
|
|
|
prefix?: string | string[];
|
|
|
|
split?: string;
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:47:53 +00:00
|
|
|
export interface MentionsOptionProps {
|
2020-10-15 01:48:43 +00:00
|
|
|
value: string;
|
2021-09-25 08:51:32 +00:00
|
|
|
disabled?: boolean;
|
|
|
|
label?: string | number | ((o: MentionsOptionProps) => any);
|
2020-10-15 01:48:43 +00:00
|
|
|
[key: string]: any;
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
interface MentionsEntity {
|
|
|
|
prefix: string;
|
|
|
|
value: string;
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
export type MentionPlacement = 'top' | 'bottom';
|
|
|
|
|
|
|
|
const getMentions = (value = '', config: MentionsConfig): MentionsEntity[] => {
|
2020-03-07 11:45:13 +00:00
|
|
|
const { prefix = '@', split = ' ' } = config || {};
|
2021-09-25 08:51:32 +00:00
|
|
|
const prefixList: string[] = Array.isArray(prefix) ? prefix : [prefix];
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
return value
|
|
|
|
.split(split)
|
2021-09-25 08:51:32 +00:00
|
|
|
.map((str = ''): MentionsEntity | null => {
|
|
|
|
let hitPrefix: string | null = null;
|
2020-03-07 11:45:13 +00:00
|
|
|
|
|
|
|
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,
|
2021-09-25 08:51:32 +00:00
|
|
|
value: str.slice((hitPrefix as string).length),
|
2020-03-07 11:45:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
2021-09-25 08:51:32 +00:00
|
|
|
.filter((entity): entity is MentionsEntity => !!entity && !!entity.value);
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-06-23 13:47:53 +00:00
|
|
|
const mentionsProps = {
|
|
|
|
...baseMentionsProps,
|
|
|
|
loading: PropTypes.looseBool,
|
|
|
|
onFocus: {
|
|
|
|
type: Function as PropType<(e: FocusEvent) => void>,
|
|
|
|
},
|
|
|
|
onBlur: {
|
|
|
|
type: Function as PropType<(e: FocusEvent) => void>,
|
|
|
|
},
|
|
|
|
onSelect: {
|
|
|
|
type: Function as PropType<(option: MentionsOptionProps, prefix: string) => void>,
|
|
|
|
},
|
|
|
|
onChange: {
|
|
|
|
type: Function as PropType<(text: string) => void>,
|
|
|
|
},
|
2021-09-25 08:51:32 +00:00
|
|
|
notFoundContent: PropTypes.any,
|
|
|
|
defaultValue: String,
|
|
|
|
id: String,
|
2021-06-23 13:47:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type MentionsProps = Partial<ExtractPropTypes<typeof mentionsProps>>;
|
|
|
|
|
2020-10-15 01:48:43 +00:00
|
|
|
const Mentions = defineComponent({
|
2020-03-07 11:45:13 +00:00
|
|
|
name: 'AMentions',
|
|
|
|
inheritAttrs: false,
|
2021-06-23 13:47:53 +00:00
|
|
|
props: mentionsProps,
|
2021-09-25 08:51:32 +00:00
|
|
|
emits: ['update:value', 'change', 'focus', 'blur', 'select', 'pressenter'],
|
|
|
|
slots: ['notFoundContent', 'option'],
|
|
|
|
setup(props, { slots, emit, attrs, expose }) {
|
|
|
|
const { prefixCls, renderEmpty, direction } = useConfigInject('mentions', props);
|
|
|
|
const focused = ref(false);
|
|
|
|
const vcMentions = ref(null);
|
|
|
|
const value = ref(props.value ?? props.defaultValue ?? '');
|
|
|
|
const formItemContext = useInjectFormItemContext();
|
|
|
|
watch(
|
|
|
|
() => props.value,
|
|
|
|
val => {
|
|
|
|
value.value = val;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
const handleFocus = (e: FocusEvent) => {
|
|
|
|
focused.value = true;
|
|
|
|
emit('focus', e);
|
2020-06-20 02:08:45 +00:00
|
|
|
};
|
2021-09-25 08:51:32 +00:00
|
|
|
|
|
|
|
const handleBlur = (e: FocusEvent) => {
|
|
|
|
focused.value = false;
|
|
|
|
emit('blur', e);
|
|
|
|
formItemContext.onFieldBlur();
|
2020-03-07 11:45:13 +00:00
|
|
|
};
|
2021-09-25 08:51:32 +00:00
|
|
|
|
|
|
|
const handleSelect = (...args: [MentionsOptionProps, string]) => {
|
|
|
|
emit('select', ...args);
|
|
|
|
focused.value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleChange = (val: string) => {
|
|
|
|
if (props.value === undefined) {
|
|
|
|
value.value = val;
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
2021-09-25 08:51:32 +00:00
|
|
|
emit('update:value', val);
|
|
|
|
emit('change', val);
|
|
|
|
formItemContext.onFieldChange();
|
|
|
|
};
|
|
|
|
|
|
|
|
const getNotFoundContent = () => {
|
|
|
|
const notFoundContent = props.notFoundContent;
|
2020-03-07 11:45:13 +00:00
|
|
|
if (notFoundContent !== undefined) {
|
|
|
|
return notFoundContent;
|
|
|
|
}
|
2021-09-25 08:51:32 +00:00
|
|
|
if (slots.notFoundContent) {
|
|
|
|
return slots.notFoundContent();
|
2020-03-07 11:45:13 +00:00
|
|
|
}
|
2021-09-25 08:51:32 +00:00
|
|
|
return renderEmpty.value('Select');
|
|
|
|
};
|
|
|
|
|
|
|
|
const getOptions = () => {
|
|
|
|
return flattenChildren(slots.default?.() || []).map(item => {
|
|
|
|
return { ...getOptionProps(item), label: (item.children as any)?.default?.() };
|
|
|
|
});
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const focus = () => {
|
|
|
|
(vcMentions.value as HTMLTextAreaElement).focus();
|
2020-03-07 11:45:13 +00:00
|
|
|
};
|
|
|
|
|
2021-09-25 08:51:32 +00:00
|
|
|
const blur = () => {
|
|
|
|
(vcMentions.value as HTMLTextAreaElement).blur();
|
|
|
|
};
|
|
|
|
|
|
|
|
expose({ focus, blur });
|
|
|
|
|
|
|
|
onMounted(() => {
|
|
|
|
nextTick(() => {
|
|
|
|
if (process.env.NODE_ENV === 'test') {
|
|
|
|
if (props.autofocus) {
|
|
|
|
focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
const {
|
|
|
|
disabled,
|
|
|
|
getPopupContainer,
|
|
|
|
rows = 1,
|
|
|
|
id = formItemContext.id.value,
|
|
|
|
...restProps
|
|
|
|
} = props;
|
|
|
|
const { class: className, ...otherAttrs } = attrs;
|
|
|
|
const otherProps = omit(restProps, ['defaultValue', 'onUpdate:value', 'prefixCls']);
|
|
|
|
|
|
|
|
const mergedClassName = classNames(className, {
|
|
|
|
[`${prefixCls.value}-disabled`]: disabled,
|
|
|
|
[`${prefixCls.value}-focused`]: focused.value,
|
|
|
|
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
|
|
|
|
});
|
|
|
|
|
|
|
|
const mentionsProps = {
|
|
|
|
prefixCls: prefixCls.value,
|
|
|
|
...otherProps,
|
|
|
|
disabled,
|
|
|
|
direction: direction.value,
|
|
|
|
filterOption: props.filterOption,
|
|
|
|
getPopupContainer,
|
|
|
|
options: props.options || getOptions(),
|
|
|
|
class: mergedClassName,
|
|
|
|
...otherAttrs,
|
|
|
|
rows,
|
|
|
|
onChange: handleChange,
|
|
|
|
onSelect: handleSelect,
|
|
|
|
onFocus: handleFocus,
|
|
|
|
onBlur: handleBlur,
|
|
|
|
ref: vcMentions,
|
|
|
|
value: value.value,
|
|
|
|
id,
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<VcMentions
|
|
|
|
{...mentionsProps}
|
|
|
|
v-slots={{ notFoundContent: getNotFoundContent, option: slots.option }}
|
|
|
|
></VcMentions>
|
|
|
|
);
|
|
|
|
};
|
2020-03-07 11:45:13 +00:00
|
|
|
},
|
2020-10-15 01:48:43 +00:00
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-11-10 02:06:59 +00:00
|
|
|
/* istanbul ignore next */
|
|
|
|
export const MentionsOption = defineComponent({
|
2021-09-25 08:51:32 +00:00
|
|
|
...Option,
|
|
|
|
name: 'AMentionsOption',
|
2021-11-10 02:06:59 +00:00
|
|
|
});
|
2020-03-07 11:45:13 +00:00
|
|
|
|
2021-11-10 02:06:59 +00:00
|
|
|
export default Object.assign(Mentions, {
|
|
|
|
Option: MentionsOption,
|
|
|
|
getMentions,
|
|
|
|
install: (app: App) => {
|
|
|
|
app.component(Mentions.name, Mentions);
|
|
|
|
app.component(MentionsOption.name, MentionsOption);
|
|
|
|
return app;
|
|
|
|
},
|
|
|
|
});
|