Input.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <template>
  2. <div class="wrap">
  3. <Input :value="value" @focus="onFocus" @change="onChange" @blur="onBlur" />
  4. <span>{{ reactData.tip }}</span>
  5. <span class="error-msg">{{ reactData.error }}</span>
  6. </div>
  7. </template>
  8. <script lang="ts">
  9. import { Input } from 'ant-design-vue';
  10. import { defineComponent, reactive, toRefs, watch } from 'vue';
  11. import { validateType } from '/@/utils/validTools';
  12. const props = {
  13. value: { type: String, default: '' },
  14. tip: { type: String, default: '' },
  15. errMsg: { type: String, default: '' },
  16. rules: { type: Array, default: [] },
  17. };
  18. export default defineComponent({
  19. components: { Input },
  20. props,
  21. emits: ['change'],
  22. setup(props, { emit }) {
  23. const reactData = reactive({
  24. tip: '',
  25. error: '',
  26. });
  27. watch(
  28. () => props.errMsg,
  29. (errMsg) => {
  30. reactData.error = errMsg;
  31. }
  32. );
  33. function onFocus() {
  34. reactData.tip = props.tip;
  35. reactData.error = '';
  36. }
  37. function onBlur(e) {
  38. reactData.tip = '';
  39. if (props.rules && props.rules[0]) {
  40. const res = validateType(props.rules, e.target.value);
  41. reactData.error = res.errMsg;
  42. }
  43. }
  44. function onChange(e) {
  45. emit('change', e.target.value);
  46. }
  47. return {
  48. onFocus,
  49. onBlur,
  50. onChange,
  51. reactData,
  52. ...toRefs(props),
  53. };
  54. },
  55. });
  56. </script>
  57. <style scoped>
  58. .wrap {
  59. display: flex;
  60. }
  61. input {
  62. width: 55%;
  63. margin-right: 10px;
  64. }
  65. span {
  66. font-size: 13px;
  67. line-height: 250%;
  68. color: gray;
  69. }
  70. .error-msg {
  71. color: red;
  72. }
  73. </style>