Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an icon before the input element in react-select

I am trying to add an icon in front of the input element of react select. I am able to get the icon in placeholder but the problem with a placeholder is that when I select some data from the dropdown the placeholder icon gets removed. I need some help to get the icon in front of the Select statement.

Here's the code of what I have achieved till now

import React, { Component } from 'react'
import Select, { components } from 'react-select'

export default class InfluencersForm extends Component {

    constructor(){
        super();
        this.handleInfluencerName = this.handleInfluencerName.bind(this);
    }
    handleInfluencerName(event){
        console.log(event)
    }
    render() {
        const influencers = [
            { value: 'abc', label: 'abc' },
            { value: 'def', label: 'def' }
        ]

        const DropdownIndicator = (props) => {
            return components.DropdownIndicator && (
                <components.DropdownIndicator {...props}>
                    <i className="fa fa-search" aria-hidden="true" style={{ position: 'initial' }}></i>
                </components.DropdownIndicator>
            );
        };
        return (
            <div>
                <div>
                    <Select
                        options={influencers}
                        isMulti={false}
                        onChange={this.handleInfluencerName}
                        isSearchable={true}
                        components={{ DropdownIndicator }}
                        placeholder={placeholderComponent}
                        classNamePrefix="vyrill"/>
                </div>
            </div>
        )
    }
}

const placeholderComponent = (
    <div>
        <i className="fa fa-search" aria-hidden="true" style={{ position: 'initial' }}></i>
        I am placeholder
    </div>
);
like image 309
Aniruddh Agarwal Avatar asked Dec 26 '18 07:12

Aniruddh Agarwal


People also ask

How to implement dynamic input fields in react?

Steps to implement dynamic input fields Create react application Design the form Implement logic to add/remove fields Output 1. Create react application Let’s start by creating a simple react application with the help of the create-react-app.

How to put icon inside an input element in a form?

CSS to put icon inside an input element in a form. To add icon inside the input element the <i> tag and <span> tag are used widely to add icons on the webpages. To add any icons on the webpages or in some specific area, it needs the fontawesome link inside the head tag.

How does the control field work in react?

Now that you know how the control field works in React, adding the other input fields will be a piece of cake. Unlike regular HTML where we define the text in between the textarea element. In React, the textarea is defined as a self-closing element just like the input element.

What is an uncontrolled input in react?

In this case, we call this type of input an uncontrolled input. In React, it is the responsibility of the component rendering the form to control the input state. This way, the input would no longer listen to its internal state but the state declared in its component. By so doing, we are making the component state a single source of truth.


1 Answers

Based on what you've already done I would do a combination of custom style + custom component.

export default class InfluencersForm extends Component {
  constructor() {
    super();
    this.handleInfluencerName = this.handleInfluencerName.bind(this);
  }
  handleInfluencerName(event) {
    console.log(event);
  }
  render() {
    const influencers = [
      { value: "abc", label: "abc" },
      { value: "def", label: "def" }
    ];

    const ValueContainer = ({ children, ...props }) => {
      return (
        components.ValueContainer && (
          <components.ValueContainer {...props}>
            {!!children && (
              <i
                className="fa fa-search"
                aria-hidden="true"
                style={{ position: 'absolute', left: 6 }}
              />
            )}
            {children}
          </components.ValueContainer>
        )
      );
    };

    const DropdownIndicator = props => {
      return (
        components.DropdownIndicator && (
          <components.DropdownIndicator {...props}>
            <i
              className="fa fa-search"
              aria-hidden="true"
            />
          </components.DropdownIndicator>
        )
      );
    };

    const styles = {
      valueContainer: base => ({
        ...base,
        paddingLeft: 24
      })
    }

    return (
      <div>
        <div>
          <Select
            options={influencers}
            isMulti={false}
            onChange={this.handleInfluencerName}
            isSearchable={true}
            components={{ DropdownIndicator, ValueContainer }}
            classNamePrefix="vyrill"
            styles={styles}
          />
        </div>
      </div>
    );
  }
}

In my custom style I have added an arbitrary paddingLeft of 24 to make some space and add the desired icon. You might have to change it depending of the icon you want to use.

Then in ValueContainer next to the children I have put the fontAwesome icon.

Here a live example of my solution.

like image 129
Laura Avatar answered Oct 20 '22 07:10

Laura