小程序封装可清空的input组件

小程序学习之input组件封装

<view class="v-input v-input-class">
  <view class="v-input-label {{required?'v-input-required':''}}">{{label}}</view>
  <view class="v-input-value_container {{border?'v-input-value_container_border':''}}">
<input class="v-input-value " placeholder="{{placeholder}}" placeholder-style="{{placehoderStyle}}" placeholder-class="{{placeholderClass}}" maxlength="{{maxlength}}" focus="{{focus}}" confirm-type="{{confirmType}}" confirm-hold="{{confirmHold}}" disabled="{{disabled}}" password="{{password}}" type="{{type}}" value="{{value}}" bindinput="handleInputChange" bindfocus="handleInputFocus" bindblur="handleInputBlur" bindconfirm="handleInputConfirm" />
  <icon class="v-input-icon" wx:if="{{clearable}}" bindtap="handleClearTap" type="clear" size="18" />
  </view>  
</view>

js文件如下

Component({
  behaviors: ['wx://form-field'],
  externalClasses:['v-input-class'],
    /**
     * 组件的属性列表
     */
    properties: {
        label: String,
        password: {
            type: Boolean,
            value: false
        },
        disabled: {
            type: Boolean,
            value: false
        },
        placeholder: {
            type: String,
            value: '请输入'
        },
        placeholderStyle: String,
        placeholderClass: String,
        maxlength: {
            type: Number,
            value: 140
        },
        focus: {
            type: Boolean,
            value: false
        },
        confirmType: {
            type: String,
            value: 'done'
        },
        confirmHold: {
            type: Boolean,
            value: false
        },
        type: {
            type: String,
            value: 'text'
        },
        value: String,
        clearable: {
            type: Boolean,
            value: false
        },
        required: {
            type: Boolean,
            value: false,
        },
        message: {
            type: String,
            value: '请输入完整内容'
        },
        border: {
            type: Boolean,
            value: false
        }
    },

    /**
     * 组件的初始数据
     */
    data: {

    },

    /**
     * 组件的方法列表
     */
    methods: {
        handleInputChange(e) {
            let detail = {
                value: e.detail.value
            }
            this.setData({
                value: detail.value
            });

            this.triggerEvent('input', detail);
        },
        handleInputFocus(e) {
            this.triggerEvent('focus', e.detail);
        },
        handleInputBlur(e) {
            let detail = {
                value: e.detail.value
            }
            if (this.data.required) {
                if (this.checkValueRequired(detail.value)) {
                    this.setData({
                        value: detail.value
                    });
                }
            }

            this.triggerEvent('blur', detail);
        },
        handleInputConfirm(e) {
            let detail = {
                value: e.detail.value
            }
            this.setData({
                value: detail.value
            });
            this.triggerEvent('confirm', detail);
        },
        handleClearTap() {
            let detail = {
                value: ''
            }
            this.setData({
                value: ''
            })
            this.triggerEvent('clear', detail);
        },

        checkValueRequired() {
            if (!!this.data.value && this.data.value !== "") {
                return true
            } else {
                this._showToast(this.data.message)
                return false
            }
        },

        _showToast(message, icon = 'none') {
            wx.showToast({
                title: message,
                icon: icon
            })
        }
    }
})

加上behavior是因为在form表单中 提交表单时获取不到input,开发文档有。

在json文件中引入


  "usingComponents": {
    "v-input": "/components/input/input"
  }

就可以在wxml中使用

<v-input label="输入框" required clearable/>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值