Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Select]: fix onValueChange will call with empty string when side form #3259

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions packages/react/select/src/Select.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { css } from '../../../../stitches.config';
import * as Select from '@radix-ui/react-select';
import { Label } from '@radix-ui/react-label';
Expand Down Expand Up @@ -511,6 +512,62 @@ export const WithinForm = () => {
);
};

export const RefreshItemsWithinForm = function SelectDemo() {
const [value, setValue] = React.useState<string>('apple');
const [hide, setHide] = React.useState(false);

return (
<form onSubmit={(e) => e.preventDefault()}>
<p>current value: {value}</p>
<button
onClick={() => {
ReactDOM.flushSync(() => {
setHide(true);
});
ReactDOM.flushSync(() => {
setValue('banana');
setHide(false);
});
}}
>
Update to banana
</button>

<Select.Root value={value} onValueChange={setValue}>
<Select.Trigger className={triggerClass()}>
<Select.Value placeholder="select a fruit" />
<Select.Icon />
</Select.Trigger>
<Select.Portal>
<Select.Content className={contentClass()}>
<Select.Viewport className={viewportClass()}>
{hide ? null : (
<>
<Select.Item className={itemClass()} value="apple">
<Select.ItemText>Apple</Select.ItemText>
</Select.Item>
<Select.Item className={itemClass()} value="banana">
<Select.ItemText>Banana</Select.ItemText>
</Select.Item>
<Select.Item className={itemClass()} value="blueberry">
<Select.ItemText>Blueberry</Select.ItemText>
</Select.Item>
<Select.Item className={itemClass()} value="grapes">
<Select.ItemText>Grapes</Select.ItemText>
</Select.Item>
<Select.Item className={itemClass()} value="pineapple">
<Select.ItemText>Pineapple</Select.ItemText>
</Select.Item>
</>
)}
</Select.Viewport>
</Select.Content>
</Select.Portal>
</Select.Root>
</form>
);
};

export const DisabledWithinForm = () => {
const [data, setData] = React.useState({});

Expand Down
4 changes: 3 additions & 1 deletion packages/react/select/src/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,9 @@ const BubbleSelect = React.forwardRef<HTMLSelectElement, React.ComponentPropsWit
if (prevValue !== value && setValue) {
const event = new Event('change', { bubbles: true });
setValue.call(select, value);
select.dispatchEvent(event);
// delay the dispatch to the next tick to ensure React has updated the select options
const id = window.setTimeout(() => select.dispatchEvent(event));
return () => window.clearTimeout(id);
}
}, [prevValue, value]);

Expand Down