FieldResolver.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type {
  3. FormFieldValue,
  4. FormSchemaField,
  5. } from '#shared/components/Form/types.ts'
  6. import type {
  7. EnumObjectManagerObjects,
  8. ObjectManagerFrontendAttribute,
  9. } from '#shared/graphql/types.ts'
  10. import type { JsonValue } from 'type-fest'
  11. export default abstract class FieldResolver {
  12. protected name: string
  13. protected object: EnumObjectManagerObjects
  14. protected label: string
  15. protected internal: boolean
  16. protected attributeType: string
  17. protected attributeConfig: Record<string, JsonValue>
  18. abstract fieldType: string | (() => string)
  19. constructor(
  20. object: EnumObjectManagerObjects,
  21. objectAttribute: ObjectManagerFrontendAttribute,
  22. ) {
  23. this.object = object
  24. this.name = objectAttribute.name
  25. this.label = objectAttribute.display
  26. this.internal = objectAttribute.isInternal
  27. this.attributeType = objectAttribute.dataType
  28. this.attributeConfig = objectAttribute.dataOption
  29. }
  30. private getFieldType(): string {
  31. if (typeof this.fieldType === 'function') {
  32. return this.fieldType()
  33. }
  34. return this.fieldType
  35. }
  36. public fieldAttributes(): FormSchemaField {
  37. const resolvedAttributes: FormSchemaField = {
  38. type: this.getFieldType(),
  39. label: this.label,
  40. name: this.name,
  41. required: 'null' in this.attributeConfig && !this.attributeConfig.null, // will normally be overriden with the screen config
  42. internal: this.internal,
  43. ...this.fieldTypeAttributes(),
  44. }
  45. if (this.attributeConfig.default) {
  46. resolvedAttributes.value = this.attributeConfig.default as FormFieldValue
  47. }
  48. // TODO: Support half-sized/single column fields based on the information hard-coded in the object attribute
  49. // backend for now. Later we can make this a concern of the frontend only, and ignore the hard-coded values.
  50. if (
  51. this.attributeConfig.item_class &&
  52. (this.attributeConfig.item_class as string).indexOf(
  53. 'formGroup--halfSize',
  54. ) !== -1
  55. ) {
  56. resolvedAttributes.outerClass = 'form-group-single-column'
  57. }
  58. return resolvedAttributes
  59. }
  60. abstract fieldTypeAttributes(): Partial<FormSchemaField>
  61. }