JSFiddle - React, Tailwind, and code Playground
author(s):
Tao Xin
HTML
<script src="https://vanjs.org/code/van-latest.nomodule.min.js"></script>
CSS
.root { display: flex; }
textarea {
width: 300px;
height: 135px;
}
.suggestion {
width: 200px;
padding: 1px 2px 1px 5px;
}
.text-row { display: table-row; }
.text-row.selected {
background-color: blue;
color: white;
}
TypeScript
const {a, div, p, pre, textarea} = van.tags
interface SuggestionListProps {
readonly candidates: readonly string[]
readonly selectedIndex: number
}
const SuggestionList = ({candidates, selectedIndex}: SuggestionListProps) =>
div({class: "suggestion"}, candidates.map((s, i) => pre({
"data-index": i,
class: i === selectedIndex ? "text-row selected" : "text-row",
}, s)))
const lastWord = (text: string) => text.match(/\w+$/)?.[0] ?? ""
const AutoComplete = ({words}: {readonly words: readonly string[]}) => {
const getCandidates = (prefix: string) => {
const maxTotal = 10, result: string[] = []
for (let word of words) {
if (word.startsWith(prefix.toLowerCase())) result.push(word)
if (result.length >= maxTotal) break
}
return result
}
const prefix = van.state("")
const candidates = van.derive(() => getCandidates(prefix.val))
// Resetting selectedIndex to 0 whenever candidates change
const selectedIndex = van.derive(() => (candidates.val, 0))
const onkeydown = (e: KeyboardEvent) => {
if (e.key === "ArrowDown") {
selectedIndex.val = selectedIndex.val + 1 < candidates.val.length ? selectedIndex.val + 1 : 0
e.preventDefault()
} else if (e.key === "ArrowUp") {
selectedIndex.val = selectedIndex.val > 0 ? selectedIndex.val - 1 : candidates.val.length - 1
e.preventDefault()
} else if (e.key === "Enter") {
const candidate = candidates.val[selectedIndex.val] ?? prefix.val
const target = <HTMLTextAreaElement>e.target
target.value += candidate.substring(prefix.val.length)
target.setSelectionRange(target.value.length, target.value.length)
prefix.val = lastWord(target.value)
e.preventDefault()
}
}
const oninput = (e: Event) => prefix.val = lastWord((<HTMLTextAreaElement>e.target).value)
return div({class: "root"}, textarea({onkeydown, oninput}), node => {
if (node && candidates.val === candidates.oldVal) {
const dom =...