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

97
Visualizações
Multiple comboboxes value in React

I am creating a page where the user choose to upload some files,then choose the file type of each file from a combo box then submit. 'reports' are the files the user chosed to upload, i mapped over them to show them in rows then the user can choose for each file the crossponding file type. How to get the value in the combo box for each file the user uploaded? My code runs for only one file but how can be edited to be for multiple comboboxes?

enter image description here

const [fileType, setFileType] = useState('');

    const fileChangeHandler = (event:React.ChangeEvent<HTMLSelectElement>) => {
        
        setFileType(event.target.value);
   
    }
return (
<div>
        {reports.map((report, i) => (
            <div className="file-row" key={i}>
                <div>{report.name}</div>
                <div>
                    <select className='frm-select' value={fileType} onChange={fileChangeHandler}>
                        <option value="" selected disabled hidden>Select File Type</option>
                        {fileTypeOptions.map((option) => (
                            <option key={option.id}>{option.FileName}</option>
                        ))}
                    </select>
                </div>
                {renderFileRow(report, i,fileType)}
            </div>
        ))}
    </div>
);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'd suggest you use a map (like an object but more dynamic) instead of a plain string. I'm not sure you have report.id or not, so I'm using report.name for this example

//create an empty map for all file types
const [fileTypes, setFileTypes] = useState<{ [key: string]: string }>({});

    const fileChangeHandler = (event:React.ChangeEvent<HTMLSelectElement>, reportName: string) => {
        //it will return an object for all file types
        setFileTypes({
           ...fileTypes,
           [reportName]: event.target.value
        });
   
    }
return (
<div>
        {reports.map((report, i) => (
            <div className="file-row" key={i}>
                <div>{report.name}</div>
                <div>
                    <select className='frm-select' value={fileTypes[report.name]} onChange={(e) => fileChangeHandler(e, report.name)}>
                        <option value="" selected disabled hidden>Select File Type</option>
                        {fileTypeOptions.map((option) => (
                            <option key={option.id}>{option.FileName}</option>
                        ))}
                    </select>
                </div>
                {renderFileRow(report, i,fileType)}
            </div>
        ))}
    </div>
);

The final results of fileTypes will be like this

{
   "report name 1": "pdf",
   "report name 2": "image"
}

Here is how we access file type for each report

fileTypes[report.name]
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