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

182
Views
conditional rendering order of the elements based on props?

I created a search input component and this input has an icon which I need to change the icon's align (on the left side or right side) based on different situations (it's a bootstrap input-group) so all I need to do is change the order of elements inside my div and I need a clean way to do this. I know I can use a ternary operation to conditional render my elements but this would cause to repeat the code. I'm looking for much cleaner way so any suggestions?

const SearchInput = ({onClose, onChangeValue, ...restProps}) => {
    return (
        <div className='input-group'>
            <button className='btn' type='button'> //right now its on left side...
                <i className='bi bi-search'></i>
            </button>
            <Input placeholder='Search here...' onChangeValue={onChangeValue} {...restProps} />
            {onClose && <i className='bi bi-x' onClick={onClose}></i>}
        </div>
    );
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

My proposal

import clsx from 'clsx';
const SearchInput = ({onClose, onChangeValue, conditionProp, ...restProps}) => {
    return (
        <div className={clsx('input-group', conditionProp && 'my-order)}>
            <button className='btn' type='button'> //right now its on left side...
                <i className='bi bi-search'></i>
            </button>
            <Input placeholder='Search here...' onChangeValue={onChangeValue} {...restProps} />
            {onClose && <i className='bi bi-x' onClick={onClose}></i>}
        </div>
    );
};

In CSS

.my-order {
  flex-direction: row-reverse;
}

I like cslsx for conditional classnames, but of course I can do without it - conditionProp ? 'input-group my-order' : 'input-group'

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!