InputTextArea.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. reactData.error = res.errMsg;
  48. }
  49. }
  50. function onChange(e) {
  51. emit('change', e.target.value);
  52. }
  53. return {
  54. onFocus,
  55. onBlur,
  56. onChange,
  57. reactData,
  58. ...toRefs(props),
  59. };
  60. },
  61. });
  62. </script>
  63. <style scoped>
  64. .wrap {
  65. display: flex;
  66. }
  67. .input-text-area {
  68. width: 55%;
  69. margin-right: 10px;
  70. }
  71. span {
  72. font-size: 13px;
  73. line-height: 250%;
  74. color: gray;
  75. }
  76. .error-msg {
  77. color: red;
  78. }
  79. </style>