JavaScript implements an input box component

JavaScript implements an input box component

This article example shares the specific code for manually implementing an input box component for your reference. The specific content is as follows

background

In taro h5, I want to implement an input box with the following style:

question:

There is no component of this style in taro component and taro-ui component. Taro h5 does not support modifying the style of placeholder. I tried to implement an input component myself, which can define the style more flexibly.

accomplish

js code

import Taro, { Component } from '@tarojs/taro';
import { View } from '@tarojs/components';
import { AtIcon } from 'taro-ui';

import './index.scss';

/**
 * @description Manually implement an input box component* @param placeholder: String Customize the placeholder in the input box* @param onClickSearch: Function Get input content callback*/
class BaseInput extends Component {
  componentDidMount() {
    //Input box focus document.querySelector('.search').focus();
  }

  handleSearch = () => {
    //Get the content of the input box const value = document.querySelector('.search').innerText;
    this.props.onClickSearch && this.props.onClickSearch(value);
  };

  render() {
    const { placeholder = 'Please enter' } = this.props;
    return (
      <View className="base_input">
        <View className="my_search">
          <AtIcon
            value="search"
            color="#999"
            className="search_icon"
            onClick={this.handleSearch}
          />
          {/* contenteditable can control whether a div can be edited*/}
          <View
            className="search"
            contenteditable
            placeholder={placeholder}
          ></View>
        </View>
      </View>
    );
  }
}
export default BaseInput;

scss code

.base_input {
  .my_search {
    box-sizing: border-box;
    width: 690px;
    height: 56px;
    line-height: 56px;
    border-radius: 28px;
    margin: 12px auto;
    background: #f8f8f8;
    display: flex;
    .search_icon {
      width: 30px;
      height: 30px;
      margin-left: 20px;
      margin-right: 18px;
    }
    .search {
      width: 560px;
      padding: 0px 18px;
      background: #f8f8f8;
      height: 56px;
      color: #999;
      font-size: 28px;
      font-weight: 400;
      &:empty::after {
        content: attr(placeholder);
      }
    }
  }
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue.js Textbox with Dropdown component
  • Detailed explanation of how to use the Vue.js digital input box component
  • JS imitates Alipay input text input box enlargement component example
  • JavaScript component development: input box plus candidate box
  • JS uses regular expressions to restrict the input box to only allow integers and decimals (amounts or cash) with two decimal places
  • js and jquery real-time monitoring input box value oninput and onpropertychange methods
  • js monitors the instant changes of input box values ​​​​onpropertychange, oninput
  • JS implements the method of popping up an input box on the web page
  • js dynamically modifies the type attribute of the input box (implementation method analysis)
  • js monitors the real-time changes of the input box value

<<:  Method of realizing automated deployment based on Docker+Jenkins

>>:  MySQL multi-table query detailed explanation

Recommend

Install MySQL in Ubuntu 18.04 (Graphical Tutorial)

Tip: The following operations are all performed u...

Problems with using wangeditor rich text editing in Vue

wangEditor is a web rich text editor developed ba...

Introduction to the use of MySQL source command

Table of contents Thoughts triggered by an online...

Ideas and codes for realizing magnifying glass effect in js

This article example shares the specific code of ...

Mysql sorting to get ranking example code

The code looks like this: SELECT @i:=@i+1 rowNum,...

Interpretation of 17 advertising effectiveness measures

1. 85% of ads go unread <br />Interpretatio...

How to install and deploy ftp image server in linux

Refer to the tutorial on setting up FTP server in...

Ubuntu 16.04 image complete installation tutorial under VMware

This article shares with you the installation tut...

How to use nginx to build a static resource server

Taking Windows as an example, Linux is actually t...

How to make a div height adaptive to the browser height

This old question has troubled countless front-end...

JavaScript uses canvas to draw coordinates and lines

This article shares the specific code of using ca...

Vue+webrtc (Tencent Cloud) practice of implementing live broadcast function

Table of contents 1. Live broadcast effect 2. Ste...

Implementation of Nginx configuration https

Table of contents 1: Prepare https certificate 2:...