简体   繁体   English

如何使用 react-select 和 react-bootstrap 来获取值

[英]How to use react-select with react-bootstrap to get the values

I'm developing an App in ReactJS and I have the following code:我正在 ReactJS 开发一个应用程序,我有以下代码:

import React, { useState, useEffect } from "react";
import {Form } from "react-bootstrap";
import Select from "react-select";

const App = () => {

    const [validated, setValidated] = useState(false);

    const [select, setSelect] = useState({
        name: "",
        category: ""
    });

    const handleChange = e => {
        const {name, value} = e.target;
        setSelect(prevState => ({
        ...prevState,
        [name]: value
        }));
    };

    const [data, setData] = useState([]);
    ...
    /* get data */
    ...

    const categories = data.map((item) => ({ value: item.id, label: item.Name }));

    return (
        <div className="app">
            <Form noValidate validated={validated}>
                <Form.Row>
                    <Form.Group as={Col} md="4" controlId="validationCustom01">
                        <Form.Label>Name</Form.Label>
                        <Form.Control
                            required
                            name="name"
                            type="text"
                            placeholder="Name"
                            onChange={handleChange}
                        />
                        <Form.Control.Feedback type="invalid">Check!</Form.Control.Feedback>
                        <Form.Control.Feedback>Ok!</Form.Control.Feedback>
                    </Form.Group>
                    <Form.Group as={Col} md="4" controlId="validationCustom02">
                        <Form.Label>Category</Form.Label>
                        <Form.Control
                            required
                            name="category"
                            as="select"
                            placeholder="Category"
                            onChange={handleChange}
                        >
                        <Select
                                options={categories}
                                defaultValue={true}
                                onChange={handleChange}
                                name="category"
                                id="search-select"
                            />
                        </Form.Control>
                        <Form.Control.Feedback type="invalid">Check!</Form.Control.Feedback>
                        <Form.Control.Feedback>Ok!</Form.Control.Feedback>
                    </Form.Group>
                </Form.Row>
            </Form>
        </div>
    );
}
        
export default App;

And I want to be able to use react-select together with react-bootstrap as shown above.而且我希望能够将react-selectreact-bootstrap一起使用,如上所示。 I need the Select component to adapt to the code so that I can use the react-bootstrap validation and have all the data selected/entered to be able to insert it, I need to pass those as follows, as an example:我需要Select组件来适应代码,以便我可以使用react-bootstrap验证并选择/输入所有数据以便能够插入它,我需要通过如下方式,例如:

console.log(select);

{name: "", category: ""}
name: "Name"
category: "Category"
__proto__: Object

How can I do it?我该怎么做? I need it because otherwise I have to use我需要它,否则我必须使用

<Form.Control
    required
    name="category"
    as="select"
    placeholder="Category"
    onChange={handleChange}
>
<option hidden value="" selected>Select</option>
    {
        categories.map(elem => {
            return <option  ... >Name</option>
        })
    }
</Form.Control>

and the select remains without style. select 仍然没有风格。

在此处输入图像描述 Thanks!谢谢!

I am assuming you simply want to use react-select in your app.我假设您只是想在您的应用程序中使用 react-select。 This should be pretty straightforward.这应该很简单。 I have created a dedicated element for you that uses react-select:我为您创建了一个使用 react-select 的专用元素:

const SelectBox = ({ options }) => {
  const [optionSelected, setSelectedOptions] = useState([]);

  const handleChange = (selected) => {
    setSelectedOptions(selected);
  };

  return (
    <Select
      options={options}
      isLoading={!options}
      closeMenuOnSelect={true}
      onChange={handleChange}
      value={optionSelected}
      name={"your_select_name"}
    />
  );
};

You can just render it like so:你可以像这样渲染它:

...
<SelectBox options={categories} />
...

Sandbox:https://codesandbox.io/s/upbeat-torvalds-kzcug?file=/src/App.js沙盒:https://codesandbox.io/s/upbeat-torvalds-kzcug?file=/src/App.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM