treeselect.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type {
  3. FieldResolverModule,
  4. ObjectAttributeTreeSelectOption,
  5. } from '#shared/entities/object-attributes/types/resolver.ts'
  6. import { FieldResolverSelect } from './select.ts'
  7. export interface ObjectTreeSelectOption {
  8. label?: string
  9. value: string
  10. children?: ObjectTreeSelectOption[]
  11. }
  12. export class FieldResolverTreeselect extends FieldResolverSelect {
  13. fieldType = 'treeselect'
  14. multiFieldAttributeType = 'multi_tree_select'
  15. mappedOptions(): ObjectTreeSelectOption[] {
  16. const mapTreeSelectOptions = (
  17. options: ObjectAttributeTreeSelectOption[],
  18. ) => {
  19. return options.reduce(
  20. (
  21. treeSelectOptions: ObjectTreeSelectOption[],
  22. { children, name, value },
  23. ) => {
  24. const treeSelectOption: ObjectTreeSelectOption = {
  25. label: name,
  26. value,
  27. }
  28. if (children) {
  29. treeSelectOption.children = mapTreeSelectOptions(children)
  30. }
  31. treeSelectOptions.push(treeSelectOption)
  32. return treeSelectOptions
  33. },
  34. [],
  35. )
  36. }
  37. return mapTreeSelectOptions(
  38. this.attributeConfig
  39. .options as unknown as ObjectAttributeTreeSelectOption[],
  40. )
  41. }
  42. }
  43. export default <FieldResolverModule>{
  44. type: 'tree_select',
  45. resolver: FieldResolverTreeselect,
  46. }