Vue@Input: 構建可復用的輸入組件

一、基礎功能

Vue@Input是一個可重用的輸入組件,能夠處理用戶的輸入並把結果傳遞出去。在構建這個組件時,我們考慮到了以下幾個基礎功能:

1. 輸入框的雙向綁定

在Vue中,雙向綁定是一個十分常見的功能,而輸入框也是使用最頻繁的元素之一。因此,在Vue@Input中,我們提供了一個名為”value”的prop來完成輸入框的雙向綁定,同時,我們也可以通過”$emit”事件來處理用戶的輸入。

<template>
  <div>
    <input v-model="inputValue" />
  </div>
</template>

<script>
export default {
  name: 'vue-input',
  props: {
    value: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      inputValue: this.value
    }
  },
  watch: {
    value(newValue) {
      this.inputValue = newValue
    },
    inputValue(newInputValue) {
      this.$emit('input', newInputValue)
    }
  }
}
</script>

2. placeholder placeholder-text

placeholder是輸入框中的一個佔位符,用戶可以在這裡輸入提示信息。在Vue@Input中,我們增加了名為”placeholder”的prop,並為輸入框添加了對應屬性。同時,我們也允許用戶可以自定義佔位符。

<template>
  <div>
    <input v-model="inputValue" :placeholder="placeholderText" />
  </div>
</template>

<script>
export default {
  name: 'vue-input',
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    placeholder: {
      type: String,
      default: '請輸入'
    }
  },
  data() {
    return {
      inputValue: this.value
    }
  },
  watch: {
    value(newValue) {
      this.inputValue = newValue
    },
    inputValue(newInputValue) {
      this.$emit('input', newInputValue)
    }
  },
  computed: {
    placeholderText() {
      return this.placeholder
    }
  }
}
</script>

3. disabled

有時候,我們需要禁用輸入框,這時候在Vue@Input中,我們增加了一個名為”disabled”的prop,並為輸入框添加了對應屬性。

<template>
  <div>
    <input v-model="inputValue" :placeholder="placeholderText" :disabled="isDisabled" />
  </div>
</template>

<script>
export default {
  name: 'vue-input',
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    placeholder: {
      type: String,
      default: '請輸入'
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      inputValue: this.value
    }
  },
  watch: {
    value(newValue) {
      this.inputValue = newValue
    },
    inputValue(newInputValue) {
      this.$emit('input', newInputValue)
    }
  },
  computed: {
    placeholderText() {
      return this.placeholder
    },
    isDisabled() {
      return this.disabled
    }
  }
}
</script>

二、提高功能

除了基礎功能以外,Vue@Input還提供了一些特殊功能來增強用戶的輸入體驗。

1. 自動聚焦

在用戶打開頁面時,輸入框能夠自動聚焦是一個比較友好的設計,因此在Vue@Input中,我們增加了一個名為”autoFocus”的prop,能夠實現自動聚焦的效果。

<template>
  <div>
    <input v-model="inputValue" :placeholder="placeholderText" :disabled="isDisabled" :autofocus="autoFocus" />
  </div>
</template>

<script>
export default {
  name: 'vue-input',
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    placeholder: {
      type: String,
      default: '請輸入'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    autoFocus: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      inputValue: this.value
    }
  },
  watch: {
    value(newValue) {
      this.inputValue = newValue
    },
    inputValue(newInputValue) {
      this.$emit('input', newInputValue)
    }
  },
  computed: {
    placeholderText() {
      return this.placeholder
    },
    isDisabled() {
      return this.disabled
    }
  }
}
</script>

2. 輸入框前置圖標

在輸入框前面添加一個圖標,能夠幫助用戶更好地理解輸入框的作用及意義,同時也美化了界面。在Vue@Input中,我們增加了一個名為”prepend”的prop,用於設置輸入框前置圖標的相關信息。

<template>
  <div>
    <div class="flex items-center border rounded px-2">
      <div v-if="prepend"><i :class="prepend.icon" :style="{ 'color': prepend.color }" /></div>
      <input v-model="inputValue" :placeholder="placeholderText" :disabled="isDisabled" />
    </div>
  </div>
</template>

<script>
export default {
  name: 'vue-input',
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    placeholder: {
      type: String,
      default: '請輸入'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    prepend: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      inputValue: this.value
    }
  },
  watch: {
    value(newValue) {
      this.inputValue = newValue
    },
    inputValue(newInputValue) {
      this.$emit('input', newInputValue)
    }
  },
  computed: {
    placeholderText() {
      return this.placeholder
    },
    isDisabled() {
      return this.disabled
    }
  }
}
</script>

3. 輸入框後置圖標

與前置圖標類似,輸入框後置圖標也能夠為用戶提供更好的體驗和理解。在Vue@Input中,我們增加了一個名為”append”的prop,用於設置輸入框後置圖標的相關信息。

<template>
  <div>
    <div class="flex items-center border rounded px-2">
      <input v-model="inputValue" :placeholder="placeholderText" :disabled="isDisabled" />
      <div v-if="append"><i :class="append.icon" :style="{ 'color': append.color }" /></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'vue-input',
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    placeholder: {
      type: String,
      default: '請輸入'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    prepend: {
      type: Object,
      default: null
    },
    append: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      inputValue: this.value
    }
  },
  watch: {
    value(newValue) {
      this.inputValue = newValue
    },
    inputValue(newInputValue) {
      this.$emit('input', newInputValue)
    }
  },
  computed: {
    placeholderText() {
      return this.placeholder
    },
    isDisabled() {
      return this.disabled
    }
  }
}
</script>

三、總結

Vue@Input是一個高度可定製化的輸入組件,能夠幫助開發者快速構建出符合需求的輸入框。其提供了雙向綁定、佔位符、禁用、前/後置圖標、自動聚焦等基礎功能,同時還提供了更多實用的提高功能。我們希望開發者能夠結合自身需求,靈活使用Vue@Input。下面是一個完整的代碼示例:

<template>
  <div>
    <div class="flex items-center border rounded px-2">
      <div v-if="prepend"><i :class="prepend.icon" :style="{ 'color': prepend.color }" /></div>
      <input v-model="inputValue" :placeholder="placeholderText" :disabled="isDisabled" :autofocus="autoFocus" />
      <div v-if="append"><i :class="append.icon" :style="{ 'color': append.color }" /></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'vue-input',
  props: {
    value: {
      type: [String, Number],
      default: ''
    },
    placeholder: {
      type: String,
      default: '請輸入'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    prepend: {
      type: Object,
      default: null
    },
    append: {
      type: Object,
      default: null
    },
    autoFocus: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      inputValue: this.value
    }
  },
  watch: {
    value(newValue) {
      this.inputValue = newValue
    },
    inputValue(newInputValue) {
      this.$emit('input', newInputValue)
    }
  },
  computed: {
    placeholderText() {
      return this.placeholder
    },
    isDisabled() {
      return this.disabled
    }
  }
}
</script>

原創文章,作者:RTQR,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/138241.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
RTQR的頭像RTQR
上一篇 2024-10-04 00:19
下一篇 2024-10-04 00:19

相關推薦

  • 使用Vue實現前端AES加密並輸出為十六進位的方法

    在前端開發中,數據傳輸的安全性問題十分重要,其中一種保護數據安全的方式是加密。本文將會介紹如何使用Vue框架實現前端AES加密並將加密結果輸出為十六進位。 一、AES加密介紹 AE…

    編程 2025-04-29
  • Python input參數變數用法介紹

    本文將從多個方面對Python input括弧里參數變數進行闡述與詳解,並提供相應的代碼示例。 一、基本介紹 Python input()函數用於獲取用戶輸入。當程序運行到inpu…

    編程 2025-04-29
  • Vue TS工程結構用法介紹

    在本篇文章中,我們將從多個方面對Vue TS工程結構進行詳細的闡述,涵蓋文件結構、路由配置、組件間通訊、狀態管理等內容,並給出對應的代碼示例。 一、文件結構 一個好的文件結構可以極…

    編程 2025-04-29
  • 如何修改ant組件的動效為中心

    當我們使用Ant Design時,其默認的組件動效可能不一定符合我們的需求,這時我們需要修改Ant Design組件動效,使其更加符合我們的UI設計。本文將從多個方面詳細闡述如何修…

    編程 2025-04-29
  • Ant Design組件的動效

    Ant Design是一個基於React技術棧的UI組件庫,其中動效是該組件庫中的一個重要特性之一。動效的使用可以讓用戶更清晰、更直觀地了解到UI交互的狀態變化,從而提高用戶的滿意…

    編程 2025-04-29
  • Vue3的vue-resource使用教程

    本文將從以下幾個方面詳細闡述Vue3如何使用vue-resource。 一、安裝Vue3和vue-resource 在使用vue-resource前,我們需要先安裝Vue3和vue…

    編程 2025-04-27
  • input代碼中代表什麼

    在web開發中,input是最基礎的輸入控制項之一,常用來收集用戶的數據並提交至伺服器進行處理。本文將從多個方面詳細闡述input代碼中代表什麼。 一、type屬性 在HTML中,i…

    編程 2025-04-27
  • Vue模擬按鍵按下

    本文將從以下幾個方面對Vue模擬按鍵按下進行詳細闡述: 一、Vue 模擬按鍵按下的場景 在前端開發中,我們常常需要模擬按鍵按下的場景,比如在表單中填寫內容後,按下「回車鍵」提交表單…

    編程 2025-04-27
  • ThinkPHP6 + Vue.js: 不使用Fetch的數據請求方法

    本文將介紹如何在ThinkPHP6和Vue.js中進行數據請求,同時避免使用Fetch函數。 一、AJAX:XMLHttpRequest的基礎使用 在進行數據請求時,最基礎的方式就…

    編程 2025-04-27
  • Python input列表

    本文將從多個角度詳細介紹Python怎麼input列表。 一、基礎概念 Python中的列表是一種有序的數據序列,可以包含任意類型的數據。當我們需要從用戶獲取一組數據時,可以使用i…

    編程 2025-04-27

發表回復

登錄後才能評論