TableRow.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <!-- Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/ -->
  2. <script setup lang="ts">
  3. import { computed } from 'vue'
  4. import type { TableItem } from './types'
  5. export interface Props {
  6. item: TableItem
  7. onClickRow?: (tableItem: TableItem) => void
  8. isRowSelected?: boolean
  9. hasCheckbox?: boolean
  10. withEvenStripes?: boolean
  11. }
  12. const props = defineProps<Props>()
  13. const emit = defineEmits<{
  14. 'click-row': [TableItem]
  15. }>()
  16. const rowId = 'selectable-table-row'
  17. const rowEventHandler = computed(() =>
  18. (props.onClickRow || props.hasCheckbox) && !props.item.disabled
  19. ? {
  20. attrs: {
  21. 'aria-describedby': rowId,
  22. tabindex: props.hasCheckbox ? -1 : 0,
  23. class:
  24. 'group focus-visible:outline-transparent cursor-pointer active:bg-blue-800 active:dark:bg-blue-800 focus-visible:bg-blue-800 focus-visible:dark:bg-blue-900 focus-within:text-white hover:bg-blue-600 dark:hover:bg-blue-900',
  25. },
  26. events: {
  27. click: () => {
  28. ;(document.activeElement as HTMLElement)?.blur()
  29. emit('click-row', props.item)
  30. },
  31. keydown: (event: KeyboardEvent) => {
  32. if (event.key !== 'Enter') return
  33. emit('click-row', props.item)
  34. },
  35. },
  36. }
  37. : { attrs: {}, events: {} },
  38. )
  39. const hasScreenReaderHelpText = computed(
  40. () => !!document?.getElementById(rowId),
  41. )
  42. </script>
  43. <template>
  44. <tr
  45. :class="{
  46. 'odd:bg-blue-200 odd:dark:bg-gray-700': !withEvenStripes,
  47. 'even:bg-blue-200 even:dark:bg-gray-700': withEvenStripes,
  48. '!bg-blue-800': !hasCheckbox && isRowSelected,
  49. }"
  50. style="clip-path: xywh(0 0 100% 100% round 0.375rem)"
  51. data-test-id="table-row"
  52. v-bind="rowEventHandler.attrs"
  53. v-on="rowEventHandler.events"
  54. >
  55. <slot :is-row-selected="isRowSelected" />
  56. <template v-if="!hasScreenReaderHelpText">
  57. <Teleport to="body">
  58. <p
  59. v-if="rowEventHandler.attrs['aria-describedby']"
  60. :id="rowId"
  61. class="sr-only absolute"
  62. >
  63. {{ __('Select table row') }}
  64. </p>
  65. </Teleport>
  66. </template>
  67. </tr>
  68. </template>