Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

206
Views
How to dynamically add isFixed if the items length is one in react-select?

I want to make a multi filter so that user can remove any of the item until items length is one. I want to add isFixed = true in the object dynamically when user remove 2 out of 3 items for this below example.

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

const options = [
    { value: 'apple', label: 'Apple' },
    { value: 'orange', label: 'Orange' },
    { value: 'grape', label: 'Grape' },
];
const orderOptions = (values) =>
    values && values.filter((v) => v.isFixed).concat(values.filter((v) => !v.isFixed));
export default function TestSelect() {
    const [value, setValue] = useState(orderOptions([...options]));
    console.log('value', value);
    const onChange = (value, { action, removedValue }) => {
        switch (action) {
            case 'remove-value':
            case 'pop-value':
                if (removedValue.isFixed) {
                    return;
                }
                break;
            case 'clear':
                value = options.filter((v) => v.isFixed);
                break;
            default:
                break;
        }
        value = orderOptions(value);
        setValue(value);
    };
    return (
        <div>
            <Select
                value={value}
                isMulti
                isClearable={value && value.some((v) => !v.isFixed)}
                name="fruit"
                classNamePrefix="select"
                onChange={onChange}
                options={options}
                defaultValue={options}
            />
        </div>
    );
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You just have to check if there's only one element remaining in the values array within the switch-case. To do so, just add two lines:

...
const onChange = (value, { action, removedValue }) => {
    switch (action) {
      case "remove-value":
      case "pop-value":
        if (removedValue.isFixed) return;
        if (value.length === 1) value[0].isFixed = true; // <-- add the isFixed element
        break;
      case "clear":
        value = options.filter((v) => v.isFixed);
        break;
      default:
        value.forEach(x => x.isFixed = false) // <-- remove the isFixed element
        break;
    }
    value = orderOptions(value);
    setValue(value);
  };
...

Remember to add the styles part to make it more visual. As you can see here.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!