index.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { checkbox as checkboxDefinition } from '@formkit/inputs'
  3. import { has } from '@formkit/utils'
  4. import { useAppName } from '#shared/composables/useAppName.ts'
  5. import initializeFieldDefinition from '#shared/form/core/initializeFieldDefinition.ts'
  6. import formUpdaterTrigger from '#shared/form/features/formUpdaterTrigger.ts'
  7. import extendSchemaDefinition from '#shared/form/utils/extendSchemaDefinition.ts'
  8. import type { FormKitNode } from '@formkit/core'
  9. const addCheckedDataAttribute = (node: FormKitNode) => {
  10. extendSchemaDefinition(node, 'wrapper', {
  11. attrs: {
  12. 'data-is-checked': {
  13. if: '$value',
  14. then: 'true',
  15. else: undefined,
  16. },
  17. 'data-test-id': 'checkbox-label',
  18. },
  19. })
  20. }
  21. const handleAlternativeBorder = (node: FormKitNode) => {
  22. // The alternative border color below is specific to desktop field design only.
  23. if (useAppName() !== 'desktop') return
  24. const { props } = node
  25. node.addProps(['alternativeBorder'])
  26. const setClasses = (alternativeBorder: boolean) => {
  27. if (alternativeBorder) {
  28. props.decoratorClass =
  29. 'border-stone-200 dark:border-neutral-500 text-stone-200 dark:text-neutral-500 formkit-checked:border-gray-300 dark:formkit-checked:border-neutral-400 formkit-checked:text-gray-300 dark:formkit-checked:text-neutral-400'
  30. } else {
  31. props.decoratorClass =
  32. 'border-stone-200 dark:border-neutral-500 text-stone-200 dark:text-neutral-500 formkit-checked:border-gray-100 dark:formkit-checked:border-neutral-400 formkit-checked:text-gray-100 dark:formkit-checked:text-neutral-400'
  33. }
  34. }
  35. node.on('created', () => {
  36. if (!has(props, 'alternativeBorder')) props.alternativeBorder = false
  37. setClasses(props.alternativeBorder)
  38. node.on('prop:alternativeBorder', ({ payload }) => {
  39. setClasses(payload)
  40. })
  41. })
  42. }
  43. initializeFieldDefinition(checkboxDefinition, {
  44. props: ['alternativeBorder'],
  45. features: [
  46. addCheckedDataAttribute,
  47. handleAlternativeBorder,
  48. formUpdaterTrigger(),
  49. ],
  50. })
  51. export default {
  52. fieldType: 'checkbox',
  53. definition: checkboxDefinition,
  54. }