search
HomeWeChat AppletMini Program DevelopmentHow to develop a WeChat applet search component from scratch

This article introduces the method of developing a WeChat applet search component from scratch. I hope it will be helpful to friends who are learning WeChat applet development!

How to develop a WeChat applet search component from scratch

How to develop a WeChat applet search component from scratch

Set a container for the component, place the search icon and input in the container box, clear text button, and search button.

How to develop a WeChat applet search component from scratch

<view class="input-wrapper">
    <icon class="search-icon"/>
    <input
        placeholder=&#39;{{placeholder}}&#39; 
        value=&#39;{{inputValue}}&#39; 
        bindinput=&#39;handleInput&#39; 
        bindconfirm=&#39;handleSearch&#39;
        bindfocus=&#39;inputFocused&#39;>
    </input>
    <view class="close-icon-wrapper">
        <icon class="close-icon"/>
    </view>
    搜索
</view>

Component style (recommended learning: 小program development)

container: height 100 rpx, background color# eee, flex layout.

input-wrapper: height 80 rpx, background color #fff, flex layout, border-radius: 20rpx.

search-icon: width and height 32 rpx.

input: font and cursor color #000, font size 32 rpx.

close-icon-wrapper: width and height 80 rpx, absolute positioning.

text: Search button 110 rpx wide, 65 rpx high, absolutely positioned, left border 2rpx solid #eee.

.container {
    background: #eee;
    height: 100rpx;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}
.input-wrapper {
    display: flex;
    align-items: center;
    height: 80rpx;
    width: 80%;
    background: #fff;
    border-radius: 20rpx;
}
.input-wrapper .search-icon {
    margin-left: 20rpx;
    width: 32rpx;
    height: 32rpx;
}
.input-wrapper input {
    margin-left: 10rpx;
    color: #000;
    font-size: 32rpx;
    caret-color: #000;
    width: 60%;
}
.input-wrapper .close-icon-wrapper{
    position: absolute;
    left: 480rpx;
    width: 80rpx;
    height: 80rpx;
    background:#fff;
    display: flex;
    justify-content: center;
    align-items: center;
}
.input-wrapper .close-icon {
    width: 42rpx;
    height: 42rpx;
}
.input-wrapper text {
    position: absolute;
    right: 80rpx;
    width: 110rpx;
    height: 65rpx;
    padding: 0;
    background: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 32rpx;
    border-left: 2rpx solid #eee;
}

Component function

How to develop a WeChat applet search component from scratch

In the constructor of the component, pay attention to distinguishing properties and data. Write the component in properties. For external properties, data writes the internal properties of the component. In this search component, placeholder and value are passed from the page, so they are written in properties, and showCloseIcon, which controls whether the clear button appears, is written in data.

properties: {
    placeholder: {
        type: String,
        value: &#39;搜索&#39; // 如果页面不传placeholder,显示“搜索”
    },
    inputValue: {
        type: String
    }
},
data: {
    showCloseIcon: false,
},

2. Method settings

Event process

(1) The cursor is not focused and there is no input - the search icon, placeholder and search are displayed button.

(2) The cursor is focused and there is no input - the cursor flashes and the search icon, placeholder and search button are displayed.

(3) The cursor is focused and there is input - the cursor flashes and the search icon, input text, clear button and search button are displayed.

(4) The cursor is not focused and there is input - the search icon, input text, clear button and search button are displayed.

(5) Press Enter to search - the clear button is hidden.

(6) Click the search button - clear the button to hide.

It can be seen that the input component's focus and keyboard input events are required.

How to develop a WeChat applet search component from scratch

<view
    placeholder=&#39;{{placeholder}}&#39; 
    value=&#39;{{inputValue}}&#39; 
    bindinput=&#39;handleInput&#39; 
    bindconfirm=&#39;handleSearch&#39;
    bindfocus=&#39;inputFocused&#39;>
</view>

inputFocused: If there is content in the input box during focus, closeIcon is displayed;

handleInput: If there is no content during input, it is not displayed. closeIcon has content, displays closeIcon and stores the value in value.

handleSearch: After clicking Enter, closeIcon is not displayed.

triggerEvent: When a custom component triggers an event, you need to use the triggerEvent method to specify the event name, detail object and event options.

inputFocused(e) {
            if (e.detail.value !== &#39;&#39;) {
                this.setData({
                    showCloseIcon: true,
                });
            }
        },
        handleInput(e) {
            if (e.detail.value == &#39;&#39;) {
                this.setData({
                    showCloseIcon: false,
                });
            } else {
                this.setData({
                    showCloseIcon: true,
                });
                this.triggerEvent(&#39;handleInput&#39;, {
                    value: e.detail.value
                });
            }
        },
        handleSearch() { // 点击键盘上的回车,调用此方法
            this.setData({
                showCloseIcon: false,
            });
            console.log(&#39;handleSearch&#39;, this.data.inputValue);
        },

Search adds click events for closeIcon and search button respectively.

Add click events for closeIcon and search button respectively.

clearValue() {
            this.triggerEvent(&#39;handleInput&#39;, {
                value: &#39;&#39;
            });
            this.setData({
                showCloseIcon: false,
            });
        },
        onTap() {
            this.setData({
                showCloseIcon: false,
            });
            console.log(&#39;onTap&#39;, this.data.inputValue);
        },组件 json
{
  component:true
}

Page json

The name of the project is cookbook, and the component prefix here is uniformly ck.

{
    usingComponents:{
        ck-input:/components/search/index
    }
}

Page wxml

<input
    placeholder=&#39;搜你想吃的&#39;
    inputValue={{inputValue}}
    bind:handleInput=handleInput>
</input>

Page js

handleInput(e) {
        this.setData({
            inputValue: e.detail.value,
        });
    },

At this point, the initial development of the search component has been completed.

PHP Chinese website, a large number of jquery video tutorials, welcome to learn!

The above is the detailed content of How to develop a WeChat applet search component from scratch. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:zixun.jisuapp.cn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools