index.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <template>
  2. <el-input :model-value="modelValue"
  3. ref="inputRef"
  4. @update:modelValue="emitInput"
  5. :placeholder="placeholder"
  6. :clearable="clearable"
  7. :disabled="disabled"
  8. :maxlength="limit"
  9. :show-word-limit="!!limit"
  10. type="textarea"
  11. :autosize="autosize"
  12. resize="none"
  13. :style="{width}"></el-input>
  14. </template>
  15. <script>
  16. import { ElInput } from 'element-plus'
  17. import { computed, watch, ref, nextTick, inject } from 'vue'
  18. import { useFormInput } from '@cip/components/hooks/form-input'
  19. import { formInputProps } from '../../form-input-props'
  20. export default {
  21. components: { ElInput },
  22. props: formInputProps,
  23. emits: ['update:modelValue'],
  24. setup (props, context) {
  25. const cipConfig = inject('cip-config', {})
  26. const { width, clearable, ...formInput } = useFormInput(props, context)
  27. const inputRef = ref()
  28. const emitInput = (val) => {
  29. context.emit('update:modelValue', val)
  30. }
  31. const limit = computed(() => {
  32. return props.config?.limit ?? cipConfig?.limit?.textarea ?? ''
  33. })
  34. const autosize = computed(() => {
  35. return props.config?.autosize || { minRows: 2, maxRows: 6 }
  36. })
  37. watch(() => props.config.autosize, (val) => {
  38. if (inputRef.value) {
  39. nextTick(() => {
  40. inputRef.value.resizeTextarea()
  41. })
  42. }
  43. }, { deep: true }) // 需要深度监听才可以正常触发
  44. return {
  45. ...formInput,
  46. inputRef,
  47. clearable,
  48. emitInput,
  49. autosize,
  50. width,
  51. limit
  52. }
  53. }
  54. }
  55. </script>