简体   繁体   English

使用 React 的输入占位符中的 FontAwesome 图标

[英]FontAwesome Icons in Input Placeholder using React

I'm using React for creating an app and I want to use FontAwesome icons for including them into the input placeholder, so that it would look like我正在使用 React 创建一个应用程序,我想使用 FontAwesome 图标将它们包含到输入占位符中,这样它看起来就像

<input type="text" placeholder="[here should be the user icon] Username" />

How can I do that?我怎样才能做到这一点? I've used "&#xF007; Username" and it shows only a square instead of an icon.我使用"&#xF007; Username" ,它只显示一个正方形而不是一个图标。 Maybe I forgot something?也许我忘了什么?

Try including your icon as an InputAddon rather than forcing it into your placeholder.尝试将您的图标包含在InputAddon中,而不是将其强制放入您的占位符中。 You can do this a few ways.您可以通过几种方式做到这一点。 I would suggest the two listed below.我建议下面列出的两个。

Approach 1: Bootstrap方法一:自举

You can install bootstrap and use the input-group-prepend or input-group-append classes and place your icon there.您可以安装引导程序并使用input-group-prependinput-group-append类并将您的图标放在那里。

Example:例子:

    import { FaUserAlt } from 'react-icons/fa';

    render() {
      return (
        <div className="input-group mb-3">
          <div className="input-group-prepend">
            <i className="classes you have"><FaUserAlt /></i>
        </div>
        <input type="text" className="form-control" placeholder="Username" />
       </div>
     )
  }

This will require you to install bootstrap and react-icons .这将要求您安装bootstrapreact-icons The bootstrap install will allow you to use those classNames and the react-icons install will allow you to use <FaUserAlt /> as a component rather than going through CSS. bootstrap安装将允许您使用这些类名,而react-icons安装将允许您将<FaUserAlt /> classNames组件使用,而不是通过 CSS。 To install bootstrap use: npm i bootstrap .要安装bootstrap使用: npm i bootstrap To install react-icons use: npm i react-icons .要安装react-icons使用: npm i react-icons

Approach 2: Reactstrap方法 2:Reactstrap

You can use reactstrap which is essentially Bootstrap for react.你可以使用reactstrap ,它本质上是用于反应的 Bootstrap。 This will require you to install both reactstrap , react-icons , and bootstrap like so: npm i reactstrap bootstrap react-icons .这将要求您像这样安装reactstrapreact-iconsbootstrapnpm i reactstrap bootstrap react-icons

Example: From reactstrap documentation (slightly altered)示例:来自reactstrap文档(略有改动)

import React from 'react';
import { InputGroup, InputGroupText, InputGroupAddon, Input } from 'reactstrap';
import { FaUserAlt } from 'react-icons/fa';
const Example = (props) => {
  return (
    <div>
      <InputGroup>
        <Input placeholder="Username" />
        <InputGroupAddon addonType="append">
          <FaUserAlt />
        </InputGroupAddon>
      </InputGroup>
    </div>
  );
};

export default Example;

Alternative Approach替代方法

You can simply do the above with CSS.您可以简单地使用 CSS 执行上述操作。 I would go that route if you are only using this "icon placeholder" once.如果你只使用一次这个“图标占位符”,我会走那条路。 But, if you are consistently doing this throughout your app, I think it maybe worth it to look into the above two approaches.但是,如果您在整个应用程序中一直这样做,我认为研究上述两种方法可能是值得的。

Hope this helps anyone who comes across this.希望这可以帮助遇到此问题的任何人。

For anyone who faces this issue:对于面临此问题的任何人:

This one worked for me: How to include a Font Awesome icon in React's render()这对我有用: How to include a Font Awesome icon in React's render()

Instead of @fortawesome/react-fontawesome package, I used font-awesome package.我使用了 font-awesome 包,而不是 @fortawesome/react-fontawesome 包。

npm install --save font-awesome

Now import font-awesome in your index.js file现在在你的 index.js 文件中导入 font-awesome

import '../node_modules/font-awesome/css/font-awesome.min.css'; 

or要么

import 'font-awesome/css/font-awesome.min.css';

Then the code in question goes like this:然后有问题的代码是这样的:

<input type="text" placeholder="&#xF007; Username" />

Just copy unicode of your desired icon from https://fontawesome.com and replace F007 with it.只需从https://fontawesome.com复制所需图标的 unicode 并用它替换F007

And style input tag with following:并使用以下样式输入标签:

font-family: FontAwesome;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM