Skip to content

Commit

Permalink
fix(Sku): add open-preview event data (#6664)
Browse files Browse the repository at this point in the history
* fix(Sku): open-preview event data

* fix(Sku): delete findIndex

* fix(Sku): change name

Co-authored-by: songweite <songweite@youzan.com>
  • Loading branch information
waiter and songweite committed Jul 2, 2020
1 parent 2ad40a7 commit 4db4c93
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
17 changes: 15 additions & 2 deletions src/sku/Sku.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,22 @@ export default createComponent({
this.selectedNum = num;
},

onPreviewImage(indexImage) {
const index = this.imageList.findIndex((image) => image === indexImage);
onPreviewImage(selectedValue) {
const { imageList } = this;
let index = 0;
let indexImage = imageList[0];
if (selectedValue && selectedValue.imgUrl) {
this.imageList.some((image, pos) => {
if (image === selectedValue.imgUrl) {
index = pos;
return true;
}
return false;
});
indexImage = selectedValue.imgUrl;
}
const params = {
...selectedValue,
index,
imageList: this.imageList,
indexImage,
Expand Down
31 changes: 23 additions & 8 deletions src/sku/components/SkuHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,42 @@ export type SkuHeaderSlots = DefaultSlots & {
'sku-header-image-extra'?: ScopedSlot;
};

type SelectedValueType = {
ks: string;
imgUrl: string;
};

const [createComponent, bem] = createNamespace('sku-header');

function getSkuImg(
function getSkuImgValue(
sku: SkuData,
selectedSku: SelectedSkuData
): string | undefined {
let img;
): SelectedValueType | undefined {
let imgValue;

sku.tree.some((item) => {
const id = selectedSku[item.k_s];

if (id && item.v) {
const matchedSku =
item.v.filter((skuValue) => skuValue.id === id)[0] || {};
img = matchedSku.previewImgUrl || matchedSku.imgUrl || matchedSku.img_url;
return img;

const img =
matchedSku.previewImgUrl || matchedSku.imgUrl || matchedSku.img_url;
if (img) {
imgValue = {
...matchedSku,
ks: item.k_s,
imgUrl: img,
};
return true;
}
}

return false;
});

return img;
return imgValue;
}

function SkuHeader(
Expand All @@ -61,10 +75,11 @@ function SkuHeader(
showHeaderImage = true,
} = props;

const imgUrl = getSkuImg(sku, selectedSku) || goods.picture;
const selectedValue = getSkuImgValue(sku, selectedSku);
const imgUrl = selectedValue ? selectedValue.imgUrl : goods.picture;

const previewImage = () => {
skuEventBus.$emit('sku:previewImage', imgUrl);
skuEventBus.$emit('sku:previewImage', selectedValue);
};

return (
Expand Down
7 changes: 6 additions & 1 deletion src/sku/components/SkuRowItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ export default createComponent({

onPreviewImg(event) {
event.stopPropagation();
this.skuEventBus.$emit('sku:previewImage', this.imgUrl);
const { skuValue, skuKeyStr } = this;
this.skuEventBus.$emit('sku:previewImage', {
...skuValue,
ks: skuKeyStr,
imgUrl: skuValue.imgUrl || skuValue.img_url,
});
},

genImage(classPrefix) {
Expand Down

0 comments on commit 4db4c93

Please sign in to comment.