8000 fix(BPagination): right/left/up/down arrow keys now operating better … by rgeerts · Pull Request #2665 · bootstrap-vue-next/bootstrap-vue-next · GitHub
[go: up one dir, main page]

Skip to content

fix(BPagination): right/left/up/down arrow keys now operating better … #2665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
:aria-label="props.ariaLabel || undefined"
@keydown="handleKeyNav"
>
<li v-for="page in pages" :key="`page-${page.id}`" v-bind="page.li" ref="_pageElements">
<li
v-for="(page, index) in pages"
:key="`page-${page.id}`"
v-bind="page.li"
ref="_pageElements"
:displayIndex="index"
>
<span
v-if="page.id === FIRST_ELLIPSIS || page.id === LAST_ELLIPSIS"
v-bind="ellipsisProps.span"
Expand Down Expand Up @@ -304,7 +310,6 @@ const pagination = computed(() => ({

const pageClick = (event: Readonly<MouseEvent>, pageNumber: number) => {
if (pageNumber === computedModelValue.value) return

const clickEvent = new BvEvent('page-click', {
cancelable: true,
target: event.target,
Expand All @@ -315,6 +320,13 @@ const pageClick = (event: Readonly<MouseEvent>, pageNumber: number) => {

modelValue.value = pageNumber

nextTick(() => {
if (pageNumber === 1) {
focusFirst()
} else if (pageNumber === pagination.value.numberOfPages) {
focusLast()
}
})
// nextTick(() => {
// if (isVisible(target) && un_element.contains(target)) {
// attemptFocus(target)
Expand All @@ -332,18 +344,24 @@ const isDisabled = (el: HTMLButtonElement) => {
return !isElement || el.disabled || hasAttr || hasClass
}

const getButtons = () =>
pageElements.value
?.map((page) => page.children[0] as HTMLButtonElement)
.filter((btn) => {
if (btn.getAttribute('display') === 'none') {
const getButtons = (): HTMLButtonElement[] =>
[...(pageElements.value ?? [])]
?.sort(
(a, b) =>
parseInt(a.getAttribute('displayIndex') || '0') -
parseInt(b.getAttribute('displayIndex') || '0')
)
?.map((page) => page.children[0])
?.filter((el) => {
if (el.getAttribute('display') === 'none' || el.tagName.toUpperCase() !== 'BUTTON') {
return false
}

const bcr = btn.getBoundingClientRect()
const bcr = el.getBoundingClientRect()

return !!(bcr && bcr.height > 0 && bcr.width > 0)
}) ?? []
return true || !!(bcr && bcr.height > 0 && bcr.width > 0)
})
?.map((el) => el as HTMLButtonElement)

const focusFirst = () => {
nextTick(() => {
Expand Down Expand Up @@ -376,7 +394,6 @@ const focusNext = () => {
nextTick(() => {
const buttons = getButtons()
const index = buttons.indexOf(getActiveElement() as HTMLButtonElement)

if (index < buttons.length - 1 && !isDisabled(buttons[index + 1])) {
buttons[index + 1]?.focus()
}
Expand All @@ -385,7 +402,6 @@ const focusNext = () => {

const handleKeyNav = (event: KeyboardEvent) => {
const {code, shiftKey} = event

if (code === CODE_LEFT || code === CODE_UP) {
stopEvent(event)
if (shiftKey) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,25 @@ describe('pagination', () => {

expect(await TestScenariosAgainstInvariants(wrapper)).toBe(0)
})
it('can navigate to different pages using the left and right arrow keys', async () => {
const wrapper = mount(BPagination, {
props: {totalRows: 7, perPage: 1, modelValue: 1},
attachTo: document.body,
})
await wrapper.find('li.active > button').element?.focus()
expect(document.activeElement?.textContent).toBe('1')
await wrapper.find('ul').trigger('keydown', {code: 'ArrowRight'})
expect(document.activeElement?.textContent).toBe('2')
await wrapper.find('ul').trigger('keydown', {code: 'ArrowRight'})
expect(document.activeElement?.textContent).toBe('3')
await wrapper.find('ul').trigger('keydown', {code: 'ArrowRight'})
expect(document.activeElement?.textContent).toBe('4')
await wrapper.find('button[aria-posinset="4"]').trigger('click')
await wrapper.find('ul').trigger('keydown', {code: 'ArrowRight'})
expect(document.activeElement?.textContent).toBe('5')
await wrapper.find('ul').trigger('keydown', {code: 'ArrowLeft'})
expect(document.activeElement?.textContent).toBe('4')
})
})

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
0