FieldResolver.ts 2.2 KB

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