Form.vue 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. <!-- Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { getNode, createMessage } from '@formkit/core'
  4. import { cloneAny } from '@formkit/utils'
  5. import { FormKit, FormKitMessages, FormKitSchema } from '@formkit/vue'
  6. import { refDebounced, watchOnce } from '@vueuse/shared'
  7. import { isEqual, cloneDeep, merge, isEmpty } from 'lodash-es'
  8. import {
  9. computed,
  10. ref,
  11. nextTick,
  12. shallowRef,
  13. reactive,
  14. toRef,
  15. watch,
  16. markRaw,
  17. useSlots,
  18. onBeforeUnmount,
  19. } from 'vue'
  20. import { NotificationTypes } from '#shared/components/CommonNotifications/types.ts'
  21. import { useNotifications } from '#shared/components/CommonNotifications/useNotifications.ts'
  22. import { useObjectAttributeFormFields } from '#shared/entities/object-attributes/composables/useObjectAttributeFormFields.ts'
  23. import { useObjectAttributeLoadFormFields } from '#shared/entities/object-attributes/composables/useObjectAttributeLoadFormFields.ts'
  24. import UserError from '#shared/errors/UserError.ts'
  25. import type {
  26. EnumObjectManagerObjects,
  27. EnumFormUpdaterId,
  28. FormUpdaterRelationField,
  29. FormUpdaterQuery,
  30. FormUpdaterQueryVariables,
  31. ObjectAttributeValue,
  32. FormUpdaterMetaInput,
  33. FormUpdaterChangedFieldInput,
  34. } from '#shared/graphql/types.ts'
  35. import { parseGraphqlId } from '#shared/graphql/utils.ts'
  36. import { I18N, i18n } from '#shared/i18n.ts'
  37. import { QueryHandler } from '#shared/server/apollo/handler/index.ts'
  38. import type { EntityObject } from '#shared/types/entity.ts'
  39. import type {
  40. FormUpdaterAdditionalParams,
  41. FormUpdaterOptions,
  42. FormUpdaterTrigger,
  43. } from '#shared/types/form.ts'
  44. import { camelize } from '#shared/utils/formatter.ts'
  45. import { getFirstFocusableElement } from '#shared/utils/getFocusableElements.ts'
  46. import getUuid from '#shared/utils/getUuid.ts'
  47. import { edgesToArray } from '#shared/utils/helpers.ts'
  48. import log from '#shared/utils/log.ts'
  49. import { markup } from '#shared/utils/markup.ts'
  50. import testFlags from '#shared/utils/testFlags.ts'
  51. import FormGroup from './FormGroup.vue'
  52. import FormLayout from './FormLayout.vue'
  53. import { useFormUpdaterQuery } from './graphql/queries/formUpdater.api.ts'
  54. import { getFormClasses } from './initializeFormClasses.ts'
  55. import { FormHandlerExecution, FormValidationVisibility } from './types.ts'
  56. import {
  57. getNodeByName as getFormkitFieldNode,
  58. getNodeId,
  59. setErrors,
  60. } from './utils.ts'
  61. import type {
  62. ChangedField,
  63. FormSubmitData,
  64. FormFieldAdditionalProps,
  65. FormFieldValue,
  66. FormHandler,
  67. FormHandlerFunction,
  68. FormSchemaField,
  69. FormSchemaLayout,
  70. FormSchemaNode,
  71. FormValues,
  72. ReactiveFormSchemData,
  73. } from './types.ts'
  74. import type {
  75. FormKitPlugin,
  76. FormKitSchemaNode,
  77. FormKitSchemaCondition,
  78. FormKitNode,
  79. FormKitClasses,
  80. FormKitSchemaDOMNode,
  81. FormKitSchemaComponent,
  82. FormKitMessageProps,
  83. } from '@formkit/core'
  84. import type { Except, SetRequired } from 'type-fest'
  85. import type { Component, Ref } from 'vue'
  86. export interface Props {
  87. id?: string
  88. schema?: FormSchemaNode[]
  89. schemaData?: Except<ReactiveFormSchemData, 'fields'>
  90. schemaComponentLibrary?: Record<string, Component>
  91. handlers?: FormHandler[]
  92. changeFields?: Record<string, Partial<FormSchemaField>>
  93. formId?: string
  94. formUpdaterId?: EnumFormUpdaterId
  95. formUpdaterInitialOnly?: boolean
  96. formUpdaterAdditionalParams?: FormUpdaterAdditionalParams
  97. // Maybe in the future this is no longer needed, when FormKit supports group
  98. // without value grouping below group name (https://github.com/formkit/formkit/issues/461).
  99. flattenFormGroups?: string[]
  100. formKitPlugins?: FormKitPlugin[]
  101. formKitSectionsSchema?: Record<
  102. string,
  103. Partial<FormKitSchemaNode> | FormKitSchemaCondition
  104. >
  105. class?: FormKitClasses | string | Record<string, boolean>
  106. formClass?: string | Record<string, string>
  107. // Can be used to define initial values on frontend side and fetched schema from the server.
  108. initialValues?: Partial<FormValues>
  109. initialEntityObject?: EntityObject
  110. validationVisibility?: FormValidationVisibility
  111. disabled?: boolean
  112. shouldAutofocus?: boolean
  113. // Some special properties for working with object attribute fields inside of a form schema.
  114. useObjectAttributes?: boolean
  115. objectAttributeSkippedFields?: string[]
  116. clearValuesAfterSubmit?: boolean
  117. // Implement the submit in this way, because we need to react on async usage of the submit function.
  118. // Don't forget that to submit a form with "Enter" key, you need to add a button with type="submit" inside of the form.
  119. // Or to have a button outside of form with "form" attribite with the same value as the form id.
  120. // After this method is called, form resets its values and state. If you need to call something afterwards,
  121. // like make route navigation, you can return a function from the submit handler, which will be called after the form reset.
  122. // When you return "false" inside the submit function the handling will be stopped.
  123. onSubmit?: (
  124. values: FormSubmitData,
  125. ) => Promise<void | (() => void) | false> | void | (() => void) | false
  126. }
  127. const props = withDefaults(defineProps<Props>(), {
  128. schema: () => {
  129. return []
  130. },
  131. changeFields: () => {
  132. return reactive({})
  133. },
  134. validationVisibility: FormValidationVisibility.Submit,
  135. useObjectAttributes: false,
  136. })
  137. const formId = props.formId ? props.formId : getUuid()
  138. const slots = useSlots()
  139. const hasSchema = computed(
  140. () => Boolean(slots.default) || Boolean(props.schema),
  141. )
  142. const formSchemaInitialized = ref(false)
  143. if (!hasSchema.value) {
  144. log.error(
  145. 'No schema defined. Please use the schema prop or the default slot for the schema.',
  146. )
  147. }
  148. // Rename prop 'class' for usage in the template, because of reserved word
  149. const localClass = toRef(props, 'class')
  150. const emit = defineEmits<{
  151. changed: [
  152. fieldName: string,
  153. newValue: FormFieldValue,
  154. oldValue: FormFieldValue,
  155. ]
  156. node: [node: FormKitNode]
  157. settled: []
  158. focused: []
  159. }>()
  160. const showInitialLoadingAnimation = ref(false)
  161. const debouncedShowInitialLoadingAnimation = refDebounced(
  162. showInitialLoadingAnimation,
  163. 300,
  164. )
  165. const formKitInitialNodesSettled = ref(false)
  166. const formResetRunning = ref(false)
  167. const formNode: Ref<FormKitNode | undefined> = ref()
  168. const formElement = ref<HTMLElement>()
  169. const changeFields = toRef(props, 'changeFields')
  170. const updaterChangedFields = new Set<string>()
  171. const changeInitialValue = new Map<string, FormFieldValue>()
  172. const getNodeByName = (id: string) => {
  173. return getFormkitFieldNode(formId, id)
  174. }
  175. const findNodeByName = (name: string) => {
  176. return formNode.value?.find(name, 'name')
  177. }
  178. const autofocusFirstInput = (node: FormKitNode) => {
  179. nextTick(() => {
  180. const firstInput = getFirstFocusableElement(formElement.value)
  181. firstInput?.focus()
  182. firstInput?.scrollIntoView({ block: 'nearest' })
  183. const formName = node.context?.id || node.name
  184. testFlags.set(`${formName}.focused`)
  185. emit('focused')
  186. })
  187. }
  188. const setInitialEntityObjectToContext = (
  189. node: FormKitNode,
  190. object = props.initialEntityObject,
  191. ) => {
  192. if (node.context && object) {
  193. node.context.initialEntityObject = object
  194. }
  195. }
  196. const setFormNode = (node: FormKitNode) => {
  197. formNode.value = node
  198. setInitialEntityObjectToContext(node)
  199. node.settled.then(() => {
  200. showInitialLoadingAnimation.value = false
  201. nextTick(() => {
  202. changeInitialValue.forEach((value, fieldName) => {
  203. findNodeByName(fieldName)?.input(value, false)
  204. })
  205. changeInitialValue.clear()
  206. formKitInitialNodesSettled.value = true
  207. // Reset directly after the initial request.
  208. updaterChangedFields.clear()
  209. const formName = node.context?.id || node.name
  210. testFlags.set(`${formName}.settled`)
  211. emit('settled')
  212. // Set the initial settled flag when all things are executed.
  213. // This means form is settled and also the initial form updater values are set.
  214. node.store.set(
  215. createMessage({
  216. blocking: false,
  217. key: 'initialSettled',
  218. value: true,
  219. visible: false,
  220. }),
  221. )
  222. executeFormHandler(FormHandlerExecution.InitialSettled, values.value)
  223. if (props.shouldAutofocus) autofocusFirstInput(node)
  224. })
  225. })
  226. node.on('autofocus', () => autofocusFirstInput(node))
  227. emit('node', node)
  228. }
  229. const formNodeContext = computed(() => formNode.value?.context)
  230. // Build the flat value when its requested for specific form groups.
  231. const getFlatValues = (values: FormValues, formGroups: string[]) => {
  232. const flatValues = {
  233. ...values,
  234. }
  235. formGroups.forEach((formGroup) => {
  236. Object.assign(flatValues, flatValues[formGroup])
  237. delete flatValues[formGroup]
  238. })
  239. return flatValues
  240. }
  241. // Use the node context value, instead of the v-model, because of performance reason.
  242. const values = computed<FormValues>(() => {
  243. if (!formNodeContext.value) {
  244. return {}
  245. }
  246. if (!props.flattenFormGroups) return formNodeContext.value.value
  247. return getFlatValues(formNodeContext.value.value, props.flattenFormGroups)
  248. })
  249. const relationFields: FormUpdaterRelationField[] = []
  250. const relationFieldBelongsToObjectField: Record<string, string> = {}
  251. const formUpdaterProcessing = computed(
  252. () => formNode.value?.context?.state.formUpdaterProcessing || false,
  253. )
  254. const uploadProcessing = computed(
  255. () => formNode.value?.context?.state.uploadProcessing || false,
  256. )
  257. let delayedSubmit = false
  258. const onSubmitRaw = () => {
  259. if (formUpdaterProcessing.value || uploadProcessing.value) {
  260. delayedSubmit = true
  261. }
  262. }
  263. const onSubmit = (values: FormSubmitData) => {
  264. // Needs to be checked, because the 'onSubmit' function is not required.
  265. if (!props.onSubmit) return undefined
  266. const flatValues = props.flattenFormGroups
  267. ? getFlatValues(values, props.flattenFormGroups)
  268. : values
  269. formNode.value?.clearErrors()
  270. const submitResult = props.onSubmit(flatValues)
  271. if (submitResult !== undefined && submitResult === false) return
  272. if (submitResult instanceof Promise) {
  273. return submitResult
  274. .then((result) => {
  275. // When false was returned the submit was skipped.
  276. if (result !== undefined && result === false) return
  277. // it's possible to destroy Form before this is called and the reset should not run when errors exists.
  278. if (!formNode.value || formNode.value.context?.state.errors) return
  279. // TODO: maybe should do some similar thing like in the formReset function for the form updater
  280. if (props.clearValuesAfterSubmit) {
  281. formNode.value.reset()
  282. } else {
  283. formNode.value.reset(values)
  284. // "dirty" check checks "_init" instead of "initial"
  285. // "initial" is updated with resetValues in "reset" function, but "_init" is static
  286. // TODO: keep an eye on https://github.com/formkit/formkit/issues/791
  287. formNode.value.props._init = cloneAny(formNode.value.props.initial)
  288. formNode.value.walk((node) => {
  289. if (node.name in flatValues) {
  290. node.props._init = cloneAny(flatValues[node.name])
  291. } else if (node.name in values) {
  292. node.props._init = cloneAny(values[node.name])
  293. }
  294. })
  295. }
  296. result?.()
  297. })
  298. .catch((errors: UserError) => {
  299. if (formNode.value) setErrors(formNode.value, errors)
  300. })
  301. }
  302. formNode.value?.reset(!props.clearValuesAfterSubmit ? values : undefined)
  303. submitResult?.()
  304. return submitResult
  305. }
  306. let formUpdaterQueryHandler: QueryHandler<
  307. FormUpdaterQuery,
  308. FormUpdaterQueryVariables
  309. >
  310. const triggerFormUpdater = (options?: FormUpdaterOptions) => {
  311. handlesFormUpdater('manual', undefined, options)
  312. }
  313. const delayedSubmitPlugin = (node: FormKitNode) => {
  314. node.on('message-removed', async ({ payload }) => {
  315. if (
  316. (payload.key === 'formUpdaterProcessing' ||
  317. payload.key === 'uploadProcessing') &&
  318. delayedSubmit
  319. ) {
  320. // We need to wait on the "next tick", so that the validation for updated fields is ready.
  321. setTimeout(() => {
  322. delayedSubmit = false
  323. node.submit()
  324. }, 0)
  325. }
  326. })
  327. return false
  328. }
  329. const localFormKitPlugins = computed(() => {
  330. return [delayedSubmitPlugin, ...(props.formKitPlugins || [])]
  331. })
  332. const formConfig = computed(() => {
  333. return {
  334. validationVisibility: props.validationVisibility,
  335. }
  336. })
  337. // Define the additional component library for the used components which are not form fields.
  338. const additionalComponentLibrary = {
  339. FormLayout: markRaw(FormLayout),
  340. FormGroup: markRaw(FormGroup),
  341. ...props.schemaComponentLibrary,
  342. }
  343. // Define the static schema, which will be filled with the real fields from the `schemaData`.
  344. const staticSchema = ref<FormKitSchemaNode[]>([])
  345. const fixedAndSkippedFields: string[] = []
  346. const schemaData = reactive<ReactiveFormSchemData>({
  347. fields: {},
  348. values,
  349. // Helper function to translate directly with the formkit syntax.
  350. // Wrapper is neded, because of unexpected side effects.
  351. t: (
  352. source: Parameters<I18N['t']>[0],
  353. ...args: Array<Parameters<I18N['t']>[1]>
  354. ) => {
  355. return i18n.t(source, ...args)
  356. },
  357. markup,
  358. ...props.schemaData,
  359. })
  360. const internalFieldCamelizeName: Record<string, string> = {}
  361. const getInternalId = (item?: { id?: string; internalId?: number }) => {
  362. if (!item) return undefined
  363. if (item.internalId) return item.internalId
  364. if (!item.id) return undefined
  365. return parseGraphqlId(item.id).id
  366. }
  367. let initialEntityObjectAttributeMap: Record<string, FormFieldValue> = {}
  368. const setInitialEntityObjectAttributeMap = (
  369. initialEntityObject = props.initialEntityObject,
  370. ) => {
  371. if (isEmpty(initialEntityObject)) return
  372. const { objectAttributeValues } = initialEntityObject
  373. if (!objectAttributeValues) return
  374. // Reduce object attribute values to flat structure
  375. initialEntityObjectAttributeMap =
  376. objectAttributeValues.reduce((acc: Record<string, FormFieldValue>, cur) => {
  377. const { attribute } = cur
  378. if (!attribute || !attribute.name) return acc
  379. acc[attribute.name] = cur.value
  380. return acc
  381. }, {}) || {}
  382. }
  383. // Initialize the initial entity object attribute map during the setup in a static way.
  384. // It will maybe be updated later, when the resetForm is used with a different entity object.
  385. setInitialEntityObjectAttributeMap()
  386. const getInitialEntityObjectValue = (
  387. fieldName: string,
  388. initialEntityObject = props.initialEntityObject,
  389. ): FormFieldValue => {
  390. if (isEmpty(initialEntityObject)) return undefined
  391. let value: FormFieldValue
  392. if (relationFieldBelongsToObjectField[fieldName]) {
  393. const belongsToObject =
  394. initialEntityObject[relationFieldBelongsToObjectField[fieldName]]
  395. if (!belongsToObject) return undefined
  396. if ('edges' in belongsToObject) {
  397. value = edgesToArray(
  398. belongsToObject as { edges?: { node: { internalId: number } }[] },
  399. ).map((item) => getInternalId(item))
  400. } else {
  401. value = getInternalId(belongsToObject)
  402. }
  403. }
  404. if (!value) {
  405. const targetFieldName = internalFieldCamelizeName[fieldName] || fieldName
  406. value =
  407. targetFieldName in initialEntityObjectAttributeMap
  408. ? initialEntityObjectAttributeMap[targetFieldName]
  409. : initialEntityObject[targetFieldName]
  410. }
  411. return value
  412. }
  413. const getResetFormValues = (
  414. rootNode: FormKitNode,
  415. values: FormValues,
  416. object?: EntityObject,
  417. groupNode?: FormKitNode,
  418. resetDirty = true,
  419. ) => {
  420. const resetValues: FormValues = {}
  421. const dirtyNodes: FormKitNode[] = []
  422. const dirtyValues: FormValues = {}
  423. const setResetFormValue = (
  424. name: string,
  425. value: FormFieldValue,
  426. parentName?: string,
  427. ) => {
  428. if (parentName) {
  429. resetValues[parentName] ||= {}
  430. ;(resetValues[parentName] as Record<string, FormFieldValue>)[name] = value
  431. return
  432. }
  433. resetValues[name] = value
  434. }
  435. Object.entries(schemaData.fields).forEach(([field, { props }]) => {
  436. const formElement = props.id ? getNode(props.id) : getNodeByName(props.name)
  437. if (!formElement) return
  438. let parentName = ''
  439. if (formElement.parent && formElement.parent.name !== rootNode.name) {
  440. parentName = formElement.parent.name
  441. }
  442. // Do not use the parentName, when we are in group node reset context.
  443. const groupName = groupNode?.name
  444. if (groupName) {
  445. if (parentName !== groupName) return
  446. parentName = ''
  447. }
  448. if (!resetDirty && formElement.context?.state.dirty) {
  449. dirtyNodes.push(formElement)
  450. dirtyValues[field] = formElement._value as FormFieldValue
  451. }
  452. if (field in values) {
  453. setResetFormValue(field, values[field], parentName)
  454. return
  455. }
  456. if (parentName && parentName in values && values[parentName]) {
  457. const value = (values[parentName] as Record<string, FormFieldValue>)[
  458. field
  459. ]
  460. setResetFormValue(field, value, parentName)
  461. return
  462. }
  463. const objectValue = getInitialEntityObjectValue(field, object)
  464. if (objectValue !== undefined) {
  465. setResetFormValue(field, objectValue, parentName)
  466. }
  467. })
  468. return {
  469. dirtyNodes,
  470. dirtyValues,
  471. resetValues,
  472. }
  473. }
  474. const resetForm = (
  475. values: FormValues = {},
  476. object: EntityObject | undefined = undefined,
  477. { resetDirty = true }: { resetDirty?: boolean } = {},
  478. groupNode: FormKitNode | undefined = undefined,
  479. ) => {
  480. if (!formNode.value) return
  481. formResetRunning.value = true
  482. const rootNode = formNode.value
  483. if (object) {
  484. setInitialEntityObjectAttributeMap(object)
  485. setInitialEntityObjectToContext(rootNode, object)
  486. }
  487. const { dirtyNodes, dirtyValues, resetValues } = getResetFormValues(
  488. rootNode,
  489. values,
  490. object,
  491. groupNode,
  492. resetDirty,
  493. )
  494. ;(groupNode || rootNode)?.reset(
  495. Object.keys(resetValues).length ? resetValues : undefined,
  496. )
  497. // keep dirty nodes as dirty
  498. dirtyNodes.forEach((node) => {
  499. node.input(dirtyValues[node.name], false)
  500. })
  501. formResetRunning.value = false
  502. // Trigger the formUpdater, when the reset is done.
  503. handlesFormUpdater('form-reset')
  504. }
  505. const localInitialValues: FormValues = { ...props.initialValues }
  506. const initializeFieldRelation = (
  507. fieldName: string,
  508. relation: FormSchemaField['relation'],
  509. belongsToObjectField?: string,
  510. ) => {
  511. if (relation) {
  512. relationFields.push({
  513. name: fieldName,
  514. relation: relation.type,
  515. filterIds: relation.filterIds,
  516. })
  517. }
  518. if (belongsToObjectField) {
  519. relationFieldBelongsToObjectField[fieldName] = belongsToObjectField
  520. }
  521. }
  522. const setInternalField = (fieldName: string, internal: boolean) => {
  523. if (!internal) return
  524. internalFieldCamelizeName[fieldName] = camelize(fieldName)
  525. }
  526. const updateSchemaLink = (
  527. specificProps: FormFieldAdditionalProps,
  528. fieldName: string,
  529. ) => {
  530. // native fields don't have link attribute, and we don't have a way to get rendered link from graphql
  531. const values = (props.initialEntityObject?.objectAttributeValues ||
  532. []) as ObjectAttributeValue[]
  533. const attribute = values.find(({ attribute }) => attribute.name === fieldName)
  534. if (attribute?.renderedLink) {
  535. specificProps.link = attribute.renderedLink
  536. }
  537. }
  538. const updateSchemaDataField = (
  539. field: FormSchemaField | SetRequired<Partial<FormSchemaField>, 'name'>,
  540. ) => {
  541. const {
  542. show,
  543. updateFields,
  544. relation,
  545. if: staticCondition,
  546. props: specificProps = {},
  547. ...fieldProps
  548. } = field
  549. const showWithStaticCondition = Boolean(
  550. staticCondition || schemaData.fields[field.name]?.staticCondition,
  551. )
  552. const showField =
  553. show ??
  554. schemaData.fields[field.name]?.show ??
  555. (showWithStaticCondition ? undefined : true)
  556. // Special handling for the disabled prop, so that the form can handle also
  557. // the disable state from outside.
  558. if ('disabled' in fieldProps && !fieldProps.disabled) {
  559. fieldProps.disabled = undefined
  560. }
  561. updateSchemaLink(fieldProps, field.name)
  562. if (schemaData.fields[field.name]) {
  563. schemaData.fields[field.name] = {
  564. show: showField,
  565. updateFields: updateFields || false,
  566. staticCondition: showWithStaticCondition,
  567. props: Object.assign(
  568. schemaData.fields[field.name].props,
  569. fieldProps,
  570. specificProps,
  571. ),
  572. }
  573. } else {
  574. initializeFieldRelation(
  575. field.name,
  576. relation,
  577. specificProps?.belongsToObjectField,
  578. )
  579. setInternalField(field.name, Boolean(fieldProps.internal))
  580. const combinedFieldProps = Object.assign(fieldProps, specificProps)
  581. // Select the correct initial value (at this time localInitialValues has not already the information
  582. // from the initial entity object, so we need to check it manually).
  583. if (field.name in localInitialValues) {
  584. combinedFieldProps.value = localInitialValues[field.name]
  585. } else {
  586. const initialEntityOjectValue = getInitialEntityObjectValue(field.name)
  587. combinedFieldProps.value =
  588. initialEntityOjectValue !== undefined
  589. ? initialEntityOjectValue
  590. : combinedFieldProps.value
  591. }
  592. // Save current initial value for later usage.
  593. localInitialValues[field.name] = combinedFieldProps.value
  594. schemaData.fields[field.name] = {
  595. show: showField,
  596. updateFields: updateFields || false,
  597. staticCondition: showWithStaticCondition,
  598. props: combinedFieldProps,
  599. }
  600. }
  601. }
  602. const updateChangedFields = (
  603. changedFields: Record<string, Partial<FormSchemaField>>,
  604. ) => {
  605. const handleUpdatedInitialFieldValue = (
  606. fieldName: string,
  607. value: FormFieldValue,
  608. directly: boolean,
  609. field: Partial<FormSchemaField>,
  610. ) => {
  611. if (value === undefined) return
  612. if (directly) {
  613. field.value = value
  614. } else if (!formKitInitialNodesSettled.value) {
  615. changeInitialValue.set(fieldName, value)
  616. }
  617. }
  618. Object.keys(changedFields).forEach(async (fieldName) => {
  619. if (!schemaData.fields[fieldName]) return
  620. const { initialValue, value, ...changedFieldProps } =
  621. changedFields[fieldName]
  622. const field: SetRequired<Partial<FormSchemaField>, 'name'> = {
  623. ...changedFieldProps,
  624. name: fieldName,
  625. }
  626. const showField = !schemaData.fields[fieldName].show && field.show
  627. const staticShowCondition = schemaData.fields[fieldName].staticCondition
  628. const pendingValueUpdate =
  629. !showField &&
  630. value !== undefined &&
  631. !isEqual(value, values.value[fieldName])
  632. if (pendingValueUpdate) {
  633. field.pendingValueUpdate = true
  634. }
  635. // This happens for the initial updater, when the form is not settled yet or the field was not rendered yet.
  636. // In this case we need to remember the changes and do it afterwards after the form is settled the first time.
  637. // Sometimes the value from the server is the "real" initial value, for this the `initialValue` can be used.
  638. handleUpdatedInitialFieldValue(
  639. fieldName,
  640. value ?? initialValue,
  641. showField ||
  642. initialValue !== undefined ||
  643. (staticShowCondition && !getNodeByName(fieldName)),
  644. field,
  645. )
  646. // When a field will be visible with the update call, we need to wait before on a settled form, before we
  647. // continue (so that we have all values present inside the form).
  648. // This situtation can happen, when the form is used very fast.
  649. if (
  650. formKitInitialNodesSettled.value &&
  651. !schemaData.fields[fieldName].show &&
  652. field.show &&
  653. !formNode.value?.isSettled
  654. ) {
  655. await formNode.value?.settled
  656. }
  657. updaterChangedFields.add(fieldName)
  658. updateSchemaDataField(field)
  659. if (!formKitInitialNodesSettled.value) return
  660. if (pendingValueUpdate) {
  661. const node = field.id ? getNode(field.id) : getNodeByName(fieldName)
  662. // Update the value in the next tick, so that all other props are already updated.
  663. nextTick(() => {
  664. node?.input(value, false)
  665. })
  666. }
  667. })
  668. nextTick(() => {
  669. updaterChangedFields.clear()
  670. formNode.value?.store.remove('formUpdaterProcessing')
  671. })
  672. }
  673. const formHandlerExecution: Record<
  674. FormHandlerExecution,
  675. FormHandlerFunction[]
  676. > = {
  677. [FormHandlerExecution.Initial]: [],
  678. [FormHandlerExecution.InitialSettled]: [],
  679. [FormHandlerExecution.FieldChange]: [],
  680. }
  681. if (props.handlers) {
  682. props.handlers.forEach((handler) => {
  683. Object.values(FormHandlerExecution).forEach((execution) => {
  684. if (handler.execution.includes(execution)) {
  685. formHandlerExecution[execution].push(handler.callback)
  686. }
  687. })
  688. })
  689. }
  690. const executeFormHandler = (
  691. execution: FormHandlerExecution,
  692. currentValues: FormValues,
  693. changedField?: ChangedField,
  694. ) => {
  695. if (formHandlerExecution[execution].length === 0) return
  696. formHandlerExecution[execution].forEach((handler) => {
  697. handler(
  698. execution,
  699. {
  700. changeFields,
  701. updateSchemaDataField,
  702. schemaData,
  703. },
  704. {
  705. formNode: formNode.value,
  706. getNodeByName,
  707. findNodeByName,
  708. values: currentValues,
  709. changedField,
  710. initialEntityObject: props.initialEntityObject,
  711. },
  712. )
  713. })
  714. }
  715. const formUpdaterVariables = shallowRef<FormUpdaterQueryVariables>()
  716. let nextFormUpdaterVariables: Maybe<FormUpdaterQueryVariables>
  717. const executeFormUpdaterRefetch = () => {
  718. if (!nextFormUpdaterVariables) return
  719. formNode.value?.store.set(
  720. createMessage({
  721. blocking: true,
  722. key: 'formUpdaterProcessing',
  723. value: true,
  724. visible: false,
  725. }),
  726. )
  727. formUpdaterVariables.value = nextFormUpdaterVariables
  728. // Reset the next variables so that it's not triggered a second time.
  729. nextFormUpdaterVariables = null
  730. }
  731. const handlesFormUpdater = (
  732. trigger: FormUpdaterTrigger,
  733. changedField?: FormUpdaterChangedFieldInput,
  734. options?: FormUpdaterOptions,
  735. ) => {
  736. if (!props.formUpdaterId || !formUpdaterQueryHandler) return
  737. // When formUpdaterInitial is set, trigger only on initial rendering and when the form was reseted.
  738. if (
  739. trigger !== 'manual' &&
  740. trigger !== 'form-reset' &&
  741. (!changedField || props.formUpdaterInitialOnly)
  742. )
  743. return
  744. const meta: FormUpdaterMetaInput = {
  745. // We need a unique requestId, so that the query will always be executed on changes, also when the variables
  746. // are the same until the last request, because it could be that core workflow is setting a value back.
  747. requestId: getUuid(),
  748. formId,
  749. additionalData: {
  750. ...props.formUpdaterAdditionalParams,
  751. ...options?.additionalParams,
  752. },
  753. }
  754. if (options?.includeDirtyFields) {
  755. const dirtyFields: string[] = []
  756. Object.entries(schemaData.fields).forEach(([field, { props }]) => {
  757. const formElement = props.id
  758. ? getNode(props.id)
  759. : getNodeByName(props.name)
  760. if (!formElement) return
  761. if (formElement.context?.state.dirty) {
  762. dirtyFields.push(field)
  763. }
  764. })
  765. meta.dirtyFields = dirtyFields
  766. }
  767. const data = {
  768. ...values.value,
  769. }
  770. if (trigger === 'form-reset') {
  771. meta.reset = true
  772. } else if (changedField) {
  773. meta.changedField = changedField
  774. data[changedField.name] = changedField.newValue
  775. }
  776. // We mark this as raw, because we want no deep reactivity on the form updater query variables.
  777. nextFormUpdaterVariables = markRaw({
  778. id: props.initialEntityObject?.id,
  779. formUpdaterId: props.formUpdaterId,
  780. data,
  781. meta,
  782. relationFields,
  783. })
  784. if (trigger !== 'blur') executeFormUpdaterRefetch()
  785. }
  786. const previousValues = new WeakMap<FormKitNode, FormFieldValue>()
  787. const changedInputValueHandling = (inputNode: FormKitNode) => {
  788. inputNode.on('commit', ({ payload: newValue, origin: node }) => {
  789. const oldValue = previousValues.get(node)
  790. if (isEqual(newValue, oldValue)) return
  791. if (!formKitInitialNodesSettled.value || formResetRunning.value) {
  792. previousValues.set(node, cloneDeep(newValue))
  793. return
  794. }
  795. if (
  796. inputNode.props.triggerFormUpdater &&
  797. !updaterChangedFields.has(node.name)
  798. ) {
  799. handlesFormUpdater(inputNode.props.formUpdaterTrigger, {
  800. name: node.name,
  801. newValue,
  802. oldValue,
  803. })
  804. }
  805. emit('changed', node.name, newValue, oldValue)
  806. formNode.value?.emit(`changed:${node.name}`, {
  807. newValue,
  808. oldValue,
  809. fieldNode: node,
  810. })
  811. executeFormHandler(FormHandlerExecution.FieldChange, values.value, {
  812. name: node.name,
  813. newValue,
  814. oldValue,
  815. })
  816. previousValues.set(node, cloneDeep(newValue))
  817. updaterChangedFields.delete(node.name)
  818. })
  819. inputNode.on('blur', async () => {
  820. if (inputNode.props.formUpdaterTrigger !== 'blur') return
  821. if (!formNode.value?.isSettled) await formNode.value?.settled
  822. if (nextFormUpdaterVariables) executeFormUpdaterRefetch()
  823. })
  824. inputNode.hook.message((payload: FormKitMessageProps, next) => {
  825. if (payload.key === 'submitted' && formUpdaterProcessing.value) {
  826. payload.value = false
  827. }
  828. return next(payload)
  829. })
  830. return false
  831. }
  832. const buildStaticSchema = () => {
  833. const { getFormFieldSchema, getFormFieldsFromScreen } =
  834. useObjectAttributeFormFields(fixedAndSkippedFields)
  835. const buildFormKitField = (
  836. field: FormSchemaField,
  837. ): FormKitSchemaComponent => {
  838. const fieldId = field.id || getNodeId(formId, field.name)
  839. const plugins = [changedInputValueHandling]
  840. if (field.plugins) {
  841. plugins.push(...field.plugins)
  842. }
  843. return {
  844. $cmp: 'FormKit',
  845. if: field.if ? field.if : `$fields.${field.name}.show`,
  846. bind: `$fields.${field.name}.props`,
  847. props: {
  848. type: field.type,
  849. key: fieldId,
  850. name: field.name,
  851. id: fieldId,
  852. formId,
  853. plugins,
  854. triggerFormUpdater: field.triggerFormUpdater ?? !!props.formUpdaterId,
  855. },
  856. }
  857. }
  858. const getLayoutType = (
  859. layoutNode: FormSchemaLayout,
  860. ): FormKitSchemaDOMNode | FormKitSchemaComponent => {
  861. let layoutField: FormKitSchemaDOMNode | FormKitSchemaComponent
  862. if ('component' in layoutNode) {
  863. layoutField = {
  864. $cmp: layoutNode.component,
  865. ...(layoutNode.if && { if: layoutNode.if }),
  866. props: layoutNode.props,
  867. }
  868. } else {
  869. layoutField = {
  870. $el: layoutNode.element,
  871. ...(layoutNode.if && { if: layoutNode.if }),
  872. attrs: layoutNode.attrs,
  873. }
  874. }
  875. if (layoutNode.if) {
  876. layoutField.if = layoutNode.if
  877. }
  878. return layoutField
  879. }
  880. type ResolveFormSchemaNode = Exclude<FormSchemaNode, string>
  881. type ResolveFormKitSchemaNode = Exclude<FormKitSchemaNode, string>
  882. const resolveSchemaNode = (
  883. node: ResolveFormSchemaNode,
  884. ): Maybe<ResolveFormKitSchemaNode | ResolveFormKitSchemaNode[]> => {
  885. if ('isLayout' in node && node.isLayout) {
  886. return getLayoutType(node)
  887. }
  888. if ('isGroupOrList' in node && node.isGroupOrList) {
  889. const nodeId = `${node.name}-${formId}`
  890. return {
  891. $cmp: 'FormKit',
  892. ...(node.if && { if: node.if }),
  893. props: {
  894. type: node.type,
  895. name: node.name,
  896. id: nodeId,
  897. key: node.name,
  898. plugins: node.plugins,
  899. },
  900. }
  901. }
  902. if ('object' in node && getFormFieldSchema && getFormFieldsFromScreen) {
  903. if ('name' in node && node.name && !node.type) {
  904. const { screen, object, ...fieldNode } = node
  905. const resolvedField = getFormFieldSchema(fieldNode.name, object, screen)
  906. if (!resolvedField) return null
  907. node = {
  908. ...resolvedField,
  909. ...fieldNode,
  910. } as FormSchemaField
  911. } else if ('screen' in node && !('name' in node)) {
  912. const resolvedFields = getFormFieldsFromScreen(node.screen, node.object)
  913. const formKitFields: ResolveFormKitSchemaNode[] = []
  914. resolvedFields.forEach((screenField) => {
  915. updateSchemaDataField(screenField)
  916. formKitFields.push(buildFormKitField(screenField))
  917. })
  918. return formKitFields
  919. }
  920. }
  921. updateSchemaDataField(node as FormSchemaField)
  922. return buildFormKitField(node as FormSchemaField)
  923. }
  924. const resolveSchema = (schema: FormSchemaNode[] = props.schema) => {
  925. return schema.reduce((resolvedSchema: FormKitSchemaNode[], node) => {
  926. if (typeof node === 'string') {
  927. resolvedSchema.push(node)
  928. return resolvedSchema
  929. }
  930. const resolvedNode = resolveSchemaNode(node)
  931. if (!resolvedNode) return resolvedSchema
  932. if ('children' in node) {
  933. const childrens = Array.isArray(node.children)
  934. ? [...resolveSchema(node.children)]
  935. : node.children
  936. resolvedSchema.push({
  937. ...(resolvedNode as Exclude<FormKitSchemaNode, string>),
  938. children: childrens,
  939. })
  940. return resolvedSchema
  941. }
  942. if (Array.isArray(resolvedNode)) {
  943. resolvedSchema.push(...resolvedNode)
  944. } else {
  945. resolvedSchema.push(resolvedNode)
  946. }
  947. return resolvedSchema
  948. }, [])
  949. }
  950. staticSchema.value = resolveSchema()
  951. }
  952. watchOnce(formKitInitialNodesSettled, () => {
  953. watch(
  954. changeFields,
  955. (newValue) => {
  956. updateChangedFields(newValue)
  957. },
  958. {
  959. deep: true,
  960. },
  961. )
  962. })
  963. watch(
  964. () => props.schemaData,
  965. () => Object.assign(schemaData, props.schemaData),
  966. {
  967. deep: true,
  968. },
  969. )
  970. const setFormSchemaInitialized = () => {
  971. if (!formSchemaInitialized.value) {
  972. formSchemaInitialized.value = true
  973. }
  974. }
  975. const { notify, removeNotification } = useNotifications()
  976. let formUpdaterQueryLoadingTimeoutId: NodeJS.Timeout | null
  977. const clearFormUpdaterQueryLoadingTimeout = () => {
  978. if (!formUpdaterQueryLoadingTimeoutId) return
  979. clearTimeout(formUpdaterQueryLoadingTimeoutId)
  980. formUpdaterQueryLoadingTimeoutId = null
  981. }
  982. const cleanupFormUpdaterAutosaveNotification = () => {
  983. removeNotification('form-updater-autosave')
  984. clearFormUpdaterQueryLoadingTimeout()
  985. }
  986. const handleFormUpdaterAutosaveNotification = () => {
  987. if (
  988. !formUpdaterVariables.value?.meta.additionalData?.taskbarId &&
  989. !formUpdaterVariables.value?.meta.additionalData?.applyTaskbarState
  990. )
  991. return
  992. // Clean up previous notification and timeout.
  993. cleanupFormUpdaterAutosaveNotification()
  994. const formUpdaterQueryLoading = formUpdaterQueryHandler.loading()
  995. watch(formUpdaterQueryLoading, (isLoading) => {
  996. if (!isLoading) {
  997. cleanupFormUpdaterAutosaveNotification()
  998. return
  999. }
  1000. // Clear previous timeout.
  1001. clearFormUpdaterQueryLoadingTimeout()
  1002. formUpdaterQueryLoadingTimeoutId = setTimeout(() => {
  1003. // Show info notification if the request takes longer than a second.
  1004. notify({
  1005. id: 'form-updater-autosave',
  1006. message: __('Autosave in progress…'),
  1007. type: NotificationTypes.Info,
  1008. persistent: true,
  1009. })
  1010. // Show warning notification if the request takes longer than five seconds.
  1011. formUpdaterQueryLoadingTimeoutId = setTimeout(() => {
  1012. notify({
  1013. id: 'form-updater-autosave',
  1014. message: __('Autosaving is taking longer than expected…'),
  1015. type: NotificationTypes.Warn,
  1016. persistent: true,
  1017. })
  1018. }, 4000)
  1019. }, 1000)
  1020. })
  1021. }
  1022. onBeforeUnmount(cleanupFormUpdaterAutosaveNotification)
  1023. const initializeFormSchema = () => {
  1024. buildStaticSchema()
  1025. if (props.formUpdaterId) {
  1026. formUpdaterVariables.value = markRaw({
  1027. id: props.initialEntityObject?.id,
  1028. formUpdaterId: props.formUpdaterId,
  1029. data: localInitialValues,
  1030. meta: {
  1031. initial: true,
  1032. additionalData: props.formUpdaterAdditionalParams,
  1033. formId,
  1034. },
  1035. relationFields,
  1036. })
  1037. formUpdaterQueryHandler = new QueryHandler(
  1038. useFormUpdaterQuery(
  1039. formUpdaterVariables as Ref<FormUpdaterQueryVariables>,
  1040. {
  1041. fetchPolicy: 'no-cache',
  1042. },
  1043. ),
  1044. )
  1045. handleFormUpdaterAutosaveNotification()
  1046. formUpdaterQueryHandler.onResult((queryResult) => {
  1047. // Execute the form handler function so that they can manipulate the form updater result.
  1048. if (!formSchemaInitialized.value) {
  1049. executeFormHandler(FormHandlerExecution.Initial, localInitialValues)
  1050. }
  1051. if (queryResult?.data?.formUpdater) {
  1052. updateChangedFields(
  1053. changeFields.value
  1054. ? merge(queryResult.data.formUpdater, changeFields.value)
  1055. : queryResult.data.formUpdater,
  1056. )
  1057. }
  1058. setFormSchemaInitialized()
  1059. })
  1060. } else {
  1061. executeFormHandler(FormHandlerExecution.Initial, localInitialValues)
  1062. if (changeFields.value) updateChangedFields(changeFields.value)
  1063. setFormSchemaInitialized()
  1064. }
  1065. }
  1066. // TODO: maybe we should react on schema changes and rebuild the static schema with a new form-id and re-rendering of
  1067. // the complete form (= use the formId as the key for the whole form to trigger the re-rendering of the component...)
  1068. if (props.schema) {
  1069. showInitialLoadingAnimation.value = true
  1070. if (props.useObjectAttributes) {
  1071. // TODO: rebuild schema, when object attributes
  1072. // was changed from outside(not such important,
  1073. // because we have currently the reload solution like in the desktop view).
  1074. if (props.objectAttributeSkippedFields) {
  1075. fixedAndSkippedFields.push(...props.objectAttributeSkippedFields)
  1076. }
  1077. const objectAttributeObjects: EnumObjectManagerObjects[] = []
  1078. const addObjectAttributeToObjects = (object: EnumObjectManagerObjects) => {
  1079. if (objectAttributeObjects.includes(object)) return
  1080. objectAttributeObjects.push(object)
  1081. }
  1082. const detectObjectAttributeObjects = (
  1083. schema: FormSchemaNode[] = props.schema,
  1084. ) => {
  1085. schema.forEach((item) => {
  1086. if (typeof item === 'string') return
  1087. if ('object' in item) {
  1088. if ('name' in item && item.name && !item.type) {
  1089. fixedAndSkippedFields.push(item.name)
  1090. }
  1091. addObjectAttributeToObjects(item.object)
  1092. }
  1093. if ('children' in item && Array.isArray(item.children)) {
  1094. detectObjectAttributeObjects(item.children)
  1095. }
  1096. })
  1097. }
  1098. detectObjectAttributeObjects()
  1099. // We need only to fetch object attributes, when there are used in the given schema.
  1100. if (objectAttributeObjects.length > 0) {
  1101. const { objectAttributesLoading } = useObjectAttributeLoadFormFields(
  1102. objectAttributeObjects,
  1103. )
  1104. const unwatchTriggerFormInitialize = watch(
  1105. objectAttributesLoading,
  1106. (loading) => {
  1107. if (!loading) {
  1108. nextTick(() => unwatchTriggerFormInitialize())
  1109. initializeFormSchema()
  1110. }
  1111. },
  1112. { immediate: true },
  1113. )
  1114. } else {
  1115. initializeFormSchema()
  1116. }
  1117. } else {
  1118. initializeFormSchema()
  1119. }
  1120. }
  1121. const classMap = getFormClasses()
  1122. defineExpose({
  1123. formNode,
  1124. formId,
  1125. values,
  1126. updateChangedFields,
  1127. updateSchemaDataField,
  1128. getNodeByName,
  1129. findNodeByName,
  1130. resetForm,
  1131. triggerFormUpdater,
  1132. })
  1133. </script>
  1134. <script lang="ts">
  1135. export default {
  1136. inheritAttrs: false,
  1137. }
  1138. </script>
  1139. <template>
  1140. <div
  1141. v-if="debouncedShowInitialLoadingAnimation"
  1142. class="flex items-center justify-center"
  1143. >
  1144. <CommonIcon :class="classMap.loading" name="loading" animation="spin" />
  1145. </div>
  1146. <FormKit
  1147. v-if="
  1148. hasSchema &&
  1149. ((formSchemaInitialized && Object.keys(schemaData.fields).length > 0) ||
  1150. $slots.default)
  1151. "
  1152. v-bind="$attrs"
  1153. :id="id"
  1154. type="form"
  1155. novalidate
  1156. :config="formConfig"
  1157. :form-class="localClass"
  1158. :actions="false"
  1159. :incomplete-message="false"
  1160. :plugins="localFormKitPlugins"
  1161. :sections-schema="formKitSectionsSchema"
  1162. :disabled="disabled"
  1163. @node="setFormNode"
  1164. @submit="onSubmit"
  1165. @submit-raw="onSubmitRaw"
  1166. >
  1167. <FormKitMessages
  1168. :sections-schema="{
  1169. messages: {
  1170. $el: 'div',
  1171. },
  1172. message: {
  1173. $el: undefined,
  1174. $cmp: 'CommonAlert',
  1175. props: {
  1176. id: `$id + '-' + $message.key`,
  1177. key: '$message.key',
  1178. variant: {
  1179. if: '$message.type == error || $message.type == validation',
  1180. then: 'danger',
  1181. else: '$message.type',
  1182. },
  1183. },
  1184. slots: {
  1185. default: '$message.value',
  1186. },
  1187. },
  1188. }"
  1189. />
  1190. <slot name="before-fields" />
  1191. <slot
  1192. name="default"
  1193. :schema="staticSchema"
  1194. :data="schemaData"
  1195. :library="additionalComponentLibrary"
  1196. >
  1197. <div
  1198. v-show="
  1199. formKitInitialNodesSettled && !debouncedShowInitialLoadingAnimation
  1200. "
  1201. ref="formElement"
  1202. :class="formClass"
  1203. >
  1204. <FormKitSchema
  1205. :schema="staticSchema"
  1206. :data="schemaData"
  1207. :library="additionalComponentLibrary"
  1208. />
  1209. </div>
  1210. </slot>
  1211. <slot name="after-fields" />
  1212. </FormKit>
  1213. </template>