Skip to content
Closed
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
46 changes: 46 additions & 0 deletions packages/ui/src/components/list.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, it, expect } from "vitest"
import { List } from "./list"
import { render, screen } from "@testing-library/solid"
import { createSignal } from "solid-js"

describe("List Component", () => {
it("handles special characters in keys without querySelector errors", () => {
const items = [
{ id: "file name with spaces.md", name: "File with Spaces" },
{ id: "file'with'quotes.md", name: "File with Quotes" },
{ id: "file#with#hashes.md", name: "File with Hashes" },
{ id: "file@with@ats.md", name: "File with @ Symbols" },
{ id: "file$with$dollars.md", name: "File with $ Symbols" },
{ id: "file^with^carets.md", name: "File with ^ Symbols" },
{ id: "file&with&amps.md", name: "File with & Symbols" },
{ id: "中国文件名.md", name: "Chinese File Name" },
]

const [current, setCurrent] = createSignal(items[0])
const [active, setActive] = createSignal(items[0].id)

render(() => (
<List
items={items}
key={(item) => item.id}
current={current()}
active={active()}
onSelect={(item) => setCurrent(item!)}
>
{(item) => <div>{item.name}</div>}
</List>
))

// Verify all items are rendered
items.forEach((item) => {
expect(screen.getByText(item.name)).toBeInTheDocument()
})

// Simulate selecting different items to trigger scrollIntoView
items.forEach((item) => {
setCurrent(item)
setActive(item.id)
// The component should handle these without throwing errors
})
})
})
4 changes: 2 additions & 2 deletions packages/ui/src/components/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
if (!props.current) return
const key = props.key(props.current)
requestAnimationFrame(() => {
const element = scrollRef()?.querySelector(`[data-key="${key}"]`)
const element = scrollRef()?.querySelector(`[data-key="${CSS.escape(key)}"]`)
element?.scrollIntoView({ block: "center" })
})
})
Expand All @@ -79,7 +79,7 @@ export function List<T>(props: ListProps<T> & { ref?: (ref: ListRef) => void })
scrollRef()?.scrollTo(0, 0)
return
}
const element = scrollRef()?.querySelector(`[data-key="${active()}"]`)
const element = scrollRef()?.querySelector(`[data-key="${CSS.escape(active() || "")}"]`)
element?.scrollIntoView({ block: "center", behavior: "smooth" })
})

Expand Down