From 880f574b9aef3b4967a5f1a06ad2e19fa26b914d Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 15 Feb 2019 21:00:46 +0800 Subject: [PATCH] [improvement] Sku: partial functional (#2756) --- packages/sku/components/SkuActions.js | 51 ++++++------- packages/sku/components/SkuHeader.js | 100 +++++++++++++------------- packages/sku/components/SkuRow.js | 27 +++---- 3 files changed, 88 insertions(+), 90 deletions(-) diff --git a/packages/sku/components/SkuActions.js b/packages/sku/components/SkuActions.js index 6b329bb7e..3a4be09ee 100644 --- a/packages/sku/components/SkuActions.js +++ b/packages/sku/components/SkuActions.js @@ -1,32 +1,33 @@ import { use } from '../../utils'; +import { inherit } from '../../utils/functional'; import Button from '../../button'; const [sfc, bem] = use('sku-actions'); -export default sfc({ - props: { - buyText: String, - skuEventBus: Object, - showAddCartBtn: Boolean - }, +function SkuActions(h, props, slots, ctx) { + const emit = name => () => { + props.skuEventBus.$emit(name); + }; - render(h) { - const emit = name => () => { - this.skuEventBus.$emit(name); - }; + return ( +
+ {props.showAddCartBtn && ( +
+ ); +} - return ( -
- {this.showAddCartBtn && ( -
- ); - } -}); +SkuActions.props = { + buyText: String, + skuEventBus: Object, + showAddCartBtn: Boolean +}; + +export default sfc(SkuActions); diff --git a/packages/sku/components/SkuHeader.js b/packages/sku/components/SkuHeader.js index 76566f75e..771ed39a7 100644 --- a/packages/sku/components/SkuHeader.js +++ b/packages/sku/components/SkuHeader.js @@ -1,61 +1,57 @@ import { use } from '../../utils'; +import { inherit } from '../../utils/functional'; import Icon from '../../icon'; const [sfc, bem] = use('sku-header'); -export default sfc({ - props: { - sku: Object, - goods: Object, - skuEventBus: Object, - selectedSku: Object - }, - - computed: { - goodsImg() { - const s1Id = this.selectedSku.s1; - const skuImg = this.getSkuImg(s1Id); - // 优先使用选中 sku 的图片 - return skuImg || this.goods.picture; - } - }, - - methods: { - getSkuImg(id) { - if (!id) return; - - // skuImg 挂载在 skuTree 中 s1 上 - const treeItem = this.sku.tree.filter(item => item.k_s === 's1')[0] || {}; - - if (!treeItem.v) return; +function getSkuImg(sku, selectedSku) { + const id = selectedSku.s1; + if (id) { + // skuImg 挂载在 skuTree 中 s1 上 + const treeItem = sku.tree.filter(item => item.k_s === 's1')[0] || {}; + if (treeItem.v) { const matchedSku = treeItem.v.filter(skuValue => skuValue.id === id)[0]; - if (matchedSku) return matchedSku.imgUrl || matchedSku.img_url; - }, - - previewImage() { - this.skuEventBus.$emit('sku:previewImage', this.goodsImg); + if (matchedSku) { + return matchedSku.imgUrl || matchedSku.img_url; + } } - }, - - render(h) { - return ( -
-
- -
-
-
{this.goods.title}
- {this.slots()} - { - this.skuEventBus.$emit('sku:close'); - }} - /> -
-
- ); } -}); +} + +function SkuHeader(h, props, slots, ctx) { + const { sku, goods, skuEventBus, selectedSku } = props; + const goodsImg = getSkuImg(sku, selectedSku) || goods.picture; + + const previewImage = () => { + skuEventBus.$emit('sku:previewImage', goodsImg); + }; + + return ( +
+
+ +
+
+
{goods.title}
+ {slots.default && slots.default()} + { + skuEventBus.$emit('sku:close'); + }} + /> +
+
+ ); +} + +SkuHeader.props = { + sku: Object, + goods: Object, + skuEventBus: Object, + selectedSku: Object +}; + +export default sfc(SkuHeader); diff --git a/packages/sku/components/SkuRow.js b/packages/sku/components/SkuRow.js index ec38fc7fa..0d0f6edb5 100644 --- a/packages/sku/components/SkuRow.js +++ b/packages/sku/components/SkuRow.js @@ -1,18 +1,19 @@ import { use } from '../../utils'; +import { inherit } from '../../utils/functional'; const [sfc, bem] = use('sku-row'); -export default sfc({ - props: { - skuRow: Object - }, +function SkuRow(h, props, slots, ctx) { + return ( +
+
{props.skuRow.k}:
+ {slots.default && slots.default()} +
+ ); +} - render(h) { - return ( -
-
{this.skuRow.k}:
- {this.slots()} -
- ); - } -}); +SkuRow.props = { + skuRow: Object +}; + +export default sfc(SkuRow);