Skip to content

Commit ea7050a

Browse files
committed
refactor(Utils): move getNextActiveElement from CDropdown utils to utils
1 parent 0b77985 commit ea7050a

File tree

5 files changed

+28
-26
lines changed

5 files changed

+28
-26
lines changed

packages/coreui-vue/src/components/dropdown/CDropdown.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import type { Placement } from '@popperjs/core'
33

44
import { usePopper } from '../../composables'
55
import type { Triggers } from '../../types'
6-
import { isRTL } from '../../utils'
6+
import { getNextActiveElement, isRTL } from '../../utils'
77

88
import type { Alignments } from './types'
9-
import { getNextActiveElement, getPlacement } from './utils'
9+
import { getPlacement } from './utils'
1010

1111
const CDropdown = defineComponent({
1212
name: 'CDropdown',

packages/coreui-vue/src/components/dropdown/utils.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,6 @@ export const getAlignmentClassNames = (alignment: Alignments) => {
1919
return classNames
2020
}
2121

22-
export const getNextActiveElement = (
23-
list: HTMLElement[],
24-
activeElement: HTMLElement,
25-
shouldGetNext: boolean,
26-
isCycleAllowed: boolean,
27-
) => {
28-
const listLength = list.length
29-
let index = list.indexOf(activeElement)
30-
31-
if (index === -1) {
32-
return !shouldGetNext && isCycleAllowed ? list[listLength - 1] : list[0]
33-
}
34-
35-
index += shouldGetNext ? 1 : -1
36-
37-
if (isCycleAllowed) {
38-
index = (index + listLength) % listLength
39-
}
40-
41-
return list[Math.max(0, Math.min(index, listLength - 1))]
42-
}
43-
4422
export const getPlacement = (
4523
placement: Placement,
4624
direction: string | undefined,

packages/coreui-vue/src/components/tabs/CTabList.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { defineComponent, h, ref } from 'vue'
2-
import { getNextActiveElement } from '../dropdown/utils'
2+
import { getNextActiveElement } from '../../utils'
33

44
const CTabList = defineComponent({
55
name: 'CTabList',
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const getNextActiveElement = (
2+
list: HTMLElement[],
3+
activeElement: HTMLElement,
4+
shouldGetNext: boolean,
5+
isCycleAllowed: boolean,
6+
) => {
7+
const listLength = list.length
8+
let index = list.indexOf(activeElement)
9+
10+
if (index === -1) {
11+
return !shouldGetNext && isCycleAllowed ? list[listLength - 1] : list[0]
12+
}
13+
14+
index += shouldGetNext ? 1 : -1
15+
16+
if (isCycleAllowed) {
17+
index = (index + listLength) % listLength
18+
}
19+
20+
return list[Math.max(0, Math.min(index, listLength - 1))]
21+
}
22+
23+
export default getNextActiveElement
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import getNextActiveElement from './getNextActiveElement'
12
import getRTLPlacement from './getRTLPlacement'
23
import getUID from './getUID'
34
import isInViewport from './isInViewport'
45
import isRTL from './isRTL'
56

6-
export { getRTLPlacement, getUID, isInViewport, isRTL }
7+
export { getNextActiveElement, getRTLPlacement, getUID, isInViewport, isRTL }

0 commit comments

Comments
 (0)