Mentions提及 - 图1

Mentions 提及

提及组件。

何时使用

  • 用于在输入中提及某人或某事,常用于发布、聊天或评论功能。

代码演示

Mentions提及 - 图2

基本用法

基本用法

  1. <template>
  2. <a-mentions autofocus v-model:value="value" @select="onSelect">
  3. <a-mentions-option value="afc163">afc163</a-mentions-option>
  4. <a-mentions-option value="zombieJ">zombieJ</a-mentions-option>
  5. <a-mentions-option value="yesmeck">yesmeck</a-mentions-option>
  6. </a-mentions>
  7. </template>
  8. <script lang="ts">
  9. import { defineComponent, ref, watch } from 'vue';
  10. export default defineComponent({
  11. setup() {
  12. const value = ref<string>('@afc163');
  13. watch(value, () => {
  14. console.log('value', value);
  15. });
  16. const onSelect = (option: { value: string }) => {
  17. console.log('select', option);
  18. };
  19. return {
  20. value,
  21. onSelect,
  22. };
  23. },
  24. });
  25. </script>

Mentions提及 - 图3

自定义触发字符

通过 prefix 属性自定义触发字符。默认为 @, 可以定义为数组。

  1. <template>
  2. <a-mentions
  3. v-model:value="value"
  4. placeholder="input @ to mention people, # to mention tag"
  5. :prefix="['@', '#']"
  6. @search="onSearch"
  7. >
  8. <a-mentions-option v-for="value in options" :key="value" :value="value">
  9. {{ value }}
  10. </a-mentions-option>
  11. </a-mentions>
  12. </template>
  13. <script lang="ts">
  14. import { computed, defineComponent, ref } from 'vue';
  15. const MOCK_DATA: Record<string, string[]> = {
  16. '@': ['afc163', 'zombiej', 'yesmeck'],
  17. '#': ['1.0', '2.0', '3.0'],
  18. };
  19. export default defineComponent({
  20. setup() {
  21. const prefix = ref<string>('@');
  22. const value = ref<string>('');
  23. const options = computed(() => {
  24. return MOCK_DATA[prefix.value] || [];
  25. });
  26. const onSearch = (_: string, val: string) => {
  27. console.log(_, val);
  28. prefix.value = val;
  29. };
  30. return {
  31. value,
  32. options,
  33. onSearch,
  34. };
  35. },
  36. });
  37. </script>

Mentions提及 - 图4

向上展开

向上展开建议。

  1. <template>
  2. <a-mentions v-model:value="value" placement="top">
  3. <a-mentions-option value="afc163">afc163</a-mentions-option>
  4. <a-mentions-option value="zombieJ">zombieJ</a-mentions-option>
  5. <a-mentions-option value="yesmeck">yesmeck</a-mentions-option>
  6. </a-mentions>
  7. </template>
  8. <script lang="ts">
  9. import { defineComponent, ref } from 'vue';
  10. export default defineComponent({
  11. setup() {
  12. const value = ref<string>('');
  13. return {
  14. value,
  15. };
  16. },
  17. });
  18. </script>

Mentions提及 - 图5

异步加载

匹配内容列表为异步返回时。

  1. <template>
  2. <a-mentions v-model:value="value" :loading="loading" @search="onSearch">
  3. <a-mentions-option v-for="{ login, avatar_url: avatar } in users" :key="login" :value="login">
  4. <img :src="avatar" :alt="login" style="width: 20px; margin-right: 8px" />
  5. <span>{{ login }}</span>
  6. </a-mentions-option>
  7. </a-mentions>
  8. </template>
  9. <script lang="ts">
  10. import { debounce } from 'lodash-es';
  11. import { defineComponent, ref } from 'vue';
  12. export default defineComponent({
  13. setup() {
  14. const value = ref<string>('');
  15. const loading = ref<boolean>(false);
  16. const users = ref<string[]>([]);
  17. const search = ref<string>('');
  18. const loadGithubUsers = debounce((key: string) => {
  19. if (!key) {
  20. users.value = [];
  21. return;
  22. }
  23. fetch(`https://api.github.com/search/users?q=${key}`)
  24. .then(res => res.json())
  25. .then(({ items = [] }) => {
  26. if (search.value !== key) return;
  27. users.value = items.slice(0, 10);
  28. loading.value = false;
  29. });
  30. }, 800);
  31. const onSearch = (searchValue: string) => {
  32. search.value = searchValue;
  33. loading.value = !!searchValue;
  34. console.log(!!searchValue);
  35. users.value = [];
  36. console.log('Search:', searchValue);
  37. loadGithubUsers(searchValue);
  38. };
  39. return {
  40. value,
  41. loading,
  42. users,
  43. loadGithubUsers,
  44. onSearch,
  45. };
  46. },
  47. });
  48. </script>

Mentions提及 - 图6

无效或只读

通过 disabled 属性设置是否生效。通过 readonly 属性设置是否只读。

  1. <template>
  2. <div>
  3. <div style="margin-bottom: 10px">
  4. <a-mentions v-model:value="value1" placeholder="this is disabled Mentions" disabled>
  5. <a-mentions-option v-for="value in options" :key="value" :value="value">
  6. {{ value }}
  7. </a-mentions-option>
  8. </a-mentions>
  9. </div>
  10. <a-mentions v-model:value="value2" placeholder="this is readOnly a-mentions" readonly>
  11. <a-mentions-option v-for="value in options" :key="value" :value="value">
  12. {{ value }}
  13. </a-mentions-option>
  14. </a-mentions>
  15. </div>
  16. </template>
  17. <script lang="ts">
  18. import { defineComponent, ref } from 'vue';
  19. export default defineComponent({
  20. setup() {
  21. const value1 = ref<string>('');
  22. const value2 = ref<string>('');
  23. const options = ref<string[]>(['afc163', 'zombieJ', 'yesmeck']);
  24. return {
  25. value1,
  26. value2,
  27. options,
  28. };
  29. },
  30. });
  31. </script>

Top coders

Bio

SubmitReset

配合 Form 使用

配合 Form 使用。

  1. <template>
  2. <a-form layout="horizontal">
  3. <a-form-item
  4. label="Top coders"
  5. :label-col="{ span: 6 }"
  6. :wrapper-col="{ span: 18 }"
  7. name="coders"
  8. v-bind="validateInfos.coders"
  9. >
  10. <a-mentions rows="1" v-model:value="modelRef.coders">
  11. <a-mentions-option value="afc163">afc163</a-mentions-option>
  12. <a-mentions-option value="zombieJ">zombieJ</a-mentions-option>
  13. <a-mentions-option value="yesmeck">yesmeck</a-mentions-option>
  14. </a-mentions>
  15. </a-form-item>
  16. <a-form-item
  17. label="Bio"
  18. :label-col="{ span: 6 }"
  19. :wrapper-col="{ span: 18 }"
  20. name="bio"
  21. v-bind="validateInfos.bio"
  22. >
  23. <a-mentions
  24. rows="3"
  25. placeholder="You can use @ to ref user here"
  26. v-model:value="modelRef.bio"
  27. >
  28. <a-mentions-option value="afc163">afc163</a-mentions-option>
  29. <a-mentions-option value="zombieJ">zombieJ</a-mentions-option>
  30. <a-mentions-option value="yesmeck">yesmeck</a-mentions-option>
  31. </a-mentions>
  32. </a-form-item>
  33. <a-form-item :wrapper-col="{ span: 12, offset: 5 }">
  34. <a-button type="primary" @click="handleSubmit">Submit</a-button>
  35. <a-button style="margin-left: 8px" @click="resetFields">Reset</a-button>
  36. </a-form-item>
  37. </a-form>
  38. </template>
  39. <script>
  40. import { Mentions } from 'ant-design-vue';
  41. import { defineComponent, reactive } from 'vue';
  42. import { useForm } from '@ant-design-vue/use';
  43. const { getMentions } = Mentions;
  44. export default defineComponent({
  45. setup() {
  46. const checkMention = async (rule, value) => {
  47. const mentions = getMentions(value);
  48. if (mentions.length < 2) {
  49. return Promise.reject('More than one must be selected!');
  50. } else {
  51. return Promise.resolve();
  52. }
  53. };
  54. const modelRef = reactive({
  55. bio: '',
  56. coders: '',
  57. });
  58. const rulesRef = reactive({
  59. bio: [{ required: true, message: 'Must input bio' }],
  60. coders: [{ required: true, validator: checkMention }],
  61. });
  62. const { resetFields, validate, validateInfos } = useForm(modelRef, rulesRef);
  63. const handleSubmit = e => {
  64. e.preventDefault();
  65. validate()
  66. .then(() => {
  67. console.log('Submit!!!', modelRef);
  68. })
  69. .catch(errors => {
  70. console.log('Errors in the form!!!', errors);
  71. });
  72. };
  73. return {
  74. modelRef,
  75. resetFields,
  76. validateInfos,
  77. handleSubmit,
  78. };
  79. },
  80. });
  81. </script>

API

Mentions

参数说明类型默认值
autofocus自动获得焦点booleanfalse
defaultValue默认值string
filterOption自定义过滤逻辑false | (input: string, option: OptionProps) => boolean
notFoundContent当下拉列表为空时显示的内容ReactNode‘Not Found’
placement弹出层展示位置top | bottombottom
prefix设置触发关键字string | string[]‘@’
split设置选中项前后分隔符string‘ ‘
validateSearch自定义触发验证逻辑(text: string, props: MentionsProps) => void
value(v-model)设置值string
getPopupContainer指定建议框挂载的 HTML 节点() => HTMLElement

事件

事件名称说明回调参数
blur失去焦点的时回调function
change值改变时触发function(value: string)
focus获得焦点时回调function
search文本框值变化时回调function(value: string, prefix: string)
select选择选项时触发function(option: OptionProps, prefix: string)

Mentions 方法

名称描述
blur()移除焦点
focus()获取焦点

Option

参数说明类型默认值
value选择时填充的值string‘’