Skip to content

Commit 8a124e4

Browse files
Jcannojinzhiqiang
andauthored
feat(Sku): support sku properties (#13454)
Co-authored-by: jinzhiqiang <jinzhiqiang@youzan.com>
1 parent e2ce4fc commit 8a124e4

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/sku/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,16 @@ sku: {
282282
];
283283
```
284284

285+
### skuProperties Data Structure
286+
287+
```js
288+
[
289+
sku_id: 239883,
290+
// sku properties, data structure is same as properties
291+
properties: Properties
292+
];
293+
```
294+
285295
### initialSku Data Structure
286296

287297
```js

src/sku/README.zh-CN.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,16 @@ sku: {
275275
];
276276
```
277277

278+
### skuProperties 对象结构
279+
280+
```js
281+
[
282+
sku_id: 239883,
283+
// 规格属性, 数据结构和properties相同
284+
properties: Properties
285+
];
286+
```
287+
278288
### initialSku 对象结构
279289

280290
```js

src/sku/Sku.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export default createComponent({
3636
lazyLoad: Boolean,
3737
hideStock: Boolean,
3838
properties: Array,
39+
skuProperties: Array,
3940
addCartText: String,
4041
stepperTitle: String,
4142
getContainer: [String, Function],
@@ -118,6 +119,7 @@ export default createComponent({
118119
selectedProp: {},
119120
selectedNum: 1,
120121
show: this.value,
122+
currentSkuProperties: []
121123
};
122124
},
123125

@@ -155,6 +157,9 @@ export default createComponent({
155157
},
156158

157159
computed: {
160+
isSkuProperties() {
161+
return this.skuProperties && this.skuProperties.length;
162+
},
158163
skuGroupClass() {
159164
return [
160165
'van-sku-group-container',
@@ -202,7 +207,7 @@ export default createComponent({
202207

203208
selectedSkuComb() {
204209
let skuComb = null;
205-
if (this.isSkuCombSelected) {
210+
if (this.isSkuCombSelected || this.isSkuProperties) {
206211
if (this.hasSku) {
207212
skuComb = getSkuComb(this.skuList, this.selectedSku);
208213
} else {
@@ -214,6 +219,8 @@ export default createComponent({
214219
}
215220

216221
if (skuComb) {
222+
// 更新当前规格属性数据
223+
this.setCurrentSkuProperties(skuComb.id);
217224
skuComb.properties = getSelectedProperties(
218225
this.propList,
219226
this.selectedProp
@@ -266,7 +273,7 @@ export default createComponent({
266273
},
267274

268275
propList() {
269-
return this.properties || [];
276+
return this.isSkuProperties ? this.currentSkuProperties : this.properties || [];
270277
},
271278

272279
imageList() {
@@ -362,6 +369,10 @@ export default createComponent({
362369
},
363370

364371
methods: {
372+
setCurrentSkuProperties(id) {
373+
const target = this.skuProperties?.find((item) => item.sku_id === id) || {};
374+
this.currentSkuProperties = target.properties || [];
375+
},
365376
resetStepper() {
366377
const { skuStepper } = this.$refs;
367378
const { selectedNum } = this.initialSku;
@@ -503,13 +514,20 @@ export default createComponent({
503514
}
504515
: { ...this.selectedSku, [skuValue.skuKeyStr]: skuValue.id };
505516

517+
// 切换sku清空当前选择属性数据,触发prop-clear
518+
if (this.isSkuProperties) {
519+
this.selectedProp = {}
520+
this.onPropClear()
521+
}
506522
this.$emit('sku-selected', {
507523
skuValue,
508524
selectedSku: this.selectedSku,
509525
selectedSkuComb: this.selectedSkuComb,
510526
});
511527
},
512-
528+
onPropClear() {
529+
this.$emit('sku-prop-clear');
530+
},
513531
onPropSelect(propValue) {
514532
const arr = this.selectedProp[propValue.skuKeyStr] || [];
515533
const pos = arr.indexOf(propValue.id);

0 commit comments

Comments
 (0)