From d8f1918fe2cf076cb8fe261cb9aae305377a0886 Mon Sep 17 00:00:00 2001 From: niuniu1448870058 <384665340@qq.com> Date: Sun, 18 Apr 2021 20:39:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(sku):=20=E5=95=86=E5=93=81=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E9=BB=98=E8=AE=A4=E9=80=89=E4=B8=AD=E4=BA=A4=E4=BA=92?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9=20(#8559)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(sku): 属性默认选中逻辑修改 * feat(sku): 属性默认选中逻辑修改 Co-authored-by: liu Co-authored-by: neverland --- src/sku/Sku.js | 20 +++++++++++++++----- src/sku/demo/data.ts | 17 +++++++++++++++++ src/utils/index.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 5 deletions(-) diff --git a/src/sku/Sku.js b/src/sku/Sku.js index 69ee927c5..320a86893 100644 --- a/src/sku/Sku.js +++ b/src/sku/Sku.js @@ -10,7 +10,7 @@ import SkuRowPropItem from './components/SkuRowPropItem'; import SkuStepper from './components/SkuStepper'; import SkuMessages from './components/SkuMessages'; import SkuActions from './components/SkuActions'; -import { createNamespace } from '../utils'; +import { createNamespace, isEmpty } from '../utils'; import { isAllSelected, isSkuChoosable, @@ -413,14 +413,24 @@ export default createComponent({ // 重置商品属性 this.selectedProp = {}; const { selectedProp = {} } = this.initialSku; - // 只有一个属性值时,默认选中,且选中外部传入信息 + // 选中外部传入信息 this.propList.forEach((item) => { - if (item.v && item.v.length === 1) { - this.selectedProp[item.k_id] = [item.v[0].id]; - } else if (selectedProp[item.k_id]) { + if (selectedProp[item.k_id]) { this.selectedProp[item.k_id] = selectedProp[item.k_id]; } }); + if (isEmpty(this.selectedProp)) { + this.propList.forEach((item) => { + // 没有加价的属性,默认选中第一个 + if (item?.v?.length > 0) { + const { v, k_id } = item; + const isHasConfigPrice = v.some((i) => +i.price !== 0); + if (!isHasConfigPrice) { + this.selectedProp[k_id] = [v[0].id]; + } + } + }); + } const propValues = this.selectedPropValues; if (propValues.length > 0) { diff --git a/src/sku/demo/data.ts b/src/sku/demo/data.ts index 27ca6a139..d54b2a403 100644 --- a/src/sku/demo/data.ts +++ b/src/sku/demo/data.ts @@ -197,6 +197,23 @@ export function getSkuData(largeImageMode = false) { }, ], }, + { + k: '未加价的属性项', + k_id: 126, + is_multiple: true, + v: [ + { + id: 1244, + name: '属性a', + price: 0, + }, + { + id: 1245, + name: '属性b', + price: 0, + }, + ], + }, ], }; } diff --git a/src/utils/index.ts b/src/utils/index.ts index 3e6e9e187..70001f3ed 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -35,3 +35,45 @@ export function get(object: any, path: string): any { return result; } + +/** + * Checks if `value` is an empty object, collection, map, or set. + * + * Objects are considered empty if they have no own enumerable string keyed + * properties. + * + * Array-like values such as `arguments` objects, arrays, buffers, strings, or + * jQuery-like collections are considered empty if they have a `length` of `0`. + * Similarly, maps and sets are considered empty if they have a `size` of `0`. + * + * @function isEmpty + * @param {*} value The value to check. + * @returns {boolean} Returns `true` if `value` is empty, else `false`. + * @example + * + * _.isEmpty(null); + * // => true + * + * _.isEmpty(true); + * // => true + * + * _.isEmpty(1); + * // => true + * + * _.isEmpty([1, 2, 3]); + * // => false + * + * _.isEmpty({ 'a': 1 }); + * // => false + */ +export function isEmpty(value: any): boolean { + if (value == null) { + return true; + } + + if (typeof value !== 'object') { + return true; + } + + return Object.keys(value).length === 0; +}