use-form-layout.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { computed } from 'vue'
  2. import { getNextItem, isEmptyObject } from '@cip/utils/util'
  3. import { getCopyItem } from '@cip/components/cip-form-design/util'
  4. export const useFormLayoutOptions = ({ props, emit }) => {
  5. const options = computed(() => {
  6. return props.config.options || []
  7. })
  8. const updateConfig = (config) => {
  9. emit('update:config', config)
  10. }
  11. const updateOptionChildren = (optionIndex, children) => {
  12. const config = props.config
  13. config.options[optionIndex].children = children
  14. updateConfig(config)
  15. }
  16. const updateOptionChild = (optionIndex, childIndex, childConfig) => {
  17. const config = props.config
  18. config.options[optionIndex].children[childIndex].config = childConfig
  19. updateConfig(config)
  20. }
  21. const deleteOptionChild = (optionIndex, childIndex) => {
  22. const config = props.config
  23. const nextItem = getNextItem(config.options[optionIndex].children, childIndex)
  24. if (!isEmptyObject(nextItem)) {
  25. emitSelectItem(nextItem)
  26. } else {
  27. emitSelectItem(props.config)
  28. }
  29. config.options[optionIndex].children.splice(childIndex, 1)
  30. updateConfig(config)
  31. }
  32. const copyOptionChild = (optionIndex, childIndex) => {
  33. const config = props.config
  34. const newItem = getCopyItem(config.options[optionIndex].children[childIndex])
  35. config.options[optionIndex].children.splice(childIndex + 1, 0, newItem)
  36. updateConfig(config)
  37. emitSelectItem(newItem)
  38. }
  39. const addOptionChild = (optionIndex, { newIndex: childIndex }) => {
  40. const config = props.config
  41. const newItem = config.options[optionIndex].children[childIndex]
  42. updateConfig(config)
  43. emitSelectItem(newItem)
  44. }
  45. // 选中
  46. const emitSelectItem = (item) => {
  47. emit('selectItem', item)
  48. }
  49. return {
  50. updateOptionChildren,
  51. updateOptionChild,
  52. deleteOptionChild,
  53. copyOptionChild,
  54. addOptionChild,
  55. emitSelectItem,
  56. options
  57. }
  58. }