-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
setValueOnMove
state on Select
not syncing between connected …
…stores (#2858) This PR fixes the [`setValueOnMove`](https://ariakit.org/reference/use-select-store#setvalueonmove) state on the [Select](https://ariakit.org/components/select) module not syncing between multiple stores. The following now works as expected: ```js const store1 = useSelectStore(); const store2 = useSelectStore({ store: store1, setValueOnMove: true }); store1.useState("setValueOnMove") === store2.useState("setValueOnMove"); // true ```
- Loading branch information
Showing
20 changed files
with
208 additions
and
758 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,15 @@ | ||
--- | ||
"@ariakit/react-core": patch | ||
"@ariakit/react": patch | ||
--- | ||
|
||
Fixed the [`setValueOnMove`](https://ariakit.org/reference/use-select-store#setvalueonmove) state on the [Select](https://ariakit.org/components/select) module not syncing between multiple stores. | ||
|
||
The following now works as expected: | ||
|
||
```js | ||
const store1 = useSelectStore(); | ||
const store2 = useSelectStore({ store: store1, setValueOnMove: true }); | ||
|
||
store1.useState("setValueOnMove") === store2.useState("setValueOnMove"); // true | ||
``` |
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,19 +1,20 @@ | ||
import * as Ariakit from "@ariakit/react"; | ||
import "./style.css"; | ||
import * as Ariakit from "@ariakit/react"; | ||
|
||
export default function Example() { | ||
const select = Ariakit.useSelectStore({ defaultValue: "Student" }); | ||
return ( | ||
<form className="wrapper"> | ||
<label htmlFor="email">Email</label> | ||
<input type="email" id="email" name="email" className="input" /> | ||
<Ariakit.SelectLabel store={select}>Role</Ariakit.SelectLabel> | ||
<Ariakit.Select store={select} name="role" className="button" /> | ||
<Ariakit.SelectPopover store={select} sameWidth className="popover"> | ||
<Ariakit.SelectItem className="select-item" value="Student" /> | ||
<Ariakit.SelectItem className="select-item" value="Tutor" /> | ||
<Ariakit.SelectItem className="select-item" value="Parent" /> | ||
</Ariakit.SelectPopover> | ||
<Ariakit.SelectProvider defaultValue="Student"> | ||
<Ariakit.SelectLabel>Role</Ariakit.SelectLabel> | ||
<Ariakit.Select name="role" className="button" /> | ||
<Ariakit.SelectPopover sameWidth className="popover"> | ||
<Ariakit.SelectItem className="select-item" value="Student" /> | ||
<Ariakit.SelectItem className="select-item" value="Tutor" /> | ||
<Ariakit.SelectItem className="select-item" value="Parent" /> | ||
</Ariakit.SelectPopover> | ||
</Ariakit.SelectProvider> | ||
</form> | ||
); | ||
} |
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,51 @@ | ||
import "./style.css"; | ||
import { useDeferredValue, useMemo } from "react"; | ||
import * as Ariakit from "@ariakit/react"; | ||
import { matchSorter } from "match-sorter"; | ||
import list from "../select-combobox/list.js"; | ||
|
||
export default function Example() { | ||
const combobox = Ariakit.useComboboxStore({ resetValueOnHide: true }); | ||
const select = Ariakit.useSelectStore({ combobox, defaultValue: "Apple" }); | ||
|
||
const value = combobox.useState("value"); | ||
const deferredValue = useDeferredValue(value); | ||
|
||
const matches = useMemo(() => { | ||
return matchSorter(list, deferredValue, { | ||
baseSort: (a, b) => (a.index < b.index ? -1 : 1), | ||
}); | ||
}, [deferredValue]); | ||
|
||
return ( | ||
<div className="wrapper"> | ||
<Ariakit.SelectLabel store={select}>Favorite fruit</Ariakit.SelectLabel> | ||
<Ariakit.Select store={select} className="button" /> | ||
<Ariakit.SelectPopover | ||
store={select} | ||
gutter={4} | ||
sameWidth | ||
className="popover" | ||
> | ||
<div className="combobox-wrapper"> | ||
<Ariakit.Combobox | ||
store={combobox} | ||
autoSelect | ||
placeholder="Search..." | ||
className="combobox" | ||
/> | ||
</div> | ||
<Ariakit.ComboboxList store={combobox}> | ||
{matches.map((value) => ( | ||
<Ariakit.ComboboxItem | ||
key={value} | ||
focusOnHover | ||
className="select-item" | ||
render={<Ariakit.SelectItem value={value} />} | ||
/> | ||
))} | ||
</Ariakit.ComboboxList> | ||
</Ariakit.SelectPopover> | ||
</div> | ||
); | ||
} |
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 @@ | ||
@import url("../select-combobox/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
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 @@ | ||
import "../select-combobox/test.js"; |
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,51 +1,52 @@ | ||
import "./style.css"; | ||
import { useDeferredValue, useMemo } from "react"; | ||
import { startTransition, useMemo, useState } from "react"; | ||
import * as Ariakit from "@ariakit/react"; | ||
import { matchSorter } from "match-sorter"; | ||
import list from "./list.js"; | ||
|
||
export default function Example() { | ||
const combobox = Ariakit.useComboboxStore({ resetValueOnHide: true }); | ||
const select = Ariakit.useSelectStore({ combobox, defaultValue: "Apple" }); | ||
|
||
const value = combobox.useState("value"); | ||
const deferredValue = useDeferredValue(value); | ||
const [searchValue, setSearchValue] = useState(""); | ||
|
||
const matches = useMemo(() => { | ||
return matchSorter(list, deferredValue, { | ||
return matchSorter(list, searchValue, { | ||
baseSort: (a, b) => (a.index < b.index ? -1 : 1), | ||
}); | ||
}, [deferredValue]); | ||
}, [searchValue]); | ||
|
||
return ( | ||
<div className="wrapper"> | ||
<Ariakit.SelectLabel store={select}>Favorite fruit</Ariakit.SelectLabel> | ||
<Ariakit.Select store={select} className="button" /> | ||
<Ariakit.SelectPopover | ||
store={select} | ||
gutter={4} | ||
sameWidth | ||
className="popover" | ||
<Ariakit.ComboboxProvider | ||
resetValueOnHide | ||
setValue={(value) => { | ||
startTransition(() => { | ||
setSearchValue(value); | ||
}); | ||
}} | ||
> | ||
<div className="combobox-wrapper"> | ||
<Ariakit.Combobox | ||
store={combobox} | ||
autoSelect | ||
placeholder="Search..." | ||
className="combobox" | ||
/> | ||
</div> | ||
<Ariakit.ComboboxList store={combobox}> | ||
{matches.map((value) => ( | ||
<Ariakit.ComboboxItem | ||
key={value} | ||
focusOnHover | ||
className="select-item" | ||
render={<Ariakit.SelectItem value={value} />} | ||
/> | ||
))} | ||
</Ariakit.ComboboxList> | ||
</Ariakit.SelectPopover> | ||
<Ariakit.SelectProvider defaultValue="Apple"> | ||
<Ariakit.SelectLabel>Favorite fruit</Ariakit.SelectLabel> | ||
<Ariakit.Select className="button" /> | ||
<Ariakit.SelectPopover gutter={4} sameWidth className="popover"> | ||
<div className="combobox-wrapper"> | ||
<Ariakit.Combobox | ||
autoSelect | ||
placeholder="Search..." | ||
className="combobox" | ||
/> | ||
</div> | ||
<Ariakit.ComboboxList> | ||
{matches.map((value) => ( | ||
<Ariakit.SelectItem | ||
key={value} | ||
value={value} | ||
className="select-item" | ||
render={<Ariakit.ComboboxItem />} | ||
/> | ||
))} | ||
</Ariakit.ComboboxList> | ||
</Ariakit.SelectPopover> | ||
</Ariakit.SelectProvider> | ||
</Ariakit.ComboboxProvider> | ||
</div> | ||
); | ||
} |
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,60 @@ | ||
import * as Ariakit from "@ariakit/react"; | ||
import Square from "../select-grid/square.jsx"; | ||
import "./style.css"; | ||
|
||
export default function Example() { | ||
const select = Ariakit.useSelectStore({ | ||
defaultValue: "Center", | ||
placement: "bottom", | ||
setValueOnMove: true, | ||
}); | ||
const value = select.useState("value"); | ||
|
||
const renderItem = (value: string) => ( | ||
<Ariakit.SelectItem | ||
value={value} | ||
className="select-item" | ||
focusOnHover={(event) => { | ||
// When the mouse leaves the item, we don't want to unset the active | ||
// item. | ||
if (event.type === "mouseleave") return false; | ||
// By default, hovering over an item doesn't focus it, nor does it set | ||
// the value. So we need to manually "move" to the item so it gets | ||
// focused and the value is set. | ||
select.move(event.currentTarget.id); | ||
return true; | ||
}} | ||
> | ||
<Ariakit.VisuallyHidden>{value}</Ariakit.VisuallyHidden> | ||
</Ariakit.SelectItem> | ||
); | ||
|
||
return ( | ||
<div className="wrapper"> | ||
<Ariakit.SelectLabel store={select}>Position</Ariakit.SelectLabel> | ||
<Ariakit.Select store={select} showOnKeyDown={false} className="button"> | ||
<Square value={value} /> | ||
{value} | ||
<Ariakit.SelectArrow /> | ||
</Ariakit.Select> | ||
<Ariakit.SelectPopover store={select} role="grid" className="popover"> | ||
<Ariakit.PopoverArrow className="arrow" /> | ||
<Ariakit.SelectRow className="row"> | ||
{renderItem("Top Left")} | ||
{renderItem("Top Center")} | ||
{renderItem("Top Right")} | ||
</Ariakit.SelectRow> | ||
<Ariakit.SelectRow className="row"> | ||
{renderItem("Center Left")} | ||
{renderItem("Center")} | ||
{renderItem("Center Right")} | ||
</Ariakit.SelectRow> | ||
<Ariakit.SelectRow className="row"> | ||
{renderItem("Bottom Left")} | ||
{renderItem("Bottom Center")} | ||
{renderItem("Bottom Right")} | ||
</Ariakit.SelectRow> | ||
</Ariakit.SelectPopover> | ||
</div> | ||
); | ||
} |
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 @@ | ||
@import url("../select-grid/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 @@ | ||
import "../select-grid/test.js"; |
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.
662742f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ariakit – ./
ariakit-ariakit.vercel.app
ariakit-git-main-ariakit.vercel.app
www.ariakit.org
ariakit.org