Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions docs/index-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,32 @@

索引列表组件,可实现类似通讯录效果。组件内节点将被添加到列表上方。

列表支持多选。如果将```selecting```属性置为```true```,列表就会进入可选择状态,。如果将```selecting```属性置为```false```,列表就会退出可选择状态,同时发送```bindselect```事件。

## 属性列表

| 属性 | 类型 | 默认值 | 必填 | 说明 |
| ---------- | --------------- | ------ | ---- | ------------------------------------- |
| list | Array<listItem> | [] | 是 | 列表数据 |
| vibrated | boolean | true | 否 | 索引上滑动时是否产生振动,仅 iOS 生效 |
| bindchoose | eventhandle | | 否 | 选择列表项, e.detail={name} |
| 属性 | 类型 | 默认值 | 必填 | 说明 |
| ---------- | ---------------- | ------ | ---- | --------------------------------------------------- |
| list | Array\<listItem> | [] | 是 | 列表数据 |
| vibrated | boolean | true | 否 | 索引上滑动时是否产生振动,仅 iOS 生效 |
| selecting | boolean | false | 否 | 列表是否为可选择状态 |
| bindchoose | eventhandle | | 否 | 单击列表项, e.detail={item:subItem} |
| bindselect | eventhandle | | 否 | 列表退出选择状态, e.detail={selected:Array\<subItem>} |

### listItem 属性列表
### listItem 对象的属性列表

| 属性 | 类型 | 说明 |
| -------- | -------------- | ------------ |
| alpha | string | 首字母(大写) |
| subItems | Array<subItem> | 子元素集合 |
| 属性 | 类型 | 说明 |
| -------- | --------------- | ------------ |
| alpha | string | 首字母(大写) |
| subItems | Array\<subItem> | 子元素集合 |

### subItem 属性列表
### subItem 对象的属性列表

| 属性 | 类型 | 说明 |
| ---- | ------ | ---- |
| name | string | 名称 |
| 属性 | 类型 | 必填 | 说明 |
| -------- | ------- | -- | ------------ |
| name | string | 是 | 名称 |
| selected | boolean | 否 | 是否已经被选择 |
| disabled | boolean | 否 | 是否可以被选择 |

### 注意事项

Expand Down
9 changes: 9 additions & 0 deletions src/index-list/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
.index-group__item.thin-border-bottom:after{
left: 24rpx;
}
.selected{
color: #1aad19;
}
.disabled{
color: rgba(0, 0, 0, 0.5);
}
.bg-highlight,.selected{
background-color: #f2f2f2;
}

.anchor-bar__wrp{
position: fixed;
Expand Down
39 changes: 35 additions & 4 deletions src/index-list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Component({
observer: function(newVal) {
if (newVal.length === 0) return
const data: any = this.data
const alphabet = data.list.map(item => item.alpha)
const alphabet:Array<any> = data.list.map(item => item.alpha)
this.setData({
alphabet,
current: alphabet[0]
Expand All @@ -61,6 +61,17 @@ Component({
vibrated: {
type: Boolean,
value: true
},
selecting: {
type: Boolean,
value: false,
observer: function (newVal:Boolean) {
if (newVal){
this.data.selected = [];
}else{
this.triggerEvent('select',{selected:this.data.selected})
}
}
}

},
Expand All @@ -70,6 +81,7 @@ Component({
intoView: '',
touching: false,
alphabet: [],
selected: [],
_tops: [],
_anchorItemH: 0,
_anchorItemW: 0,
Expand All @@ -88,9 +100,19 @@ Component({
}
},
methods: {
choose(e) {
const item = e.target.dataset.item
this.triggerEvent('choose', {item})
choose(item:{item:subItem,selected:boolean}) {
//let item = e.target.dataset.item
let selected:Array<subItem> = this.data.selected
const curItem = selected.findIndex((v)=> v.name==item.item.name)

if(item.selected){
selected.splice(curItem,1);
}else{
if(curItem<0){
selected.push(item.item);
}
}
this.triggerEvent('choose', {item:item.item})
},
scrollTo(e) {
this.__scrollTo(e)
Expand Down Expand Up @@ -158,3 +180,12 @@ Component({
}
}
})

interface subItem{
name:string,
style:"selected"|"disabled"|""
}
interface listItem{
alpha:string,
subItems:Array<subItem>
}
7 changes: 5 additions & 2 deletions src/index-list/index.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@
<view>
<slot></slot>
</view>
<wxs module="index" src="./index.wxs"></wxs>
<view class="index_list_item" wx:for="{{list}}" wx:key="alpha" id="{{item.alpha}}">
<view class="index-group__title font-size-26 tips-color">{{item.alpha}}</view>
<view class="index-group__content">
<view class="index-group__list">
<block wx:for="{{item.subItems}}" wx:for-item="subItem" wx:key="name">
<view
class="index-group__item thin-border-bottom"
class="index-group__item thin-border-bottom {{subItem.style}}"
hover-class="bg-highlight"
data-selecting="{{selecting}}"
change:data-selecting="{{index.init}}"
data-item="{{subItem}}"
bindtap="choose">
bindtap="{{index.toggle}}">
{{subItem.name}}
</view>
</block>
Expand Down
34 changes: 34 additions & 0 deletions src/index-list/index.wxs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports.toggle =function (event,instance) {
//instance.getDataset().selecting&&
var dataset= event.target.dataset
var item = dataset.item
var curInst = event.instance
if(!item.disabled&&dataset.selecting){
if(curInst.hasClass('selected')){
curInst.removeClass('selected')
instance.callMethod('choose',{
item:item,
selected:true
});
}else{
curInst.addClass('selected')
instance.callMethod('choose',{
item:item,
selected:false
});
}
}
}

module.exports.init = function (newValue,oldValue,hostInst,inst) {
var item = inst.getDataset().item
if(item.disabled){
inst.addClass('disabled')
}else{
if(newValue&&item.selected){
inst.addClass('selected')
}else{
inst.removeClass('selected')
}
}
}
4 changes: 3 additions & 1 deletion tools/demo/example/index-list/index-list.wxml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@

<mp-indexList class="city__list" list="{{list}}" bindchoose="onChoose">
<mp-indexList class="city__list" list="{{list}}" selecting="{{select}}" bindchoose="onChoose" bindselect="onSelect">
<view class="page">
<view class="page__hd">
<view class="page__title">Index List</view>
<view class="page__desc">类通讯录列表</view>
</view>
<view class="page__bd">
可选择状态:
<switch type="switch" bindtap="onTap" checked="{{select}}"></switch>
</view>
</view>
</mp-indexList>
2 changes: 0 additions & 2 deletions tools/demo/project.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"ignore": []
},
"setting": {
"urlCheck": true,
"es6": true,
"postcss": true,
"minified": true,
Expand All @@ -13,7 +12,6 @@
},
"compileType": "miniprogram",
"libVersion": "2.9.4",
"appid": "wxe0b5580bba739b69",
"projectname": "小程序拓展组件库",
"isGameTourist": false,
"condition": {
Expand Down