Input输入框 - 图1

Input 输入框

通过鼠标或键盘输入内容,是最基础的表单域的包装。

何时使用

  • 需要用户输入表单域内容时。
  • 提供组合型输入框,带搜索的输入框,还可以进行大小选择。

代码演示

基本用法

基本用法。

  1. <template>
  2. <a-input v-model:value="value" placeholder="Basic usage" />
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, ref } from 'vue';
  6. export default defineComponent({
  7. setup() {
  8. const value = ref<string>('');
  9. return {
  10. value,
  11. };
  12. },
  13. });
  14. </script>

Input输入框 - 图2

前缀和后缀

在输入框上添加前缀或后缀图标。

  1. <template>
  2. <div class="components-input-demo-presuffix">
  3. <a-input placeholder="Basic usage" v-model:value="userName">
  4. <template #prefix>
  5. <user-outlined type="user" />
  6. </template>
  7. <template #suffix>
  8. <a-tooltip title="Extra information">
  9. <info-circle-outlined style="color: rgba(0, 0, 0, 0.45)" />
  10. </a-tooltip>
  11. </template>
  12. </a-input>
  13. <br />
  14. <br />
  15. <a-input prefix="¥" suffix="RMB" />
  16. </div>
  17. </template>
  18. <script lang="ts">
  19. import { UserOutlined, InfoCircleOutlined } from '@ant-design/icons-vue';
  20. import { defineComponent, ref } from 'vue';
  21. export default defineComponent({
  22. components: {
  23. UserOutlined,
  24. InfoCircleOutlined,
  25. },
  26. setup() {
  27. const userName = ref<string>('');
  28. return {
  29. userName,
  30. };
  31. },
  32. });
  33. </script>

Input输入框 - 图3

搜索框 loading

用于 onSearch 的时候展示 loading

  1. <template>
  2. <div>
  3. <a-input-search v-model:value="value" placeholder="input search loading deault" loading />
  4. <br />
  5. <br />
  6. <a-input-search
  7. v-model:value="value"
  8. placeholder="input search loading with enterButton"
  9. loading
  10. enter-button
  11. />
  12. </div>
  13. </template>
  14. <script lang="ts">
  15. import { defineComponent, ref } from 'vue';
  16. export default defineComponent({
  17. setup() {
  18. const value = ref<string>('');
  19. return {
  20. value,
  21. };
  22. },
  23. });
  24. </script>

Input输入框 - 图4

输入框组合

输入框的组合展现。 注意:使用 compact 模式时,不需要通过 Col 来控制宽度。

  1. <template>
  2. <div>
  3. <a-input-group size="large">
  4. <a-row :gutter="8">
  5. <a-col :span="5">
  6. <a-input v-model:value="value1" />
  7. </a-col>
  8. <a-col :span="8">
  9. <a-input v-model:value="value2" />
  10. </a-col>
  11. </a-row>
  12. </a-input-group>
  13. <br />
  14. <a-input-group compact>
  15. <a-input style="width: 20%" v-model:value="value1" />
  16. <a-input style="width: 30%" v-model:value="value2" />
  17. </a-input-group>
  18. <br />
  19. <a-input-group compact>
  20. <a-select v-model:value="value3">
  21. <a-select-option value="Zhejiang">Zhejiang</a-select-option>
  22. <a-select-option value="Jiangsu">Jiangsu</a-select-option>
  23. </a-select>
  24. <a-input style="width: 50%" v-model:value="value4" />
  25. </a-input-group>
  26. <br />
  27. <a-input-group compact>
  28. <a-select v-model:value="value5">
  29. <a-select-option value="Option1">Option1</a-select-option>
  30. <a-select-option value="Option2">Option2</a-select-option>
  31. </a-select>
  32. <a-input style="width: 50%" v-model:value="value6" />
  33. </a-input-group>
  34. <br />
  35. <a-input-group compact>
  36. <a-input style="width: 50%" v-model:value="value7" />
  37. <a-date-picker v-model:value="value8" style="width: 50%" />
  38. </a-input-group>
  39. <br />
  40. <a-input-group compact>
  41. <a-select v-model:value="value9">
  42. <a-select-option value="Option1-1">Option1-1</a-select-option>
  43. <a-select-option value="Option1-2">Option1-2</a-select-option>
  44. </a-select>
  45. <a-select v-model:value="value10">
  46. <a-select-option value="Option2-1">Option2-1</a-select-option>
  47. <a-select-option value="Option2-2">Option2-2</a-select-option>
  48. </a-select>
  49. </a-input-group>
  50. <br />
  51. <a-input-group compact>
  52. <a-select v-model:value="value11">
  53. <a-select-option value="1">Between</a-select-option>
  54. <a-select-option value="2">Except</a-select-option>
  55. </a-select>
  56. <a-input
  57. v-model:value="value12"
  58. style="width: 100px; text-align: center"
  59. placeholder="Minimum"
  60. />
  61. <a-input
  62. v-model:value="value13"
  63. style="width: 30px; border-left: 0; pointer-events: none; backgroundcolor: #fff"
  64. placeholder="~"
  65. disabled
  66. />
  67. <a-input
  68. v-model:value="value14"
  69. style="width: 100px; text-align: center; border-left: 0"
  70. placeholder="Maximum"
  71. />
  72. </a-input-group>
  73. <br />
  74. <a-input-group compact>
  75. <a-select v-model:value="value15">
  76. <a-select-option value="Sign Up">Sign Up</a-select-option>
  77. <a-select-option value="Sign In">Sign In</a-select-option>
  78. </a-select>
  79. <a-auto-complete
  80. v-model:value="value16"
  81. :data-source="dataSource"
  82. style="width: 200px"
  83. placeholder="Email"
  84. />
  85. </a-input-group>
  86. <br />
  87. <a-input-group compact>
  88. <a-select v-model:value="value17" style="width: 30%">
  89. <a-select-option value="Home">Home</a-select-option>
  90. <a-select-option value="Company">Company</a-select-option>
  91. </a-select>
  92. <a-cascader
  93. v-model:value="value18"
  94. style="width: 70%"
  95. :options="options"
  96. placeholder="Select Address"
  97. />
  98. </a-input-group>
  99. </div>
  100. </template>
  101. <script lang="ts">
  102. import { defineComponent, ref, watch } from 'vue';
  103. const options = [
  104. {
  105. value: 'zhejiang',
  106. label: 'Zhejiang',
  107. children: [
  108. {
  109. value: 'hangzhou',
  110. label: 'Hangzhou',
  111. children: [
  112. {
  113. value: 'xihu',
  114. label: 'West Lake',
  115. },
  116. ],
  117. },
  118. ],
  119. },
  120. {
  121. value: 'jiangsu',
  122. label: 'Jiangsu',
  123. children: [
  124. {
  125. value: 'nanjing',
  126. label: 'Nanjing',
  127. children: [
  128. {
  129. value: 'zhonghuamen',
  130. label: 'Zhong Hua Men',
  131. },
  132. ],
  133. },
  134. ],
  135. },
  136. ];
  137. export default defineComponent({
  138. setup() {
  139. const value1 = ref<string>('0571');
  140. const value2 = ref<string>('26888888');
  141. const value3 = ref<string>('Zhejiang');
  142. const value4 = ref<string>('Xihu District, Hangzhou');
  143. const value5 = ref<string>('Option1');
  144. const value6 = ref<string>('input content');
  145. const value7 = ref<string>('input content');
  146. const value8 = ref<string | null>(null);
  147. const value9 = ref<string>('Option1-1');
  148. const value10 = ref<string>('Option2-2');
  149. const value11 = ref<string>('1');
  150. const value12 = ref<string>('');
  151. const value13 = ref<string>('');
  152. const value14 = ref<string>('');
  153. const value15 = ref<string>('Sign Up');
  154. const value16 = ref<string>('');
  155. const value17 = ref<string>('Home');
  156. const value18 = ref<string[]>([]);
  157. const dataSource = ref<string[]>([]);
  158. watch(value16, val => {
  159. dataSource.value =
  160. !val || val.indexOf('@') >= 0
  161. ? []
  162. : [`${val}@gmail.com`, `${val}@163.com`, `${val}@qq.com`];
  163. });
  164. return {
  165. value1,
  166. value2,
  167. value3,
  168. value4,
  169. value5,
  170. value6,
  171. value7,
  172. value8,
  173. value9,
  174. value10,
  175. value11,
  176. value12,
  177. value13,
  178. value14,
  179. value15,
  180. value16,
  181. value17,
  182. value18,
  183. options,
  184. dataSource,
  185. };
  186. },
  187. });
  188. </script>

输入时格式化展示

结合 Tooltip 组件,实现一个数值输入框,方便内容超长时的全量展现。

  1. <template>
  2. <a-tooltip :trigger="['focus']" placement="topLeft" overlay-class-name="numeric-input">
  3. <template v-if="inputValue" #title>
  4. <span class="numeric-input-title">
  5. {{ formatValue }}
  6. </span>
  7. </template>
  8. <a-input
  9. v-model:value="inputValue"
  10. placeholder="Input a number"
  11. :max-length="25"
  12. style="width: 120px"
  13. @blur="onBlur"
  14. />
  15. </a-tooltip>
  16. </template>
  17. <script lang="ts">
  18. import { computed, defineComponent, ref, watch } from 'vue';
  19. function formatNumber(value: string) {
  20. value += '';
  21. const list = value.split('.');
  22. const prefix = list[0].charAt(0) === '-' ? '-' : '';
  23. let num = prefix ? list[0].slice(1) : list[0];
  24. let result = '';
  25. while (num.length > 3) {
  26. result = `,${num.slice(-3)}${result}`;
  27. num = num.slice(0, num.length - 3);
  28. }
  29. if (num) {
  30. result = num + result;
  31. }
  32. return `${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;
  33. }
  34. export default defineComponent({
  35. setup() {
  36. const inputValue = ref<string>('111');
  37. const formatValue = computed(() => {
  38. if (inputValue.value === '-') return '-';
  39. return formatNumber(inputValue.value);
  40. });
  41. const format = (val: string, preVal: string) => {
  42. const reg = /^-?\d*(\.\d*)?$/;
  43. if ((!isNaN(+val) && reg.test(val)) || val === '' || val === '-') {
  44. inputValue.value = val;
  45. } else {
  46. inputValue.value = preVal;
  47. }
  48. };
  49. // '.' at the end or only '-' in the input box.
  50. const onBlur = () => {
  51. if (
  52. inputValue.value.charAt(inputValue.value.length - 1) === '.' ||
  53. inputValue.value === '-'
  54. ) {
  55. format(inputValue.value.slice(0, -1), inputValue.value);
  56. }
  57. };
  58. watch(inputValue, (val, preVal) => {
  59. format(val, preVal);
  60. });
  61. return {
  62. inputValue,
  63. onBlur,
  64. formatValue,
  65. };
  66. },
  67. });
  68. </script>
  69. <style>
  70. /* to prevent the arrow overflow the popup container,
  71. or the height is not enough when content is empty */
  72. .numeric-input .ant-tooltip-inner {
  73. min-width: 32px;
  74. min-height: 37px;
  75. }
  76. .numeric-input .numeric-input-title {
  77. font-size: 14px;
  78. }
  79. </style>

Input输入框 - 图5

密码框

密码框。

  1. <template>
  2. <a-input-password v-model:value="value" placeholder="input password" />
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, ref } from 'vue';
  6. export default defineComponent({
  7. setup() {
  8. const value = ref<string>('');
  9. return {
  10. value,
  11. };
  12. },
  13. });
  14. </script>

文本域

用于多行输入。

  1. <template>
  2. <a-textarea v-model:value="value" placeholder="Basic usage" :rows="4" />
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, ref } from 'vue';
  6. export default defineComponent({
  7. setup() {
  8. const value = ref<string>('');
  9. return {
  10. value,
  11. };
  12. },
  13. });
  14. </script>

Input输入框 - 图6

适应文本高度的文本域

属性适用于 textarea 节点,并且只有高度会自动变化。另外 autoSize 可以设定为一个对象,指定最小行数和最大行数。

1.5.0autosize 被废弃,请使用 autoSize

  1. <template>
  2. <div>
  3. <a-textarea
  4. v-model:value="value1"
  5. placeholder="Autosize height based on content lines"
  6. auto-size
  7. />
  8. <div style="margin: 24px 0" />
  9. <a-textarea
  10. v-model:value="value2"
  11. placeholder="Autosize height with minimum and maximum number of lines"
  12. :auto-size="{ minRows: 2, maxRows: 5 }"
  13. />
  14. </div>
  15. </template>
  16. <script lang="ts">
  17. import { defineComponent, ref } from 'vue';
  18. export default defineComponent({
  19. setup() {
  20. const value1 = ref<string>('');
  21. const value2 = ref<string>('');
  22. return {
  23. value1,
  24. value2,
  25. };
  26. },
  27. });
  28. </script>

Input输入框 - 图7

搜索框

带有搜索按钮的输入框。

  1. <template>
  2. <div>
  3. <a-input-search
  4. v-model:value="value"
  5. placeholder="input search text"
  6. style="width: 200px"
  7. @search="onSearch"
  8. />
  9. <br />
  10. <br />
  11. <a-input-search
  12. v-model:value="value"
  13. placeholder="input search text"
  14. enter-button
  15. @search="onSearch"
  16. />
  17. <br />
  18. <br />
  19. <a-input-search
  20. v-model:value="value"
  21. placeholder="input search text"
  22. enter-button="Search"
  23. size="large"
  24. @search="onSearch"
  25. />
  26. <br />
  27. <br />
  28. <a-input-search
  29. v-model:value="value"
  30. placeholder="input search text"
  31. size="large"
  32. @search="onSearch"
  33. >
  34. <template #enterButton>
  35. <a-button>Custom</a-button>
  36. </template>
  37. </a-input-search>
  38. </div>
  39. </template>
  40. <script lang="ts">
  41. import { defineComponent, ref } from 'vue';
  42. export default defineComponent({
  43. setup() {
  44. const value = ref<string>('');
  45. const onSearch = (searchValue: string) => {
  46. console.log('use value', searchValue);
  47. console.log('or use this.value', value.value);
  48. };
  49. return {
  50. value,
  51. onSearch,
  52. };
  53. },
  54. });
  55. </script>

Input输入框 - 图8

三种大小

我们为 <Input /> 输入框定义了三种尺寸(大、默认、小),高度分别为 40px32px24px

  1. <template>
  2. <div class="components-input-demo-size">
  3. <a-input v-model:value="value" size="large" placeholder="large size" />
  4. <a-input v-model:value="value" placeholder="default size" />
  5. <a-input v-model:value="value" size="small" placeholder="small size" />
  6. </div>
  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>
  19. <style scoped>
  20. .components-input-demo-size .ant-input {
  21. width: 200px;
  22. margin: 0 8px 8px 0;
  23. }
  24. </style>

Input输入框 - 图9

前置/后置标签

用于配置一些固定组合。

  1. <template>
  2. <div>
  3. <div style="margin-bottom: 16px">
  4. <a-input addon-before="Http://" addon-after=".com" v-model:value="value1" />
  5. </div>
  6. <div style="margin-bottom: 16px">
  7. <a-input v-model:value="value2">
  8. <template #addonBefore>
  9. <a-select v-model:value="value3" style="width: 90px">
  10. <a-select-option value="Http://">Http://</a-select-option>
  11. <a-select-option value="Https://">Https://</a-select-option>
  12. </a-select>
  13. </template>
  14. <template #addonAfter>
  15. <a-select v-model:value="value4" style="width: 80px">
  16. <a-select-option value=".com">.com</a-select-option>
  17. <a-select-option value=".jp">.jp</a-select-option>
  18. <a-select-option value=".cn">.cn</a-select-option>
  19. <a-select-option value=".org">.org</a-select-option>
  20. </a-select>
  21. </template>
  22. </a-input>
  23. </div>
  24. <div style="margin-bottom: 16px">
  25. <a-input v-model:value="value5">
  26. <template #addonAfter>
  27. <setting-outlined />
  28. </template>
  29. </a-input>
  30. </div>
  31. </div>
  32. </template>
  33. <script lang="ts">
  34. import { SettingOutlined } from '@ant-design/icons-vue';
  35. import { defineComponent, ref } from 'vue';
  36. export default defineComponent({
  37. components: {
  38. SettingOutlined,
  39. },
  40. setup() {
  41. const value1 = ref<string>('mysite');
  42. const value2 = ref<string>('mysite');
  43. const value3 = ref<string>('Http://');
  44. const value4 = ref<string>('.com');
  45. const value5 = ref<string>('mysite');
  46. return {
  47. value1,
  48. value2,
  49. value3,
  50. value4,
  51. value5,
  52. };
  53. },
  54. });
  55. </script>

Input输入框 - 图10

带移除图标

带移除图标的输入框,点击图标删除所有内容。

  1. <template>
  2. <div>
  3. <a-input v-model:value="value1" placeholder="input with clear icon" allow-clear />
  4. <br />
  5. <br />
  6. <a-textarea v-model:value="value2" placeholder="textarea with clear icon" allow-clear />
  7. </div>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent, ref } from 'vue';
  11. export default defineComponent({
  12. setup() {
  13. const value1 = ref<string>('');
  14. const value2 = ref<string>('');
  15. return {
  16. value1,
  17. value2,
  18. };
  19. },
  20. });
  21. </script>

Input输入框 - 图11

带字数提示的文本域

展示字数提示。

  1. <template>
  2. <a-textarea v-model:value="value" showCount :maxlength="100" />
  3. </template>
  4. <script lang="ts">
  5. import { defineComponent, ref } from 'vue';
  6. export default defineComponent({
  7. setup() {
  8. const value = ref<string>('test value');
  9. return {
  10. value,
  11. };
  12. },
  13. });
  14. </script>

API

Input

参数说明类型默认值版本
addonAfter带标签的 input,设置后置标签string|slot
addonBefore带标签的 input,设置前置标签string|slot
defaultValue输入框默认内容string
disabled是否禁用状态,默认为 falsebooleanfalse
id输入框的 idstring
maxlength最大长度number1.5.0
prefix带有前缀图标的 inputstring|slot
size控件大小。注:标准表单内的输入框大小限制为 large。可选 large default smallstringdefault
suffix带有后缀图标的 inputstring|slot
type声明 input 类型,同原生 input 标签的 type 属性,见:MDN(请直接使用 Input.TextArea 代替 type=”textarea”)。stringtext
value(v-model)输入框内容string
allowClear可以点击清除图标删除内容boolean

Input 事件

事件名称说明回调参数
change输入框内容变化时的回调function(e)
pressEnter按下回车的回调function(e)

如果 InputForm.Item 内,并且 Form.Item 设置了 idoptions 属性,则 value defaultValueid 属性会被自动设置。

Input.TextArea

参数说明类型默认值版本
autosize自适应内容高度,可设置为 true | false 或对象:{ minRows: 2, maxRows: 6 }boolean|objectfalse
defaultValue输入框默认内容string
value(v-model)输入框内容string
allowClear可以点击清除图标删除内容boolean1.5.0
showCount是否展示字数booleanfalse

Input.TextArea 事件

事件名称说明回调参数
pressEnter按下回车的回调function(e)

Input.TextArea 的其他属性和浏览器自带的 textarea 一致。

Input.Search

参数说明类型默认值版本
enterButton是否有确认按钮,可设为按钮文字。该属性会与 addon 冲突。boolean|slotfalse
loading搜索 loadingboolean1.5.0

Input.Search 事件

事件名称说明回调参数
search点击搜索或按下回车键时的回调function(value, event)

其余属性和 Input 一致。

Input.Group

参数说明类型默认值
compact是否用紧凑模式booleanfalse
sizeInput.Group 中所有的 Input 的大小,可选 large default smallstringdefault
  1. <a-input-group>
  2. <a-input />
  3. <a-input />
  4. </a-input-group>

Input.Password (1.14.0 中新增)

参数说明类型默认值
visibilityToggle是否显示切换按钮booleantrue