Skip to content

Commit f207876

Browse files
feat: ✨ Swiper 轮播组件增加value-key用于自定义目标字段属性名 (#485)
Closes: #410
1 parent 197d61a commit f207876

4 files changed

Lines changed: 47 additions & 8 deletions

File tree

docs/component/swiper.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,27 @@ function onChange(e) {
186186
}
187187
```
188188

189+
## 指定valueKey <el-tag text style="vertical-align: middle;margin-left:8px;" effect="plain">$LOWEST_VERSION$</el-tag>
190+
191+
通过`value-key` 属性指定 `list` 中每个对象图片地址字段,默认为 `value`
192+
193+
194+
```html
195+
<wd-swiper value-key="url" :list="customSwiperList" autoplay v-model:current="current" @click="handleClick" @change="onChange"></wd-swiper>
196+
```
197+
```ts
198+
const current = ref<number>(0)
199+
200+
const customSwiperList = ref([
201+
{ url: 'https://registry.npmmirror.com/wot-design-uni-assets/*/files/redpanda.jpg' },
202+
{ url: 'https://registry.npmmirror.com/wot-design-uni-assets/*/files/capybara.jpg' },
203+
{ url: 'https://registry.npmmirror.com/wot-design-uni-assets/*/files/panda.jpg' },
204+
{ url: 'https://registry.npmmirror.com/wot-design-uni-assets/*/files/moon.jpg' }
205+
])
206+
```
207+
208+
209+
189210
## 属性控制切换
190211

191212
```html
@@ -228,7 +249,9 @@ const isLoop = ref(false)
228249
| snapToEdge | 边距是否应用到第一个、最后一个元素 | `boolean` | - | false | 0.1.22 |
229250
| indicator | 指示器全部配置 | `SwiperIndicatorProps \| boolean` | - | true | 0.1.22 |
230251
| imageMode | 图片裁剪、缩放的模式 | `string` | 参考官方文档[mode](https://uniapp.dcloud.net.cn/component/image.html#mode-%E6%9C%89%E6%95%88%E5%80%BC) | `aspectFill` | 0.1.55 |
231-
| customStyle | 外部自定义样式 | `string` | - | '' | 0.1.22 |
252+
| customStyle | 外部自定义样式 | `string` | - | '' | 0.1.22 |
253+
| value-key | 选项对象中,value 对应的 key | `string` | - | `value` | $LOWEST_VERSION$ |
254+
232255

233256
### DirectionType
234257

@@ -263,7 +286,7 @@ const isLoop = ref(false)
263286

264287
| 事件名称 | 说明 | 参数 | 最低版本 |
265288
| -------- | ---------------- | ----------------------------------------------------------- | -------- |
266-
| click | 点击轮播项时触发 | `(index: number)` | 0.1.22 |
289+
| click | 点击轮播项时触发 | `(index: number, item: SwiperList \| string)` | 0.1.22 |
267290
| change | 轮播切换时触发 | `(current: number, source: 'autoplay' \| 'touch' \| 'nav') ` | 0.1.22 |
268291

269292
## Slot

src/pages/swiper/Index.vue

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@
110110
</wd-swiper>
111111
</demo-block>
112112

113+
<demo-block title="指定valueKey">
114+
<wd-swiper value-key="url" :list="customSwiperList" autoplay v-model:current="current9" @click="handleClick" @change="onChange"></wd-swiper>
115+
</demo-block>
116+
113117
<demo-block title="属性控制切换">
114118
<wd-swiper :loop="isLoop" :autoplay="false" :list="swiperList" v-model:current="current8" />
115119
<wd-gap />
@@ -138,6 +142,13 @@ const swiperList = ref([
138142
'https://registry.npmmirror.com/wot-design-uni-assets/*/files/meng.jpg'
139143
])
140144
145+
const customSwiperList = ref([
146+
{ url: 'https://registry.npmmirror.com/wot-design-uni-assets/*/files/redpanda.jpg' },
147+
{ url: 'https://registry.npmmirror.com/wot-design-uni-assets/*/files/capybara.jpg' },
148+
{ url: 'https://registry.npmmirror.com/wot-design-uni-assets/*/files/panda.jpg' },
149+
{ url: 'https://registry.npmmirror.com/wot-design-uni-assets/*/files/moon.jpg' }
150+
])
151+
141152
const current = ref<number>(0)
142153
const current1 = ref<number>(1)
143154
const current2 = ref<number>(2)
@@ -147,6 +158,7 @@ const current5 = ref<number>(0)
147158
const current6 = ref<number>(0)
148159
const current7 = ref<number>(0)
149160
const current8 = ref<number>(0)
161+
const current9 = ref<number>(0)
150162
151163
const isLoop = ref(false)
152164

src/uni_modules/wot-design-uni/components/wd-swiper/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type IndicatorPositionType = 'left' | 'top-left' | 'top' | 'top-right' |
2020

2121
export interface SwiperList {
2222
[key: string]: any
23-
value: string
23+
value?: string
2424
}
2525

2626
export const swiperProps = {
@@ -143,7 +143,10 @@ export const swiperProps = {
143143
* 默认值:'aspectFill'
144144
*/
145145
imageMode: makeStringProp<ImageMode>('aspectFill'),
146-
146+
/**
147+
* 选项对象中,value 对应的 key
148+
*/
149+
valueKey: makeStringProp('value'),
147150
/**
148151
* 自定义指示器类名
149152
* 类型:string

src/uni_modules/wot-design-uni/components/wd-swiper/wd-swiper.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
@change="handleChange"
1818
@animationfinish="handleAnimationfinish"
1919
>
20-
<swiper-item v-for="(item, index) in list" :key="index" class="wd-swiper__item" @click="handleClick(index)">
20+
<swiper-item v-for="(item, index) in list" :key="index" class="wd-swiper__item" @click="handleClick(index, item)">
2121
<image
22-
:src="isObj(item) ? item.value : item"
22+
:src="isObj(item) ? item[valueKey] : item"
2323
:class="`wd-swiper__image ${customImageClass} ${getCustomImageClass(navCurrent, index, list)}`"
2424
:style="{ height: addUnit(height) }"
2525
:mode="imageMode"
@@ -161,9 +161,10 @@ function handleAnimationfinish(e: { detail: { current: any; source: string } })
161161
/**
162162
* 点击滑块事件
163163
* @param index 点击的滑块下标
164+
* @param item 点击的滑块内容
164165
*/
165-
function handleClick(index: number) {
166-
emit('click', { index })
166+
function handleClick(index: number, item: string | SwiperList) {
167+
emit('click', { index, item })
167168
}
168169
169170
function handleIndicatorChange(e: { dir: any; source: string }) {

0 commit comments

Comments
 (0)