键入时保持占位符可见

时间:2020-08-20 10:59:35

标签: html reactjs forms sass placeholder

我正在使用React并尝试建立一个网站,让您分析您的Twitter气泡。因此,我有一个表单,您可以在其中搜索Twitter用户名。因此,我在此表单中放置了一个“ @”作为占位符,当您开始输入时,该字符会消失。我的目标是在您开始键入时保留“ @”,例如我可以输入“ potus44”,并以这种形式显示“ @ potus44”(我希望这是有道理的)。因此,最后,“ @”不会消失,而是停留在Twitter句柄的前面。

查看我当前表单的屏幕截图:Screenshot

这是我的表单的代码。

import React, { Component } from 'react'; 
import { FiTwitter } from "react-icons/fi";

const app = {
    options: []
};

const onFormSubmit = (e) => {
    e.preventDefault();

    const option = e.target.elements.option.value;

    if (option) {
        app.options.push(option);
        e.target.elements.option.value = '';
    }
}

class Homepage extends Component {
    render () {
        return (
            <div className="homepage">
                <div className="container-pagecontent">
                <main style={{marginTop: '150px'}}>
                <h1 id="home" >Analyse your bubble! <FiTwitter size=".8em" /></h1>
                <p>Search for your bubble.</p>
                <form onSubmit={onFormSubmit}>
                    <input placeholder="@" className ="input" type="text" name="option"/>
                    <button className="button">Suchen</button>
                </form>
                </main>
                </div>
            </div>
        )
    }
}

export default Homepage

这是我的scss(以防可能对您有用)。

.button {
    background-color: #1763A5;
    font-family: bergen;
    color: white;
    border: 2px solid #1763A5;
    padding: 6px;
    font-size: 18px;
    border-radius: 6px;
    margin-left: 10px;
    transition-duration: 0.4s;
    box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
    outline: none;
}

.button:hover {
    background-color: white;
    color: #1763A5;
    box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}

.input {
    width: 150px;
}

input[type=text] {
    width: 170px;
    padding: 7px 10px;
    margin: 12px 0;
    margin-right: 2px; 
    box-sizing: border-box;
    border: 2px solid #1763A5;
    border-radius: 6px;
    box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
    outline: none;
  }

以及“ container-pagecontent”的代码:

.container-pagecontent {
    max-width: 95rem;
    margin: 0 auto;
    padding: 0 $m-size;
    width: 100%;
    text-align: center;
}

在此先感谢您的帮助。让我知道您是否需要对该项目有更多了解。

1 个答案:

答案 0 :(得分:1)

好的。 在styles.scss中::

.homepage {
  h1::before { 
      display: block; 
      content: " "; 
      margin-top: -150px; 
      height: 130px; 
      visibility: hidden; 
      pointer-events: none;
    }
  height: 110vh;
  background-color: white; 
  padding-left: 40px;
  padding-right: 40px;
}

@font-face {
  src: url(/fonts/BergenText-Regular.otf);
  font-family: bergen;
}

.button {
  background-color: #1763A5;
  font-family: bergen;
  color: white;
  border: 2px solid #1763A5;
  padding: 6px;
  font-size: 18px;
  border-radius: 6px;
  margin-left: 10px;
  transition-duration: 0.4s;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19);
  outline: none;
}

.button:hover {
  background-color: white;
  color: #1763A5;
  box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
}


.placeholder{
  position: relative;
}

.placeholder::after{
  content: attr(data-placeholder);
  position: absolute;
  left:0.325rem;
  top:33%;
}


input[type=text] {
  width: 170px;
  padding: 7px 0 7px 20px;
  margin: 12px 0;
  margin-right: 2px; 
  box-sizing: border-box;
  border: 2px solid #1763A5;
  border-radius: 6px;
  box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24), 0 17px 50px 0 rgba(0,0,0,0.19);
  outline: none;
}

在App.js中::

import React, { Component } from "react";
import "./styles.scss";

const app = {
  options: []
};

const onFormSubmit = (e) => {
  e.preventDefault();

  const option = e.target.elements.option.value;

  if (option) {
    app.options.push(option);
    e.target.elements.option.value = "";
  }
};

class Homepage extends Component {
  render() {
    return (
      <div className="homepage">
        <div className="container-pagecontent">
          <main style={{ marginTop: "150px" }}>
            <h1 id="home">Analyse your bubble!</h1>
            <p>Search for your bubble.</p>
            <form onSubmit={onFormSubmit}>
              <div className="placeholder" data-placeholder="@">
                <input
                  className="input"
                  type="text"
                  name="option"
                />
              </div>
              <button className="button">Suchen</button>
            </form>
          </main>
        </div>
      </div>
    );
  }
}

export default Homepage;