-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from szhsin/feat/search
feat: search
- Loading branch information
Showing
18 changed files
with
137 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import React, { useState } from 'react'; | ||
import { useCombobox, supercomplete, linearTraversal } from '@szhsin/react-autocomplete'; | ||
import styles from '@/styles/Home.module.css'; | ||
import { US_STATES } from '../data'; | ||
|
||
type Item = { name: string; abbr: string }; | ||
const getItemValue = (item: Item) => item.name; | ||
|
||
const search = (value: string | undefined) => value && console.log(`Searching for "${value}"`); | ||
|
||
export default function Home() { | ||
const [value, setValue] = useState<string | undefined>(); | ||
const items = value | ||
? US_STATES.filter((item) => item.name.toLowerCase().startsWith(value.toLowerCase())) | ||
: US_STATES; | ||
|
||
const { | ||
getInputProps, | ||
getListProps, | ||
getItemProps, | ||
getToggleProps, | ||
getClearProps, | ||
open, | ||
focusItem, | ||
isInputEmpty | ||
} = useCombobox({ | ||
onSelectChange: (selected) => search(selected?.name), | ||
getItemValue, | ||
value, | ||
onChange: setValue, | ||
feature: supercomplete({ | ||
selectOnBlur: false, | ||
getFocusItem: (newValue) => | ||
US_STATES.filter((item) => | ||
item.name.toLowerCase().startsWith(newValue.toLowerCase()) | ||
)[0] | ||
}), | ||
|
||
traversal: linearTraversal({ items, traverseInput: true }) | ||
}); | ||
|
||
return ( | ||
<div className={styles.wrapper}> | ||
<div>value: {value}</div> | ||
<div>Focus item: {focusItem?.name}</div> | ||
|
||
<form | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
search(value); | ||
}} | ||
> | ||
<input className={styles.input} {...getInputProps()} /> | ||
|
||
{!isInputEmpty && ( | ||
<button | ||
type="button" | ||
className={styles.clearButton} | ||
style={{ position: 'absolute', transform: 'translate(-120%, 10%)' }} | ||
{...getClearProps()} | ||
> | ||
❎ | ||
</button> | ||
)} | ||
<button type="button" {...getToggleProps()}> | ||
{open ? '⬆️' : '⬇️'} | ||
</button> | ||
</form> | ||
<ul | ||
{...getListProps()} | ||
className={styles.list} | ||
style={{ | ||
position: 'absolute', | ||
border: '1px solid', | ||
display: open && items.length ? 'block' : 'none' | ||
}} | ||
> | ||
{items.map((item) => ( | ||
<li | ||
className={styles.option} | ||
key={item.abbr} | ||
style={{ | ||
background: focusItem === item ? '#0a0' : 'none' | ||
}} | ||
{...getItemProps({ item })} | ||
> | ||
{item.name} | ||
</li> | ||
))} | ||
</ul> | ||
</div> | ||
); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.