微信小程序|开发实战篇之八-list列表组件及其子组件

本文介绍了一个基于微信小程序的List选择器组件与Tag标签组件的开发实战,详细解析了组件的WXML骨架文件与JS逻辑代码。涵盖组件属性配置、事件监听及自定义布局等方面。

前言

实战篇内容参考:
1、Lin Ui开源组件源码分析。https://doc.mini.talelin.com/
2、开发过程遇到问题。

1、list选择器组件

1.1 子组件—tag标签组件

在这里插入图片描述

1.1.1 tag的骨架文件wxml

<view style="display:flex">
  <view class="x-tag {
   
   {type==='touch'?'x-tag-touch':''}} {
   
   {plain?'x-tag-plain-'+ size:'x-tag-'+size}} {
   
   {disable?'x-tag-disable':''}} {
   
   {select?'select x-select-class':'x-class'}} {
   
   {plain?'x-tag-plain':''}} {
   
   {'x-tag-'+size+'-'+ shape}}" style="{
   
   {height?'line-height:'+height+'rpx;height:'+height+'rpx;':''}}{
   
   {plain?'color:'+fontColor+';border-color:'+fontColor:'background-color:'+bgColor+';color:'+fontColor}}" mut-bind:tap="handleTap">
    <view class="{
   
   {location==='left'?'content':'content-x'}}" style="{
   
   {iconName?'line-height:0':''}}">
      <x-icon wx:if="{
   
   {icon}}" style="{
   
   {location==='left'?'margin-right:5rpx':'margin-left:5rpx'}}" name="{
   
   {icon}}" size="{
   
   {iconSize}}" color="{
   
   {iconColor}}"/>
      <image style="{
   
   {location==='left'?'margin-right:5rpx':'margin-left:5rpx'}}" wx:if="{
   
   {image}}" src="{
   
   {image}}" class="{
   
   {'tag-image-'+size}} x-image-class"></image>
      <slot/>
    </view>
  </view>
</view>

分析:

  • 骨架文件主要是最外部一个view采用flex布局;
  • 然后view作为整个标签的容器。可以设置该标签的type类型;是否镂空plain属性;是否禁用disable;是否可以点击select,与disable的区别就是样式上还有参数名的不同;背景、字体的颜色、高度等;
  • 设置mut-bind:tap监听事件。表示互斥事件,与其他mut-bind:tap函数不会同时响应。具体参考:互斥事件响应。
  • 然后是一个view表示icon组件容器。设置icon的类型、位置、颜色。

1.1.2 tag的js文件

import validator from "../behaviors/validator";
Component({
   
   
  externalClasses: ["x-class", "x-select-class", "x-image-class"],
  behaviors: [validator],
  properties: {
   
   
    // 标签传递内容 string
    name: String,
    // 标签传递内容为 object
    cell: Object,
    // 标签类型touch有最小宽度,reading宽度由文本长度决定
    type: {
   
   
      type: String,
      value: "touch",
      options: ["reading", "touch"]
    },
    bgColor: String,
    fontColor: String,
    disable: Boolean,
    shape: {
   
   
      type: String,
      value: "square",
      options: ["square", "circle"]
    },
    // 标签是否点击
    select: Boolean,
    // 标签是否镂空
    plain: Boolean,
    size: {
   
   
      type: String,
      value: "medium",
      options: ["large", "medium", "mini", "super-mini"]
    },
    locat
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值