0

I am developing a react application based on hooks and for multi select dropdown am using react semantic ui Dropdown. But there is no option to Select All items. How to acheieve this? On click of Select All all the items should be selected and not be shown in the dropdown:-

import React, { useState } from "react";
import { Dropdown } from "semantic-ui-react";

const options = [
  { key: "select all", text: "Select All", value: "Select All" },
  { key: "angular", text: "Angular", value: "angular" },
  { key: "css", text: "CSS", value: "css" },
  { key: "design", text: "Graphic Design", value: "design" },
  { key: "ember", text: "Ember", value: "ember" },
  { key: "html", text: "HTML", value: "html" },
  { key: "ia", text: "Information Architecture", value: "ia" },
  { key: "ux", text: "User Experience", value: "ux" }
];

const DropdownExampleMultipleSelection = () => {
  const [selectedItems, setSelectedItems] = useState([]);
  const onChange = (event, data) => {
    setSelectedItems(data.value);

    if (data.value == "Select All") {
      console.log(data);
      console.log(event.target);
    }
  };

  return (
    <Dropdown
      placeholder="Skills"
      clearable
      fluid
      multiple
      selection
      options={options}
      onChange={onChange}
      value={selectedItems}
      label="Skills"
    />
  );
};

export default DropdownExampleMultipleSelection;
1
  • 2
    Please, include the code you already have. This will let the community give you a better anwser. Commented Aug 16, 2020 at 15:12

1 Answer 1

1

You need to check for the last selected items with event.target.textContent and add all options if this is selected.

const onChange = (event, data) => {
    if(event.target.textContent === 'Select All') {
      setSelectedItems(data.options.map(d => d.value))
    } else {
      setSelectedItems(data.value);
    }
  };
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting a warning when I add value prop to Dropdown.Could any one help.Warning: Failed prop type: Invalid prop value supplied to Dropdown.
I think it needs to be values not value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.