Skip to content

Commit

Permalink
fix(search): 预览效果优化. (#2432)
Browse files Browse the repository at this point in the history
  • Loading branch information
byq1213 committed Jun 21, 2024
1 parent f3938ef commit 72543bd
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 38 deletions.
15 changes: 9 additions & 6 deletions src/search/_example/base/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ Component({
methods: {
onChangeValue(e) {
const { value } = e.detail;

const list = [
'tdesign-vue',
'tdesign-react',
'tdesign-miniprogram',
'tdesign-angular',
'tdesign-mobile-vue',
'tdesign-mobile-react',
];
this.setData({
resultList: value
? Array.from({ length: 3 }).map((v, i) => {
return `预览结果:${i}`;
})
: [],
resultList: value ? list.filter((v) => v.includes(value)) : [],
});
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/search/search.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"component": true,
"usingComponents": {
"t-icon": "../icon/icon"
"t-icon": "../icon/icon",
"t-cell": "../cell/cell"
}
}
18 changes: 2 additions & 16 deletions src/search/search.less
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,8 @@
}
}
&__result-item {
padding: 30rpx;
position: relative;
display: flex;
align-items: center;
font-size: 34rpx;
color: rgba(0, 0, 0, 0.55);
position: relative;
&:not(:last-child)::after {
content: '';
position: absolute;
left: 0;
bottom: 0;
right: 0;
height: 1rpx;
z-index: 2;
background-color: rgba(0, 0, 0, 0.1);
&--hightlight {
color: @brand-color;
}
}
}
47 changes: 37 additions & 10 deletions src/search/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,49 @@ export default class Search extends SuperComponent {

properties = props;

observers = {};
observers = {
resultList(val) {
const { isSelected } = this.data;
if (val.length) {
if (isSelected) {
// 已选择
this.setData({
isShowResultList: false,
isSelected: false,
});
} else {
this.setData({
isShowResultList: true,
});
}
} else {
this.setData({
isShowResultList: false,
});
}
},
};

data = {
classPrefix: name,
prefix,
isShowResultList: false,
isSelected: false,
};

onInput(e) {
let { value } = e.detail;
const { maxcharacter } = this.properties;

if (maxcharacter && typeof maxcharacter === 'number' && maxcharacter > 0) {
const { characters } = getCharacterLength('maxcharacter', value, maxcharacter);

value = characters;
}

this.setData({ value });
this.setData({
value,
});

this.triggerEvent('change', { value });
}

Expand Down Expand Up @@ -70,14 +95,16 @@ export default class Search extends SuperComponent {
onActionClick() {
this.triggerEvent('action-click');
}
selectResultItem(e) {
const { index } = e.currentTarget.dataset
const item = this.properties.resultList[index]

onSelectResultItem(e) {
const { index } = e.currentTarget.dataset;
const item = this.properties.resultList[index];
this.setData({
value: item
})
value: item,
isSelected: true,
});

this.triggerEvent('change', { value: item });
this.triggerEvent('selectresult', { index, item })
this.triggerEvent('selectresult', { index, item });
}
}
15 changes: 10 additions & 5 deletions src/search/search.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,17 @@
</view>
<slot name="action" />
</view>
<view class="{{classPrefix}}__result-list" aria-role="listbox">
<view
<wxs src="./search.wxs" module="this"></wxs>
<view wx:if="{{isShowResultList && !isSelected}}" class="{{classPrefix}}__result-list" aria-role="listbox">
<t-cell
wx:for="{{resultList}}"
wx:key="{{index}}"
wx:key="index"
data-index="{{index}}"
class="{{classPrefix}}__result-item"
bind:tap="selectResultItem"
aria-role="option">{{item}}</view>
hover
bind:tap="onSelectResultItem"
aria-role="option"
>
<rich-text slot="title" nodes="{{this.hightLight(item, value)}}"></rich-text>
</t-cell>
</view>
6 changes: 6 additions & 0 deletions src/search/search.wxs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
var hightLight = function (label, keyword) {
return label.replace(keyword, '<span class="t-search__result-item--hightlight">' + keyword + '</span>');
};
module.exports = {
hightLight: hightLight,
};
4 changes: 4 additions & 0 deletions src/search/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* */

export interface TdSearchProps {
/**
* 预览结果列表
* @default []
*/
resultList: {
type: ArrayConstructor;
value?: [];
Expand Down

0 comments on commit 72543bd

Please sign in to comment.