fix(app): querySelector errors, more defensive scroll-to-item
This commit is contained in:
@@ -6,6 +6,13 @@ import { Icon, type IconProps } from "./icon"
|
|||||||
import { IconButton } from "./icon-button"
|
import { IconButton } from "./icon-button"
|
||||||
import { TextField } from "./text-field"
|
import { TextField } from "./text-field"
|
||||||
|
|
||||||
|
function findByKey(container: HTMLElement, key: string) {
|
||||||
|
const nodes = container.querySelectorAll<HTMLElement>('[data-slot="list-item"][data-key]')
|
||||||
|
for (const node of nodes) {
|
||||||
|
if (node.getAttribute("data-key") === key) return node
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export interface ListSearchProps {
|
export interface ListSearchProps {
|
||||||
placeholder?: string
|
placeholder?: string
|
||||||
autofocus?: boolean
|
autofocus?: boolean
|
||||||
@@ -97,8 +104,8 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
|
|||||||
if (!props.current) return
|
if (!props.current) return
|
||||||
const key = props.key(props.current)
|
const key = props.key(props.current)
|
||||||
requestAnimationFrame(() => {
|
requestAnimationFrame(() => {
|
||||||
const element = scroll.querySelector(`[data-key="${CSS.escape(key)}"]`)
|
const element = findByKey(scroll, key)
|
||||||
if (!(element instanceof HTMLElement)) return
|
if (!element) return
|
||||||
scrollIntoView(scroll, element, "center")
|
scrollIntoView(scroll, element, "center")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -114,8 +121,8 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
|
|||||||
}
|
}
|
||||||
const key = active()
|
const key = active()
|
||||||
if (!key) return
|
if (!key) return
|
||||||
const element = scroll.querySelector(`[data-key="${CSS.escape(key)}"]`)
|
const element = findByKey(scroll, key)
|
||||||
if (!(element instanceof HTMLElement)) return
|
if (!element) return
|
||||||
scrollIntoView(scroll, element, "center")
|
scrollIntoView(scroll, element, "center")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user