Skip to content
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

The Column component ignores lazyItems when setting selectedIndex #102

Open
edatencio-velope opened this issue Aug 9, 2022 · 0 comments

Comments

@edatencio-velope
Copy link

Suppose a Column component with 10 children, from which only 4 are created, and the rest are stored in the _lazyItems array, if you navigate to the 8th element directly (with no animations) by setting column.selectedIndex = 8, the column will truncate the index and focus on the last one that was created, the 4th one.

We made a workaround while the issue is addressed here.

import { Lightning } from '@lightningjs/sdk'
import { Column as LngColumn } from 'lightning-ui-components'
import { getW } from 'lightning-ui-components/utils'
import { clamp } from '../../Utils'

export default class Column extends LngColumn {
    // Add lazy items as needed to reach the desired index
    set lazyIndex(index: number) {
        if (index < 0) return

        if (this._lazyItems && this._lazyItems.length > 0 && index >= this.items.length) {
            const itemsLength = this.items.length
            const lazyItemsLength = this._lazyItems.length
            const totalLength = itemsLength + lazyItemsLength
            const indexClamped = clamp(index, 0, totalLength - 1)
            const lazyItemCount = indexClamped - itemsLength + 1 + this.lazyUpCount
            const lazyItemCountClamped = clamp(lazyItemCount, 0, lazyItemsLength)
            const lazyItems = this._lazyItems.splice(0, lazyItemCountClamped)
            index = indexClamped
            this._appendItems(lazyItems)
        }

        this.selectedIndex = index
    }

    // Append items without adding them to the _lazyItems array
    _appendItems(items: Lightning.Component[] = []): void {
        const itemWidth = this.renderWidth
        this._smooth = false

        items.forEach(item => {
            item.parentFocus = this.hasFocus()
            item = this.Items.childList.a(item)
            item.w = getW(item) || itemWidth
        })

        this.stage.update()
        this._update()
        this._refocus()
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant