initializeFieldDefinition.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import {
  3. outer,
  4. inner,
  5. wrapper,
  6. icon,
  7. help,
  8. messages,
  9. message,
  10. prefix,
  11. suffix,
  12. label,
  13. } from '@formkit/inputs'
  14. import { mergeArray } from '#shared/utils/helpers.ts'
  15. import { arrow } from '../sections/arrow.ts'
  16. import { block } from '../sections/block.ts'
  17. import { link } from '../sections/link.ts'
  18. import defaulfFieldDefinition from './defaultFieldDefinition.ts'
  19. import type { FormKitTypeDefinition } from '@formkit/core'
  20. import type {
  21. FormKitSchemaExtendableSection,
  22. FormKitInputs,
  23. FormKitSlotData,
  24. } from '@formkit/inputs'
  25. declare module '@formkit/inputs' {
  26. interface FormKitBaseSlots<Props extends FormKitInputs<Props>> {
  27. // Add new slots or modify existing ones directly here
  28. link: FormKitSlotData<Props>
  29. }
  30. }
  31. export interface FieldsCustomOptions {
  32. addDefaultProps?: boolean
  33. addDefaultFeatures?: boolean
  34. addArrow?: boolean
  35. schema?: () => FormKitSchemaExtendableSection
  36. }
  37. const initializeFieldDefinition = (
  38. definition: FormKitTypeDefinition,
  39. additionalDefinitionOptions: Pick<
  40. FormKitTypeDefinition,
  41. 'props' | 'features'
  42. > = {},
  43. options: FieldsCustomOptions = {},
  44. ) => {
  45. const {
  46. addDefaultProps = true,
  47. addDefaultFeatures = true,
  48. addArrow = false,
  49. } = options
  50. const localDefinition = definition
  51. localDefinition.props = Array.isArray(localDefinition.props)
  52. ? localDefinition.props
  53. : []
  54. localDefinition.features ||= []
  55. if (options.schema) {
  56. const wrapperSchema = wrapper(
  57. label('$label'),
  58. inner(
  59. icon('prefix', 'label'),
  60. prefix(),
  61. options.schema(),
  62. suffix(),
  63. icon('suffix'),
  64. ),
  65. )
  66. const blockSchema = [wrapperSchema]
  67. if (addArrow) {
  68. blockSchema.push(arrow())
  69. }
  70. blockSchema.push(link())
  71. localDefinition.schema = outer(
  72. block(...blockSchema),
  73. help('$help'),
  74. messages(message('$message.value')),
  75. )
  76. }
  77. const additionalProps = Array.isArray(additionalDefinitionOptions.props)
  78. ? additionalDefinitionOptions.props
  79. : []
  80. if (addDefaultProps) {
  81. const defaulfFieldDefinitionProps = Array.isArray(
  82. defaulfFieldDefinition.props,
  83. )
  84. ? defaulfFieldDefinition.props
  85. : []
  86. localDefinition.props = mergeArray(
  87. localDefinition.props,
  88. defaulfFieldDefinitionProps.concat(additionalProps),
  89. )
  90. }
  91. const additionalFeatures = additionalDefinitionOptions.features || []
  92. if (addDefaultFeatures) {
  93. localDefinition.features = mergeArray(
  94. defaulfFieldDefinition.features.concat(additionalFeatures),
  95. localDefinition.features,
  96. )
  97. }
  98. }
  99. export default initializeFieldDefinition