微信小程序自定义组件实践

2019-01-04  本文已影响18人  小毛同学

目的

从小程序基础库版本1.6.3开始,小程序支持了简洁的组件化编程。通过编写一个自定义组件来学习小程序自定义组件的用法,这里我准备写一个如下的搜索框组件。


搜索框组件

流程

1.新建组件相关文件

在项目根目录下新建如下文件

/component/
    searchbar/
        index.wxml
        index.js
        index.wxss
        index.json

2.编写组件相关文件

<!--index.wxml-->
<view class="my-search-bar">
    <view class="my-search-bar__form">
        <view class="my-search-bar__box" bindtap="showInput">
            <icon class="my-icon-search_in-box" type="search" size="14" color='{{searchIconColor}}'></icon>
            <input type="text" class="my-search-bar__input" placeholder="{{placeholder}}" confirm-type="search" value="{{searchValue}}" focus="{{inputFocused}}" bindinput="inputTyping" bindconfirm="getSearchValue"/>
            <view class="my-icon-clear" wx:if="{{searchValue.length > 0}}" bindtap="clearInput">
                <icon type="clear" size="14" color='{{clearIconColor}}'></icon>
            </view>
        </view>
        <label class="my-search-bar__label" hidden="{{inputFocused}}" bindtap="showInput">
            <icon class="my-icon-search" type="search" size="14"></icon>
            <view class="my-search-bar__text">{{title}}</view>
        </label>
    </view>
    <view class="my-search-bar__cancel-btn" hidden="{{!inputFocused}}" bindtap="hideInput">
        取消
    </view>
</view>
//index.js 
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        title: {
            type: String,
            value: "标题"
        },
        placeholder: {
            type: String,
            value: "搜索"
        },
        searchIconColor: {
            type: String,
            value: ""
        },
        clearIconColor: {
            type: String,
            value: ""
        }
    },
   /**
     * 组件的初始数据
     */
    data: {
        inputFocused: false,
        searchValue: ""
    },
    /**
     * 组件的方法列表
     */
    methods: {
        showInput() {
            this.setData({
                inputFocused: true
            });
        },
        hideInput() {
            this.setData({
                inputFocused: false
            });
        },
        clearInput() {
            this.setData({
                searchValue: ""
            });
        },
        inputTyping(e) {
            this.setData({
                searchValue: e.detail.value
            });
        },
        getSearchValue(e) {
            const eventDetails = {
                value: this.data.searchValue
            };
            this.triggerEvent("search", eventDetails)
        }
    }
})
/*index.wxss*/
.my-search-bar {
    position: relative;
    padding: 8px 10px;
    display: -webkit-box;
    display: -webkit-flex;
    display: flex;
    box-sizing: border-box;
    background-color: #efeff4;
    border-top: 1rpx solid #d7d6dc;
    border-bottom: 1rpx solid #d7d6dc;
}

.my-icon-search {
    margin-right: 8px;
    font-size: inherit;
    vertical-align: middle;
}

.my-icon-search_in-box {
    position: absolute;
    left: 10px;
    top: 7px;
}

.my-search-bar__text {
    display: inline-block;
    font-size: 14px;
    vertical-align: middle;
}

.my-search-bar__form {
    position: relative;
    -webkit-box-flex: 1;
    -webkit-flex: auto;
    flex: auto;
    border-radius: 5px;
    background: #fff;
    border: 1rpx solid #e6e6ea;
}

.my-search-bar__box {
    position: relative;
    padding-left: 30px;
    padding-right: 30px;
    width: 100%;
    box-sizing: border-box;
    z-index: 1;
}

.my-search-bar__input {
    height: 28px;
    line-height: 28px;
    font-size: 14px;
}

.my-icon-clear {
    position: absolute;
    top: 0;
    right: 0;
    padding: 7px 8px;
    font-size: 0;
}

.my-search-bar__label {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 2;
    border-radius: 3px;
    text-align: center;
    color: #9b9b9b;
    background: #fff;
    line-height: 28px;
}

.my-search-bar__cancel-btn {
    margin-left: 10px;
    line-height: 28px;
    color: #09bb07;
    white-space: nowrap;
}
{
    "component": true,
    "usingComponents": {}
}

3.使用组件

{
    "usingComponents": {
        "my-searchbar": "/component/searchbar/index"
    }
}
<my-searchbar title="标题" placeholder="输入搜索内容" bind:search="onSearch" ></my-searchbar>
Page({
    onSearch: function(e) {
        console.log("search:", e.detail.value)
    },
})
搜索框
上一篇 下一篇

猜你喜欢

热点阅读