view.jsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { computed, h, watch, ref } from 'vue'
  2. import { formInputViewProps } from '../../form-input-props'
  3. import FileRow from './file-row'
  4. import { useFormView } from '@cip/components/hooks/form-input'
  5. import { isString } from '@cip/utils/util'
  6. export default {
  7. props: formInputViewProps,
  8. setup (props) {
  9. const { securityConfig } = useFormView(props)
  10. const FileComponent = (file) => h(FileRow, {
  11. file: file,
  12. config: securityConfig.value,
  13. dependOnValues: props.dependOnValues
  14. })
  15. const useStringValue = computed(() => {
  16. return securityConfig.value.stringValue
  17. })
  18. const objectValue = computed(() => { // 返回的数据是一个对象
  19. return securityConfig.value.objectValue
  20. })
  21. const fileList = ref()
  22. watch(() => props.modelValue, () => {
  23. if (props.modelValue) {
  24. if (!useStringValue.value) {
  25. // 兼容未配置stringValue但存储结果为string的modelValue
  26. if (isString(props.modelValue)) {
  27. fileList.value = props.modelValue.split(',').map(v => ({ url: v }))
  28. } else if (objectValue.value) {
  29. fileList.value = [props.modelValue]
  30. } else {
  31. fileList.value = props.modelValue
  32. }
  33. } else {
  34. fileList.value = props.modelValue.split(',').map(v => ({ url: v }))
  35. }
  36. } else {
  37. fileList.value = []
  38. }
  39. }, { immediate: true })
  40. const FileRows = () => (fileList.value || []).map(FileComponent)
  41. return () => h('div', { style: { width: '100%' } }, { default: FileRows })
  42. }
  43. }