-
Notifications
You must be signed in to change notification settings - Fork 561
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fix/binding-value-undesirely-expands-combobox' of https…
…://github.com/dlacaille/reach-ui into dlacaille-fix/binding-value-undesirely-expands-combobox
- Loading branch information
Showing
2 changed files
with
108 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import * as React from "react"; | ||
import { | ||
Combobox, | ||
ComboboxInput, | ||
ComboboxList, | ||
ComboboxOption, | ||
ComboboxPopover, | ||
} from "@reach/combobox"; | ||
import { useCityMatch } from "./utils"; | ||
import "@reach/combobox/styles.css"; | ||
|
||
let name = "Simulated Change"; | ||
|
||
function Example() { | ||
let [term, setTerm] = React.useState("Detroit"); | ||
let [selection, setSelection] = React.useState(""); | ||
let results = useCityMatch(term); | ||
let ref = React.useRef(); | ||
|
||
const handleChange = (event) => { | ||
setTerm(event.target.value); | ||
}; | ||
|
||
const handleSelect = (value) => { | ||
setSelection(value); | ||
setTerm(""); | ||
}; | ||
|
||
const handleSimulateChange = () => { | ||
setTerm("New York"); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h2>Clientside Search</h2> | ||
<p> | ||
This example tests that changes to the controlled value of Combobox | ||
don't expand it unless we are actually typing. The initial value and | ||
programmatically set value here shouldn't open the Popover. | ||
</p> | ||
<p>Selection: {selection}</p> | ||
<p>Term: {term}</p> | ||
<p> | ||
<button onClick={handleSimulateChange}> | ||
Set value programmatically | ||
</button> | ||
</p> | ||
<Combobox onSelect={handleSelect} aria-label="choose a city"> | ||
<ComboboxInput | ||
ref={ref} | ||
value={term} | ||
onChange={handleChange} | ||
autocomplete={false} | ||
style={{ width: 400 }} | ||
/> | ||
{results && ( | ||
<ComboboxPopover> | ||
{results.length === 0 && ( | ||
<p> | ||
No Results{" "} | ||
<button | ||
onClick={() => { | ||
setTerm(""); | ||
ref.current.focus(); | ||
}} | ||
> | ||
clear | ||
</button> | ||
</p> | ||
)} | ||
<ComboboxList> | ||
{results.slice(0, 10).map((result, index) => ( | ||
<ComboboxOption | ||
key={index} | ||
value={`${result.city}, ${result.state}`} | ||
/> | ||
))} | ||
</ComboboxList> | ||
<p> | ||
<a href="/new">Add a record</a> | ||
</p> | ||
</ComboboxPopover> | ||
)} | ||
</Combobox> | ||
</div> | ||
); | ||
} | ||
|
||
Example.story = { name }; | ||
export const Comp = Example; | ||
export default { title: "Combobox" }; |
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