Hide the empty line on a select

I have that code using React and I have that select, I would like in the case where I got empty labels and values I don’t want that items. How is it possible ?

Here is my code :

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

let cheeses = [
  "Wagasi",
  "Kalari",
  "Halloumi",
  "Manouri"
];

let options = [];
options = options.concat(cheeses.map((x) => "Cheese - " + x));

const Foo = () => {
  const [value, setValue] = useState("");

  function MakeOption(x) {
    if(value){
    return { value: x, label: x };
    }
    else{
      return {value: "", label: ""};
    }
  }


  const handleInputChange = (value, e) => {
    if (e.action === "input-change") {
      setValue(value);
    }
  };

  return (
    <Select
      isMulti
      name="colors"
      options={options.map((x) => MakeOption(x))}
      className="basic-multi-select"
      classNamePrefix="select"
      closeMenuOnSelect={false}
      onInputChange={handleInputChange}
      inputValue={value}
      noOptionsMessage={() => null}
    />
  );
};

export default Foo;

At that moment I got that :

Hide the empty line on a select

Could you help me please ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You could just filter your array after the map :

options={options.map((x) => MakeOption(x)).filter(opt => opt.value !== "")}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x