forked from ariakit/ariakit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
295 additions
and
17 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,5 @@ | ||
--- | ||
"ariakit-utils": minor | ||
--- | ||
|
||
Add `usePreviousValue` function to `ariakit-utils/hooks`. ([#1219](https://github.com/ariakit/ariakit/pull/1219)) |
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,5 @@ | ||
--- | ||
"ariakit": patch | ||
--- | ||
|
||
Fix `Combobox` with `autoSelect` and `autoComplete="both"` props setting an incorrect value when there are no matches. ([#1219](https://github.com/ariakit/ariakit/pull/1219)) |
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
73 changes: 73 additions & 0 deletions
73
packages/ariakit/src/combobox/__examples__/combobox-group/food.ts
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,73 @@ | ||
export default [ | ||
{ name: "Apple", type: "Fruits" }, | ||
{ name: "Avocado", type: "Fruits" }, | ||
{ name: "Banana", type: "Fruits" }, | ||
{ name: "Cherry", type: "Fruits" }, | ||
{ name: "Coconut", type: "Fruits" }, | ||
{ name: "Grape", type: "Fruits" }, | ||
{ name: "Kiwi", type: "Fruits" }, | ||
{ name: "Lemon", type: "Fruits" }, | ||
{ name: "Lime", type: "Fruits" }, | ||
{ name: "Mango", type: "Fruits" }, | ||
{ name: "Orange", type: "Fruits" }, | ||
{ name: "Papaya", type: "Fruits" }, | ||
{ name: "Pear", type: "Fruits" }, | ||
{ name: "Pineapple", type: "Fruits" }, | ||
{ name: "Pomegranate", type: "Fruits" }, | ||
{ name: "Strawberry", type: "Fruits" }, | ||
{ name: "Watermelon", type: "Fruits" }, | ||
|
||
{ name: "Bacon", type: "Meat" }, | ||
{ name: "Beef", type: "Meat" }, | ||
{ name: "Chicken", type: "Meat" }, | ||
{ name: "Fish", type: "Meat" }, | ||
{ name: "Ham", type: "Meat" }, | ||
{ name: "Lamb", type: "Meat" }, | ||
{ name: "Pork", type: "Meat" }, | ||
{ name: "Sausage", type: "Meat" }, | ||
{ name: "Turkey", type: "Meat" }, | ||
|
||
{ name: "Bread", type: "Bakery" }, | ||
{ name: "Cake", type: "Bakery" }, | ||
{ name: "Cookie", type: "Bakery" }, | ||
{ name: "Croissant", type: "Bakery" }, | ||
{ name: "Donut", type: "Bakery" }, | ||
{ name: "Eclair", type: "Bakery" }, | ||
{ name: "Fondant", type: "Bakery" }, | ||
{ name: "Gelato", type: "Bakery" }, | ||
{ name: "Muffin", type: "Bakery" }, | ||
{ name: "Pie", type: "Bakery" }, | ||
{ name: "Pudding", type: "Bakery" }, | ||
{ name: "Scone", type: "Bakery" }, | ||
{ name: "Souffle", type: "Bakery" }, | ||
{ name: "Tart", type: "Bakery" }, | ||
{ name: "Waffle", type: "Bakery" }, | ||
|
||
{ name: "Butter", type: "Dairy" }, | ||
{ name: "Cheese", type: "Dairy" }, | ||
{ name: "Cream", type: "Dairy" }, | ||
{ name: "Egg", type: "Dairy" }, | ||
{ name: "Milk", type: "Dairy" }, | ||
{ name: "Sour cream", type: "Dairy" }, | ||
{ name: "Yogurt", type: "Dairy" }, | ||
|
||
{ name: "Beer", type: "Beverages" }, | ||
{ name: "Brandy", type: "Beverages" }, | ||
{ name: "Cider", type: "Beverages" }, | ||
{ name: "Coffee", type: "Beverages" }, | ||
{ name: "Cognac", type: "Beverages" }, | ||
{ name: "Gin", type: "Beverages" }, | ||
{ name: "Juice", type: "Beverages" }, | ||
{ name: "Liqueur", type: "Beverages" }, | ||
{ name: "Milkshake", type: "Beverages" }, | ||
{ name: "Rum", type: "Beverages" }, | ||
{ name: "Sake", type: "Beverages" }, | ||
{ name: "Sherry", type: "Beverages" }, | ||
{ name: "Soda", type: "Beverages" }, | ||
{ name: "Tea", type: "Beverages" }, | ||
{ name: "Tequila", type: "Beverages" }, | ||
{ name: "Vodka", type: "Beverages" }, | ||
{ name: "Water", type: "Beverages" }, | ||
{ name: "Whiskey", type: "Beverages" }, | ||
{ name: "Wine", type: "Beverages" }, | ||
]; |
68 changes: 68 additions & 0 deletions
68
packages/ariakit/src/combobox/__examples__/combobox-group/index.tsx
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,68 @@ | ||
import { Fragment, useMemo } from "react"; | ||
import { | ||
Combobox, | ||
ComboboxGroup, | ||
ComboboxGroupLabel, | ||
ComboboxItem, | ||
ComboboxPopover, | ||
ComboboxSeparator, | ||
useComboboxState, | ||
} from "ariakit/combobox"; | ||
import groupBy from "lodash/groupBy"; | ||
import food from "./food"; | ||
import "./style.css"; | ||
|
||
const list = food.map((item) => item.name); | ||
|
||
export default function Example() { | ||
const combobox = useComboboxState({ gutter: 8, sameWidth: true, list }); | ||
|
||
// Transform combobox.matches into groups of objects. | ||
const matches = useMemo(() => { | ||
const items = combobox.matches | ||
.map((value) => food.find((item) => item.name === value)!) | ||
.filter(Boolean); | ||
return Object.entries(groupBy(items, "type")); | ||
}, [combobox.matches]); | ||
|
||
return ( | ||
<div> | ||
<label className="label"> | ||
Your favorite food | ||
<Combobox | ||
state={combobox} | ||
placeholder="e.g., Apple" | ||
className="combobox" | ||
autoComplete="both" | ||
autoSelect | ||
/> | ||
</label> | ||
<ComboboxPopover state={combobox} className="popover"> | ||
{!!matches.length ? ( | ||
matches.map(([type, items], index, array) => ( | ||
<Fragment key={type}> | ||
<ComboboxGroup className="group"> | ||
<ComboboxGroupLabel className="group-label"> | ||
{type} | ||
</ComboboxGroupLabel> | ||
{items.map((item) => ( | ||
<ComboboxItem | ||
focusOnHover | ||
key={item.name} | ||
value={item.name} | ||
className="combobox-item" | ||
/> | ||
))} | ||
</ComboboxGroup> | ||
{index < array.length - 1 && ( | ||
<ComboboxSeparator className="separator" /> | ||
)} | ||
</Fragment> | ||
)) | ||
) : ( | ||
<div className="no-results">No results found</div> | ||
)} | ||
</ComboboxPopover> | ||
</div> | ||
); | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/ariakit/src/combobox/__examples__/combobox-group/style.css
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,20 @@ | ||
@import url("../combobox/style.css"); | ||
|
||
.group-label { | ||
@apply | ||
text-sm | ||
p-2 | ||
font-medium | ||
opacity-60 | ||
cursor-default; | ||
} | ||
|
||
.separator { | ||
border-width: 1px 0 0; | ||
border-color: currentcolor; | ||
@apply my-2 h-0 opacity-25; | ||
} | ||
|
||
.no-results { | ||
@apply gap-2 p-2; | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/ariakit/src/combobox/__examples__/combobox-group/test.tsx
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,80 @@ | ||
import { | ||
click, | ||
getByRole, | ||
hover, | ||
press, | ||
render, | ||
type, | ||
} from "ariakit-test-utils"; | ||
import { axe } from "jest-axe"; | ||
import Example from "."; | ||
|
||
const getCombobox = () => getByRole("combobox"); | ||
const getOption = (name: string) => getByRole("option", { name }); | ||
|
||
function getSelectionValue(element: Element) { | ||
const input = element as HTMLInputElement; | ||
const { selectionStart, selectionEnd } = input; | ||
const selectionValue = input.value.slice(selectionStart!, selectionEnd!); | ||
return selectionValue; | ||
} | ||
|
||
test("a11y", async () => { | ||
const { container } = render(<Example />); | ||
expect(await axe(container)).toHaveNoViolations(); | ||
}); | ||
|
||
test("auto select with inline autocomplete on typing", async () => { | ||
render(<Example />); | ||
await press.Tab(); | ||
await type("a"); | ||
expect(getCombobox()).toHaveValue("apple"); | ||
expect(getSelectionValue(getCombobox())).toBe("pple"); | ||
await press.ArrowDown(); | ||
expect(getCombobox()).toHaveValue("Avocado"); | ||
expect(getSelectionValue(getCombobox())).toBe(""); | ||
await press.ArrowUp(); | ||
expect(getCombobox()).toHaveValue("apple"); | ||
expect(getSelectionValue(getCombobox())).toBe("pple"); | ||
await type("e"); | ||
expect(getCombobox()).toHaveValue("ae"); | ||
expect(getSelectionValue(getCombobox())).toBe(""); | ||
await type("\b\bv"); | ||
expect(getCombobox()).toHaveValue("vodka"); | ||
expect(getSelectionValue(getCombobox())).toBe("odka"); | ||
}); | ||
|
||
test("auto select with inline autocomplete on arrow down", async () => { | ||
render(<Example />); | ||
await press.Tab(); | ||
await press.ArrowDown(); | ||
await press.ArrowDown(); | ||
expect(getCombobox()).toHaveValue("Apple"); | ||
expect(getSelectionValue(getCombobox())).toBe("Apple"); | ||
await press.ArrowDown(); | ||
expect(getCombobox()).toHaveValue("Avocado"); | ||
expect(getSelectionValue(getCombobox())).toBe(""); | ||
await press.ArrowUp(); | ||
expect(getCombobox()).toHaveValue("Apple"); | ||
}); | ||
|
||
test("blur input after autocomplete", async () => { | ||
render(<Example />); | ||
await press.Tab(); | ||
await type("a"); | ||
expect(getCombobox()).toHaveValue("apple"); | ||
await press.ArrowDown(); | ||
expect(getCombobox()).toHaveValue("Avocado"); | ||
await click(document.body); | ||
await click(document.body); | ||
expect(getCombobox()).toHaveValue("Avocado"); | ||
}); | ||
|
||
test("autocomplete on focus on hover", async () => { | ||
render(<Example />); | ||
await click(getCombobox()); | ||
await type("g"); | ||
expect(getCombobox()).toHaveValue("grape"); | ||
await hover(getOption("Gelato")); | ||
expect(getCombobox()).toHaveValue("g"); | ||
}); |
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
4 changes: 4 additions & 0 deletions
4
packages/ariakit/src/combobox/__examples__/combobox-matches/style.css
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 |
---|---|---|
@@ -1 +1,5 @@ | ||
@import url("../combobox/style.css"); | ||
|
||
.no-results { | ||
@apply gap-2 p-2; | ||
} |
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