index.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <!--
  3. ss-input 智能输入组件
  4. 自动绑定ValidatedTd的事件处理函数
  5. -->
  6. <view class="smart-input">
  7. <input
  8. class="smart-input__field"
  9. :placeholder="$attrs.placeholder || '请输入'"
  10. :value="modelValue"
  11. @input="handleInput"
  12. @blur="handleBlur"
  13. @change="handleInput"
  14. />
  15. </view>
  16. </template>
  17. <script setup>
  18. import { inject } from 'vue'
  19. const props = defineProps({
  20. modelValue: {
  21. type: String,
  22. default: ''
  23. }
  24. })
  25. const emit = defineEmits(['update:modelValue'])
  26. // 从ValidatedTd注入事件处理函数(兼容旧方式)
  27. const onInput = inject('onInput', null)
  28. const onBlur = inject('onBlur', null)
  29. // ss-input初始化完成
  30. const handleInput = (event) => {
  31. const value = event.detail?.value || event.target?.value || ''
  32. // 1. 支持v-model
  33. emit('update:modelValue', value)
  34. // 2. 兼容旧的inject方式
  35. if (onInput) {
  36. onInput(event)
  37. }
  38. console.log(`SsInput handleInput: ${value}`)
  39. }
  40. const handleBlur = (event) => {
  41. // 调用ValidatedTd的处理函数
  42. if (onBlur) {
  43. onBlur(event)
  44. }
  45. }
  46. </script>
  47. <style lang="scss" scoped>
  48. .smart-input {
  49. width: 100%;
  50. &__field {
  51. width: 100%;
  52. height: 60rpx;
  53. // padding: 0 20rpx;
  54. font-size: 32rpx;
  55. line-height: 60rpx;
  56. color: #333;
  57. background-color: transparent;
  58. border: none;
  59. outline: none;
  60. box-sizing: border-box;
  61. &::placeholder {
  62. color: #999;
  63. font-size: 32rpx;
  64. }
  65. &:focus {
  66. color: #333;
  67. border: 1px solid #ccc;
  68. }
  69. }
  70. }
  71. </style>