Skip to content

Commit

Permalink
[Combobox] Update openOnFocus example to work even with empty input
Browse files Browse the repository at this point in the history
The existing openOnFocus example mounts/unmounts the popover based on
the results array state, which returns null if the input term is an
empty string. This is confusing in the context of the openOnFocus prop
because the popover doesn't show on focus if the input is empty.
  • Loading branch information
Austin Wood committed Jan 4, 2021
1 parent 881b6ee commit be4ac7d
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions packages/combobox/examples/open-on-focus.example.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ import {
ComboboxOption,
ComboboxPopover,
} from "@reach/combobox";
import { useCityMatch } from "./utils";
import "@reach/combobox/styles.css";

let name = "Open on focus";

function Example() {
let [term, setTerm] = React.useState("D");
let [term, setTerm] = React.useState("");
let [selection, setSelection] = React.useState("");
let results = useCityMatch(term);
let inputRef = React.useRef(null);

function handleChange(event) {
Expand All @@ -37,21 +35,15 @@ function Example() {
style={inputStyle}
ref={inputRef}
/>
{results && (
<ComboboxPopover style={popupStyle}>
<p>
<button>Hi</button>
</p>
<ComboboxList>
{results.slice(0, 10).map((result, index) => (
<ComboboxOption
key={index}
value={`${result.city}, ${result.state}`}
/>
))}
</ComboboxList>
</ComboboxPopover>
)}
<ComboboxPopover>
<ComboboxList>
<ComboboxOption value="Apple" />
<ComboboxOption value="Banana" />
<ComboboxOption value="Orange" />
<ComboboxOption value="Pineapple" />
<ComboboxOption value="Kiwi" />
</ComboboxList>
</ComboboxPopover>
</Combobox>
</div>
);
Expand Down

0 comments on commit be4ac7d

Please sign in to comment.