index.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. import FieldEditorInner from '@common/components/form/field/FieldEditor/FieldEditorInner.vue'
  3. import createInput from '@common/form/core/createInput'
  4. import { FormKitExtendableSchemaRoot, FormKitNode } from '@formkit/core'
  5. import { cloneDeep } from 'lodash-es'
  6. function addAriaLabel(node: FormKitNode) {
  7. const { props } = node
  8. if (!props.definition) return
  9. const definition = cloneDeep(props.definition)
  10. const originalSchema = definition.schema as FormKitExtendableSchemaRoot
  11. // Specification doesn't allow accessing non-labeled elements, which Editor is (<div />)
  12. // (https://html.spec.whatwg.org/multipage/forms.html#category-label)
  13. // So, editor has `aria-labelledby` attribute and a label with the same ID
  14. definition.schema = (definition) => {
  15. const localDefinition = {
  16. ...definition,
  17. label: {
  18. attrs: {
  19. id: props.id,
  20. },
  21. },
  22. }
  23. return originalSchema(localDefinition)
  24. }
  25. props.definition = definition
  26. }
  27. const fieldDefinition = createInput(FieldEditorInner, [], {
  28. features: [addAriaLabel],
  29. })
  30. export default {
  31. fieldType: 'editor',
  32. definition: fieldDefinition,
  33. }