index.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import {
  3. button as buttonDefinition,
  4. submit as submitDefinition,
  5. } from '@formkit/inputs'
  6. import { has } from '@formkit/utils'
  7. import initializeFieldDefinition from '#shared/form/core/initializeFieldDefinition.ts'
  8. import extendSchemaDefinition from '#shared/form/utils/extendSchemaDefinition.ts'
  9. import type {
  10. FormFieldsTypeDefinition,
  11. FormFieldType,
  12. } from '#shared/types/form.ts'
  13. import type { FormKitNode } from '@formkit/core'
  14. // TODO: Build-In loading cycle funcitonality for the buttons or at least a disabled-state when loading is in progress?
  15. const addVariantDataAttribute = (node: FormKitNode) => {
  16. extendSchemaDefinition(node, 'wrapper', {
  17. attrs: {
  18. 'data-variant': '$variant',
  19. },
  20. })
  21. }
  22. const setVariantDefault = (node: FormKitNode) => {
  23. const { props } = node
  24. node.addProps(['variant'])
  25. node.on('created', () => {
  26. if (!has(props, 'variant')) {
  27. props.variant = 'primary'
  28. }
  29. })
  30. }
  31. const buttonFieldDefinitionList: FormFieldsTypeDefinition = {
  32. button: buttonDefinition,
  33. submit: submitDefinition,
  34. }
  35. const buttonInputs: FormFieldType[] = []
  36. Object.keys(buttonFieldDefinitionList).forEach((buttonType) => {
  37. initializeFieldDefinition(buttonFieldDefinitionList[buttonType], {
  38. features: [setVariantDefault, addVariantDataAttribute],
  39. })
  40. buttonInputs.push({
  41. fieldType: buttonType,
  42. definition: buttonFieldDefinitionList[buttonType],
  43. })
  44. })
  45. export default buttonInputs