Comments (3)
The Select component should natively work with forms out of the box—we'll take a look
from themes.
Hi, @rago4,
The issue you're experiencing is due to how the Radix UI Select component handles its value. The Select component doesn't behave like a traditional HTML select element, and it doesn't directly interact with the form it's in. This means that when you submit the form, the value of the Select component isn't automatically included in the form data.
To work around this, you can create a hidden input field in your form that holds the value of the Select component. When the form is submitted, the value of the hidden input field will be included in the form data.
Here's how you can modify your code to include a hidden input field:
import { useState, useCallback } from 'react';
import { Button, Select } from '@radix-ui/themes';
import { v4 as uuidv4 } from 'uuid';
const colors = [
{ value: 'red', label: 'Color red' },
{ value: 'green', label: 'Color green' },
{ value: 'blue', label: 'Color blue' },
];
export default function App() {
const [color, setColor] = useState('');
const [key, setKey] = useState(uuidv4());
const reset = useCallback(() => {
setColor('');
setKey(uuidv4()); // Change key to reset Select component
}, []);
const handleSubmit = useCallback((event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
const form = new FormData(event.currentTarget);
console.log(Object.fromEntries(form.entries()));
}, []);
return (
<div>
<form onSubmit={handleSubmit}>
<Select.Root key={key} name="color" onValueChange={setColor}>
<Select.Trigger placeholder="Select a color" />
<Select.Content>
<Select.Group>
{colors.map((color) => (
<Select.Item key={color.value} value={color.value}>
{color.label}
</Select.Item>
))}
</Select.Group>
</Select.Content>
</Select.Root>
<input type="hidden" name="color" value={color} />
<Button type="submit">Submit</Button>
<Button type="button" variant="outline" onClick={reset}>
Clear
</Button>
</form>
</div>
);
}
In this code, I've added a hidden input field with the name color
and the value set to the current color state. When the form is submitted, the value of this hidden input field will be included in the form data.
from themes.
Actually, Using a UUID for the key in this specific case could be considered overengineering. The key just needs to change to trigger a re-render of the component, it doesn't need to be globally unique or follow the UUID format.
Using Math.random()
is perfectly fine in this case, as the likelihood of generating the same number twice in a row is extremely low.
So, you can stick with your original approach: I've gave an example with uuid.
from themes.
Related Issues (20)
- [Layout] Extract layout to separated package HOT 5
- Button's ghost variant has inconsistent padding HOT 3
- TextField autofocus HOT 1
- [Feature Request] Accordion
- Cannot find module `@radix-ui\themes\dist\cjs` HOT 2
- [Switch] Issue- Screen readers announce "Button" instead of "Switch". HOT 1
- [Tooltip] Issue: Text inside is based on theme HOT 1
- Button vanishes after adding type HOT 1
- [Dialog] Issue
- [Avatar] Conditionally set color HOT 3
- [Question] Why separate layout, margin and another props? HOT 1
- [DropdownMenu] Issue- Dropdown can't be navigated with TAB key HOT 2
- Left align Radio Card HOT 1
- Select Trigger Color Prop Broken HOT 1
- Improve documentation on customizing the CSS variables (e.g. the Container max widths) HOT 1
- [SegmentedControl] Disabled items not supported HOT 1
- [Question] accordions expanding with code HOT 1
- [Alert Dialog] - dialog has no close button
- Dialog in Dropdown Menu: Space Bar Not Working in Inputs HOT 5
- [RadioCards] Issue with import RadioCard HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from themes.