index.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { dateFieldProps } from '#shared/components/Form/fields/FieldDate/types.ts'
  3. import createInput from '#shared/form/core/createInput.ts'
  4. import addLink from '#shared/form/features/addLink.ts'
  5. import formUpdaterTrigger from '#shared/form/features/formUpdaterTrigger.ts'
  6. import FieldDateTimeInput from './FieldDateTimeInput.vue'
  7. import type { FormKitNode, FormKitProps } from '@formkit/core'
  8. const addDateRangeValidation = (node: FormKitNode) => {
  9. const addDataRangeValidation = (props: Partial<FormKitProps>) => {
  10. const { validation } = props
  11. if (Array.isArray(validation)) {
  12. validation.push(['date_range'])
  13. return
  14. }
  15. if (!validation) {
  16. props.validation = 'date_range'
  17. return
  18. }
  19. if (!validation.includes('required')) {
  20. props.validation = `${validation}|date_range`
  21. }
  22. }
  23. const removeDataRangeValidation = (props: Partial<FormKitProps>) => {
  24. const { validation } = props
  25. if (!validation) return
  26. if (Array.isArray(validation)) {
  27. props.validation = validation.filter(([rule]) => rule !== 'date_range')
  28. return
  29. }
  30. if (validation.includes('date_range')) {
  31. props.validation = validation
  32. .split('|')
  33. .filter((rule: string) => !rule.includes('date_range'))
  34. .join('|')
  35. }
  36. }
  37. node.on('created', () => {
  38. const { props } = node
  39. if (props.range) {
  40. addDataRangeValidation(props)
  41. }
  42. node.on('prop:range', ({ payload }) => {
  43. if (payload) {
  44. addDataRangeValidation(props)
  45. } else {
  46. removeDataRangeValidation(props)
  47. }
  48. })
  49. })
  50. }
  51. const dateFieldDefinition = createInput(FieldDateTimeInput, dateFieldProps, {
  52. features: [addLink, formUpdaterTrigger(), addDateRangeValidation],
  53. })
  54. export default [
  55. {
  56. fieldType: 'date',
  57. definition: dateFieldDefinition,
  58. },
  59. {
  60. fieldType: 'datetime',
  61. definition: dateFieldDefinition,
  62. },
  63. ]