स्पष्ट एकाधिक प्रतिक्रिया का चयन करें

0

सवाल

मैं एक का चयन करें:

<Select isClearable classNamePrefix="select" ref={myRef} menuPortalTarget={document.body} styles={style} placeholder="Select Foods" name="Foods" value={inputField.foods} options={options} onChange={event => handleInputChange2(index, event)} className="select selectNarrow">

और मैं एक बटन है कि करने के लिए स्पष्ट सभी का चयन करता है:

<IconButton size="small" aria-label="edit" onClick={() => handleRemoveFieldsAll(index)}><RemoveCircleIcon /></IconButton>

समारोह:

const handleRemoveFieldsAll = (index, event) => {
    const values = [...inputFields];
    setInputFields(INITIAL_STATE);
    values.splice(0);
    console.log(values);
  };

इस काम को हटाने के द्वारा और खाली सरणी है, लेकिन लेबल का चयन बनी हुई है । कैसे मैं स्पष्ट सभी शेष लेबल वापस करने के लिए प्लेसहोल्डर?

react-select reactjs
2021-11-24 06:38:37
1

सबसे अच्छा जवाब

0

इस दृष्टिकोण की कोशिश करो,

मैं बनाया है दो स्थानीय अमेरिका में यहाँ. एक के लिए विकल्प और अन्य के लिए चयनित विकल्प है । राज्य को अद्यतन करने पर आधारित बटन पर क्लिक करें और एक ही परिवर्तन को प्रतिबिंबित करेगा टेम्पलेट में.

import { IconButton } from "@material-ui/core";
import { useState } from "react";
import Select from "react-select";

import "./styles.css";

const opts = [
  { value: "chocolate", label: "Chocolate" },
  { value: "strawberry", label: "Strawberry" },
  { value: "vanilla", label: "Vanilla" }
];

export default function App() {
  const [options, setOptions] = useState(opts);
  const [selected, setSelected] = useState(opts[0]);
  const handleRemoveFieldsAll = (event) => {
    setSelected(null);
    setOptions([]);
  };

  const handleInputChange2 = (option) => {
    setSelected(option);
  };

  const handlePopulateFieldsAll = () => {
    setSelected(opts[0]);
    setOptions(opts);
  };

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <Select
        isClearable
        classNamePrefix="select"
        // ref={myRef}
        menuPortalTarget={document.body}
        // styles={style}
        placeholder="Select Foods"
        name="Foods"
        value={selected}
        options={options}
        onChange={(event) => handleInputChange2(event)}
        className="select selectNarrow"
      ></Select>

      <IconButton
        size="small"
        aria-label="edit"
        onClick={() => handleRemoveFieldsAll()}
      >
        Reset all
      </IconButton>

      <IconButton
        size="small"
        aria-label="edit"
        onClick={() => handlePopulateFieldsAll()}
      >
        Populate all
      </IconButton>
    </div>
  );
}

CODESANDBOX - https://codesandbox.io/s/intelligent-moore-nn8w5?file=/src/App.js:0-1483

मुझे पता है कि मामले में मैं कर रहा हूँ नहीं है को कवर करने के अपने उपयोग के मामले में ।

2021-11-24 08:07:36

इस तरह लग रहा होगा आपके मामले में काम है, मैं कोशिश कर रहा हूँ का उपयोग करने के लिए myRef.वर्तमान.का चयन करें । clearValue() ... लेकिन यह केवल साफ करता है पिछले का चयन करें (वहाँ कई हैं) के बजाय सभी. तो मैं प्रभावी ढंग से nulling सरणी लेकिन प्लेसहोल्डर अभी भी बनी हुई है
Paul VI

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में