简体   繁体   English

Select 依赖于 reactjs 与 react-select

[英]Select dependent on reactjs with react-select

I'm developing an App in ReactJS, and I have a page where I want to show two select, one dependent on the other.我正在 ReactJS 中开发一个应用程序,我有一个页面我想显示两个 select,一个依赖于另一个。

I'm using react-select and @material-ui .我正在使用react-select@material-ui

In dates :dates

[
  {
    "id": 1,
    "name": "202001"
  },
  {
    "id": 2,
    "name": "202002"
  },
  {
    "id": 3,
    "name": "202003"
  },
  {
    "id": 4,
    "name": "202004"
  },
  {
    "id": 5,
    "name": "202005"
  },
  {
    "id": 6,
    "name": "202006"
  },
  {
    "id": 7,
    "name": "202007"
  }
]

I have a list of dates that are available to select.我有一个可供 select 使用的日期列表。

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

const App = () => {

    ...

    const DateA = dates.map((item) => ({
        value: item.id,
        label: item.name,
    }));

    const DateB = dates.map((item) => ({
        value: item.id,
        label: item.name,
    }));

    const [dateA, setDateA] = React.useState(null);
    const [dateB, setDateB] = React.useState(null);

    function handleChangeDateA(value) {
        setDateA(value);
    }

    function handleChangeDateB(value) {
        setDateB(value);
    }

    return (
            <div className="App">
                <div className="col-3">
                    <Select
                        classes={classes}
                        styles={selectStyles}
                        inputId="DateA"
                        TextFieldProps={{
                            label: "DateA",
                            InputLabelProps: {
                            htmlFor: "DateA",
                            shrink: true,
                            },
                            placeholder: "DateA...",
                        }}
                        options={DateA}
                        components={components}
                        value={dateA}
                        onChange={handleChangeDateA}
                    />
                </div>
                <div className="col-3">
                    <Select
                        classes={classes}
                        styles={selectStyles}
                        inputId="DateB"
                        TextFieldProps={{
                            label: "DateB",
                            InputLabelProps: {
                                htmlFor: "DateB",
                                shrink: true,
                            },
                            placeholder: "DateB...",
                        }}
                        options={DateB}
                        components={components}
                        value={dateB}
                        onChange={handleChangeDateB}
                    />
                </div>
            </div>
    );
};

export default App;

The idea is that the DateB select take dates greater than the ones selected in the DateA select.这个想法是DateB select 采用的日期大于DateA select 中选择的日期。

How can I do this, suggestions?我该怎么做,建议?

Try this尝试这个

useEffect(() => {
        if (condition) {
         setDateB(value)
    }
          
   }, [DateA])

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

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