InputTextArea.vue 1.8 KB

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