Code Monkey home page Code Monkey logo

Comments (3)

vladmoroz avatar vladmoroz commented on May 27, 2024 15

The Select component should natively work with forms out of the box—we'll take a look

from themes.

onurhan1337 avatar onurhan1337 commented on May 27, 2024 1

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.

onurhan1337 avatar onurhan1337 commented on May 27, 2024 1

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.