Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value from Select/Option component from Ant design

I want to retrieve the value from my selection so I can make post requests. I have no problem getting from text input, but for some reason I can't get it from the drop down menu select. I end up getting a "TypeError: Cannot read property 'value' of undefined"

Here is the code I am using.

import React from "react";
import { Form, Input, Button, Select } from "antd";

const { Option } = Select;

class ItemForm extends React.Component {

  handleFormSubmit = event => {
    event.preventDefault();
    const name = event.target.elements.name.value;
    const description = event.target.elements.description.value;
    const category = event.target.elements.category.value;
    console.log(name, description, this.refs.category.value);
  };

  render() {
    return (
      <div>
        <Form onSubmit={this.handleFormSubmit}>
          <Form.Item label="Form Layout" />
          <Form.Item label="Product Name">
            <Input name="name" placeholder="Ex: Organic Apple..." />
          </Form.Item>
          <Form.Item label="Description">
            <Input name="description" placeholder="Ex: Juicy organic apples!" />
          </Form.Item>
          <Form.Item label="Category">
            <Select name="category" placeholder="Please select a category">
              <Option value="Fruit">Fruit</Option>
              <Option value="Vegetable">Vegetable</Option>
              <Option value="Poultry">Poultry</Option>
            </Select>
          </Form.Item>
          <Form.Item>
            <Button type="primary" htmlType="submit">
              Submit
            </Button>
          </Form.Item>
        </Form>
      </div>
    );
  }
}
export default ItemForm;
like image 619
Tesuji Avatar asked Apr 22 '19 02:04

Tesuji


People also ask

How do I select value from options?

Select component to select value from options. A dropdown menu for displaying choices - an elegant alternative to the native <select> element. Utilizing Radio is recommended when there are fewer total options (less than 5).

How to select multiple items from existing items in an option?

Multiple selection, selecting from existing items. Specify the prop name of Option which will be rendered in select box. Select with tags, transform input to tag (scroll the menu).

How to get the value of the selected item using onchange?

Using the Cascader component is strongly recommended instead as it is more flexible and capable. As a default behavior, the onChange callback can only get the value of the selected item. The labelInValue prop can be used to get the label property of the selected item.

What is the default height of the input field for select?

The height of the input field for the select defaults to 32px. If size is set to large, the height will be 40px, and if set to small, 24px. Using OptGroup to group the options. Search with remote data.


1 Answers

Use onChange which is fired when the value of the select changes. antd select documentation

<Form.Item label="Category">
  <Select 
    onChange={(value) => {
      alert(value)
    }} 
    name="category" 
    placeholder="Please select a category">
      <Option value="Fruit">Fruit</Option>
      <Option value="Vegetable">Vegetable</Option>
      <Option value="Poultry">Poultry</Option>
  </Select>
</Form.Item>

working example

like image 184
Junius L. Avatar answered Oct 03 '22 23:10

Junius L.