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

160
Views
How i can shorten the following switch case condition?

I'm trying to select a text field depending on the selected variable type and I don’t want to use the textfield component in every suite condition.

const setInputType = () => {
        switch (type) {
            case 'boolean':
                return <Select size='small' defaultValue='True' onChange={(e) => onChangeProp('value', e.target.value)}>
                    <MenuItem value='True'>True</MenuItem>
                    <MenuItem value='False'>False</MenuItem>
                </Select>;
            case 'array':
                return <TextField
                    size='small'
                    variant="outlined"
                    defaultValue={[...value]}
                    onChange={(e) => onChangeProp('value', e.target.value)}
                />;
            case 'Date':
                return <TextField
                    size='small'
                    variant="outlined"
                    inputProps={{ type: 'Date' }}
                    defaultValue={value}
                    onChange={(e) => onChangeProp('value', e.target.value)}
                />;
            case 'number':
                return <TextField
                    size='small'
                    variant="outlined"
                    inputProps={{ type: 'number' }}
                    defaultValue={value}
                    onChange={(e) => onChangeProp('value', e.target.value)}
                />;
            default:
                return <TextField
                    size='small'
                    variant="outlined"
                    defaultValue={value}
                    onChange={(e) => onChangeProp('value', e.target.value)}
                />;
        }
    }

    const inputType = setInputType();
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

One way is:

 const setInputType = () => { switch (type) { case 'boolean': return ( <Select size='small' defaultValue='True' onChange={(e) => onChangeProp('value', e.target.value)}> <MenuItem value='True'>True</MenuItem> <MenuItem value='False'>False</MenuItem> </Select> ); default: return ( <TextField size='small' variant="outlined" defaultValue={value} onChange={(e) => onChangeProp('value', e.target.value)} {...['Date', 'number'].includes(type) && {inputProps: {type}}} /> ); } }

about 4 years ago · Santiago Trujillo 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!