index.jsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { h, toRefs, computed } from 'vue'
  2. import { getH5LayoutComponent, getLayoutComponent, getLayoutViewComponent } from '@cip/components/cip-form-layout/util'
  3. import { useFormInject } from '../hooks/use-form'
  4. import './index.less'
  5. export default {
  6. name: 'CipFormLayout',
  7. props: {
  8. config: Object, // 字段配置信息
  9. fieldKey: String, // 字段名
  10. model: { // 字段所属model
  11. type: Object,
  12. default: () => ({})
  13. },
  14. grid: {}, // Number | Boolean
  15. componentKey: {},
  16. readonly: Boolean // 是否只读-即查看模式
  17. },
  18. emits: ['update:model', 'validate', 'submit', 'cancel', 'selectItem', 'update:config'],
  19. setup (props, context) {
  20. const { model } = toRefs(props)
  21. const cipForm = useFormInject()
  22. const cipFormStyle = computed(() => {
  23. if (props.grid) {
  24. return { gridColumn: `span ${props.config.span}` }
  25. } else {
  26. return 'width: 100%'
  27. }
  28. })
  29. const formLayout = () => {
  30. const componentProps = {
  31. class: ['cip-form-layout', { 'cip-form-layout--hidden': props.config.hideItem }],
  32. model: model.value,
  33. config: props.config,
  34. fieldKey: props.fieldKey,
  35. style: cipFormStyle.value
  36. }
  37. const componentType = props.config.type
  38. if (props.readonly) {
  39. return h(getLayoutViewComponent(componentType), componentProps, {
  40. item: ({ children, index }) => {
  41. return context.slots.item?.({ children, index })
  42. }
  43. })
  44. } else {
  45. const method = cipForm.equipment === 'pc' ? getLayoutComponent : getH5LayoutComponent
  46. return h(method(componentType),
  47. {
  48. ...componentProps,
  49. 'onUpdate:config': (val) => { context.emit('update:config', val) },
  50. onValidate: (fn) => { context.emit('validate', fn) },
  51. onSubmit: () => { context.emit('submit') },
  52. onCancel: () => { context.emit('cancel') },
  53. onSelectItem: (val) => { context.emit('selectItem', val) }
  54. }
  55. , {
  56. item: (scope) => {
  57. return context.slots.item?.(scope)
  58. }
  59. }
  60. )
  61. }
  62. }
  63. return () => formLayout()
  64. }
  65. }