Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda