getFieldFromAttribute.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 { EnumObjectManagerObjects } from '#shared/graphql/types.ts'
  7. import getFieldResolver from './resolver/getFieldResolver.ts'
  8. import type { ScreenConfig } from '../types/resolver.ts'
  9. import type { ObjectAttribute } from '../types/store.ts'
  10. export const transformResolvedFieldForScreen = (
  11. screen: ScreenConfig,
  12. resolvedField: FormSchemaField,
  13. ) => {
  14. resolvedField.required = screen.required || ('null' in screen && !screen.null)
  15. if ('default' in screen) {
  16. resolvedField.value = screen.default as FormFieldValue
  17. }
  18. if ('filter' in screen && resolvedField.relation) {
  19. resolvedField.relation.filterIds = screen.filter as number[]
  20. }
  21. // Special handling for the clearable prop in the select/treeselect/autocomplete fields.
  22. if (
  23. 'nulloption' in screen &&
  24. resolvedField.props &&
  25. 'clearable' in resolvedField.props
  26. ) {
  27. resolvedField.props.clearable = screen.nulloption
  28. }
  29. }
  30. const getFieldFromAttribute = (
  31. object: EnumObjectManagerObjects,
  32. attribute: ObjectAttribute,
  33. ): FormSchemaField => {
  34. const fieldResolver = getFieldResolver(object, attribute)
  35. return fieldResolver.fieldAttributes()
  36. }
  37. export default getFieldFromAttribute