最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I use React icons in Placeholder? - Stack Overflow

programmeradmin1浏览0评论

Is it possible to use React icons in placeholder attribute of input field i-e input text for example?

If yes can some one give me an example?

I do it like this but instead of Icon I got [object object] in placeholder

var  emailIcon = <FaEnvelopeO  size={10}  />;
<Field
 placeholder={emailIcon}
 name="email"
 type="text"
 ponent={this.renderField}
/>

Is it possible to use React icons in placeholder attribute of input field i-e input text for example?

If yes can some one give me an example?

I do it like this but instead of Icon I got [object object] in placeholder

var  emailIcon = <FaEnvelopeO  size={10}  />;
<Field
 placeholder={emailIcon}
 name="email"
 type="text"
 ponent={this.renderField}
/>
Share Improve this question edited Sep 11, 2017 at 8:52 Vishal 11k22 gold badges89 silver badges136 bronze badges asked Sep 11, 2017 at 8:37 KahnKahn 4513 gold badges6 silver badges19 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Nope, placeholders are meant to contain text only. Look at the definition below:

The placeholder attribute places text inside the input in a light gray color. The text remains whenever the input has no value.

You can however use hexcodes like below:

<input type="text" placeholder="&#xF002;" style="font-family:Arial, FontAwesome" />

Here is an example using a functional React ponent, with the React-Icons and Styled-Components libraries. The key is the absolute and relative positioning on the icon and the container. Hope this helps!

import React from "react";

// libaries
import { IoMdSearch } from 'react-icons/io';
import styled from 'styled-ponents';

const SearchBarView = () => {
  return (
    <Search>
      <IoMdSearch style={{marginLeft: "1rem", position: "absolute"}} color="#623CEA" size="1.5em" />
      <SearchBar 
        id="search-bar" 
        type="text"
        placeholder="Search">
      </SearchBar>
    </Search>
  )
}

const Search = styled.div`
  padding: .5rem;
  position: relative;
  display: flex;  
  align-items: center;
  width: 100%;
`

const SearchBar = styled.input`
  padding: 1rem 1rem 1rem 3.5rem;
  width: 100%;
`

export default SearchBarView;

You can directly paste

发布评论

评论列表(0)

  1. 暂无评论