SkuHeader.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import _mergeJSXProps from "@vue/babel-helper-vue-jsx-merge-props";
  2. import _extends from "@babel/runtime/helpers/esm/extends";
  3. // Utils
  4. import { createNamespace } from '../../utils';
  5. import { inherit } from '../../utils/functional';
  6. import { BORDER_BOTTOM } from '../../utils/constant'; // Components
  7. import Image from '../../image'; // Types
  8. var _createNamespace = createNamespace('sku-header'),
  9. createComponent = _createNamespace[0],
  10. bem = _createNamespace[1];
  11. function getSkuImgValue(sku, selectedSku) {
  12. var imgValue;
  13. sku.tree.some(function (item) {
  14. var id = selectedSku[item.k_s];
  15. if (id && item.v) {
  16. var matchedSku = item.v.filter(function (skuValue) {
  17. return skuValue.id === id;
  18. })[0] || {};
  19. var img = matchedSku.previewImgUrl || matchedSku.imgUrl || matchedSku.img_url;
  20. if (img) {
  21. imgValue = _extends({}, matchedSku, {
  22. ks: item.k_s,
  23. imgUrl: img
  24. });
  25. return true;
  26. }
  27. }
  28. return false;
  29. });
  30. return imgValue;
  31. }
  32. function SkuHeader(h, props, slots, ctx) {
  33. var _slots$skuHeaderIma;
  34. var sku = props.sku,
  35. goods = props.goods,
  36. skuEventBus = props.skuEventBus,
  37. selectedSku = props.selectedSku,
  38. _props$showHeaderImag = props.showHeaderImage,
  39. showHeaderImage = _props$showHeaderImag === void 0 ? true : _props$showHeaderImag;
  40. var selectedValue = getSkuImgValue(sku, selectedSku);
  41. var imgUrl = selectedValue ? selectedValue.imgUrl : goods.picture;
  42. var previewImage = function previewImage() {
  43. skuEventBus.$emit('sku:previewImage', selectedValue);
  44. };
  45. return h("div", _mergeJSXProps([{
  46. "class": [bem(), BORDER_BOTTOM]
  47. }, inherit(ctx)]), [showHeaderImage && h(Image, {
  48. "attrs": {
  49. "fit": "cover",
  50. "src": imgUrl
  51. },
  52. "class": bem('img-wrap'),
  53. "on": {
  54. "click": previewImage
  55. }
  56. }, [(_slots$skuHeaderIma = slots['sku-header-image-extra']) == null ? void 0 : _slots$skuHeaderIma.call(slots)]), h("div", {
  57. "class": bem('goods-info')
  58. }, [slots.default == null ? void 0 : slots.default()])]);
  59. }
  60. SkuHeader.props = {
  61. sku: Object,
  62. goods: Object,
  63. skuEventBus: Object,
  64. selectedSku: Object,
  65. showHeaderImage: Boolean
  66. };
  67. export default createComponent(SkuHeader);