Input输入框·续1

<​template>  <​a-tooltip :trigger="['focus']" placement="topLeft" overlay-class-name="numeric-input">    <​template v-if="inputValue" #title>      <​span class="numeric-input-title">        {{ formatValue }}      <​/span>    <​/template>    <​a-input      v-model:value="inputValue"      placeholder="Input a number"      :max-length="25"      style="width: 120px"      @blur="onBlur"    />  <​/a-tooltip><​/template><​script lang="ts">import { computed, defineComponent, ref, watch } from 'vue';function formatNumber(value: string) {  value += '';  const list = value.split('.');  const prefix = list[0].charAt(0) === '-' ? '-' : '';  let num = prefix ? list[0].slice(1) : list[0];  let result = '';  while (num.length > 3) {    result = `,${num.slice(-3)}${result}`;    num = num.slice(0, num.length - 3);  }  if (num) {    result = num + result;  }  return `${prefix}${result}${list[1] ? `.${list[1]}` : ''}`;}export default defineComponent({  setup() {    const inputValue = ref<​string>('111');    const formatValue = computed(() => {      if (inputValue.value === '-') return '-';      return formatNumber(inputValue.value);    });    const format = (val: string, preVal: string) => {      const reg = /^-?\d*(\.\d*)?$/;      if ((!isNaN(+val) && reg.test(val)) || val === '' || val === '-') {        inputValue.value = val;      } else {        inputValue.value = preVal;      }    };    // '.' at the end or only '-' in the input box.    const onBlur = () => {      if (        inputValue.value.charAt(inputValue.value.length - 1) === '.' ||        inputValue.value === '-'      ) {        format(inputValue.value.slice(0, -1), inputValue.value);      }    };    watch(inputValue, (val, preVal) => {      format(val, preVal);    });    return {      inputValue,      onBlur,      formatValue,    };  },});<​/script><​style>/* to prevent the arrow overflow the popup container,or the height is not enough when content is empty */.numeric-input .ant-tooltip-inner {  min-width: 32px;  min-height: 37px;}.numeric-input .numeric-input-title {  font-size: 14px;}<​/style>

密码框

密码框。

<​template>  <​a-input-password v-model:value="value" placeholder="input password" /><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const value = ref<​string>('');    return {      value,    };  },});<​/script>

文本域

用于多行输入。

<​template>  <​a-textarea v-model:value="value" placeholder="Basic usage" :rows="4" /><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const value = ref<​string>('');    return {      value,    };  },});<​/script>

适应文本高度的文本域

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

1.5.0 后 autosize 被废弃,请使用 autoSize。

<​template>  <​div>    <​a-textarea      v-model:value="value1"      placeholder="Autosize height based on content lines"      auto-size    />    <​div style="margin: 24px 0" />    <​a-textarea      v-model:value="value2"      placeholder="Autosize height with minimum and maximum number of lines"      :auto-size="{ minRows: 2, maxRows: 5 }"    />  <​/div><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const value1 = ref<​string>('');    const value2 = ref<​string>('');    return {      value1,      value2,    };  },});<​/script>

搜索框

带有搜索按钮的输入框。

<​template>  <​div>    <​a-input-search      v-model:value="value"      placeholder="input search text"      style="width: 200px"      @search="onSearch"    />    <​br />    <​br />    <​a-input-search      v-model:value="value"      placeholder="input search text"      enter-button      @search="onSearch"    />    <​br />    <​br />    <​a-input-search      v-model:value="value"      placeholder="input search text"      enter-button="Search"      size="large"      @search="onSearch"    />    <​br />    <​br />    <​a-input-search      v-model:value="value"      placeholder="input search text"      size="large"      @search="onSearch"    >      <​template #enterButton>        <​a-button>Custom<​/a-button>      <​/template>    <​/a-input-search>  <​/div><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const value = ref<​string>('');    const onSearch = (searchValue: string) => {      console.log('use value', searchValue);      console.log('or use this.value', value.value);    };    return {      value,      onSearch,    };  },});<​/script>

三种大小

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

<​template>  <​div class="components-input-demo-size">    <​a-input v-model:value="value" size="large" placeholder="large size" />    <​a-input v-model:value="value" placeholder="default size" />    <​a-input v-model:value="value" size="small" placeholder="small size" />  <​/div><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const value = ref<​string>('');    return {      value,    };  },});<​/script><​style scoped>.components-input-demo-size .ant-input {  width: 200px;  margin: 0 8px 8px 0;}<​/style>

前置/后置标签

用于配置一些固定组合。

<​template>  <​div>    <​div style="margin-bottom: 16px">      <​a-input addon-before="Http://" addon-after=".com" v-model:value="value1" />    <​/div>    <​div style="margin-bottom: 16px">      <​a-input v-model:value="value2">        <​template #addonBefore>          <​a-sel​ect v-model:value="value3" style="width: 90px">            <​a-sel​ect-option value="Http://">Http://<​/a-sel​ect-option>            <​a-sel​ect-option value="Https://">Https://<​/a-sel​ect-option>          <​/a-sel​ect>        <​/template>        <​template #addonAfter>          <​a-sel​ect v-model:value="value4" style="width: 80px">            <​a-sel​ect-option value=".com">.com<​/a-sel​ect-option>            <​a-sel​ect-option value=".jp">.jp<​/a-sel​ect-option>            <​a-sel​ect-option value=".cn">.cn<​/a-sel​ect-option>            <​a-sel​ect-option value=".org">.org<​/a-sel​ect-option>          <​/a-sel​ect>        <​/template>      <​/a-input>    <​/div>    <​div style="margin-bottom: 16px">      <​a-input v-model:value="value5">        <​template #addonAfter>          <​setting-outlined />        <​/template>      <​/a-input>    <​/div>  <​/div><​/template><​script lang="ts">import { SettingOutlined } from '@ant-design/icons-vue';import { defineComponent, ref } from 'vue';export default defineComponent({  components: {    SettingOutlined,  },  setup() {    const value1 = ref<​string>('mysite');    const value2 = ref<​string>('mysite');    const value3 = ref<​string>('Http://');    const value4 = ref<​string>('.com');    const value5 = ref<​string>('mysite');    return {      value1,      value2,      value3,      value4,      value5,    };  },});<​/script>

带移除图标

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

<​template>  <​div>    <​a-input v-model:value="value1" placeholder="input with clear icon" allow-clear />    <​br />    <​br />    <​a-textarea v-model:value="value2" placeholder="textarea with clear icon" allow-clear />  <​/div><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const value1 = ref<​string>('');    const value2 = ref<​string>('');    return {      value1,      value2,    };  },});<​/script>

带字数提示的文本域

展示字数提示。

<​template>  <​a-textarea v-model:value="value" showCount :maxlength="100" /><​/template><​script lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    const value = ref<​string>('test value');    return {      value,    };  },});<​/script>

API

Input

参数 说明 类型 默认值 版本

| addonAfter | 带标签的 input,设置后置标签 | string|slot | | | |---|---|---|---|---|

| addonBefore | 带标签的 input,设置前置标签 | string|slot | | | |---|---|---|---|---|

defaultValue 输入框默认内容 string
disabled 是否禁用状态,默认为 false boolean false