getFieldFromAttribute.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type {
  3. FormSchemaField,
  4. FormFieldValue,
  5. } from '#shared/components/Form/types.ts'
  6. import type {
  7. EnumObjectManagerObjects,
  8. ObjectManagerFrontendAttribute,
  9. } from '#shared/graphql/types.ts'
  10. import getFieldResolver from './resolver/getFieldResolver.ts'
  11. import type { ScreenConfig } from '../types/resolver.ts'
  12. export const transformResolvedFieldForScreen = (
  13. screen: ScreenConfig,
  14. resolvedField: FormSchemaField,
  15. ) => {
  16. resolvedField.required = screen.required || ('null' in screen && !screen.null)
  17. if ('default' in screen) {
  18. resolvedField.value = screen.default as FormFieldValue
  19. }
  20. if ('filter' in screen && resolvedField.relation) {
  21. resolvedField.relation.filterIds = screen.filter as number[]
  22. }
  23. // Special handling for the clearable prop in the select/treeselect/autocomplete fields.
  24. if (
  25. 'nulloption' in screen &&
  26. resolvedField.props &&
  27. 'clearable' in resolvedField.props
  28. ) {
  29. resolvedField.props.clearable = screen.nulloption
  30. }
  31. }
  32. const getFieldFromAttribute = (
  33. object: EnumObjectManagerObjects,
  34. attribute: ObjectManagerFrontendAttribute,
  35. ): FormSchemaField => {
  36. const fieldResolver = getFieldResolver(object, attribute)
  37. return fieldResolver.fieldAttributes()
  38. }
  39. export default getFieldFromAttribute