mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[new feature] Sku: set selectedNum when inited (#3045)
This commit is contained in:
parent
3e1b048d88
commit
24764de878
@ -9,7 +9,7 @@ import SkuRowItem from './components/SkuRowItem';
|
||||
import SkuStepper from './components/SkuStepper';
|
||||
import SkuMessages from './components/SkuMessages';
|
||||
import SkuActions from './components/SkuActions';
|
||||
import { use } from '../utils';
|
||||
import { use, isDef } from '../utils';
|
||||
import { isAllSelected, isSkuChoosable, getSkuComb, getSelectedSkuValues } from './utils/skuHelper';
|
||||
import { LIMIT_TYPE, UNSELECTED_SKU_VALUE_ID } from './constants';
|
||||
|
||||
@ -29,10 +29,11 @@ export default sfc({
|
||||
hideQuotaText: Boolean,
|
||||
stepperTitle: String,
|
||||
getContainer: Function,
|
||||
customSkuValidator: Function,
|
||||
closeOnClickOverlay: Boolean,
|
||||
disableStepperInput: Boolean,
|
||||
resetStepperOnHide: Boolean,
|
||||
resetSelectedSkuOnHide: Boolean,
|
||||
disableStepperInput: Boolean,
|
||||
closeOnClickOverlay: Boolean,
|
||||
initialSku: {
|
||||
type: Object,
|
||||
default: () => ({})
|
||||
@ -61,7 +62,6 @@ export default sfc({
|
||||
type: Object,
|
||||
default: () => ({})
|
||||
},
|
||||
customSkuValidator: Function
|
||||
},
|
||||
|
||||
data() {
|
||||
@ -85,7 +85,7 @@ export default sfc({
|
||||
});
|
||||
|
||||
if (this.resetStepperOnHide) {
|
||||
this.$refs.skuStepper && this.$refs.skuStepper.setCurrentNum(1);
|
||||
this.resetStepper();
|
||||
}
|
||||
|
||||
if (this.resetSelectedSkuOnHide) {
|
||||
@ -93,9 +93,11 @@ export default sfc({
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
value(val) {
|
||||
this.show = val;
|
||||
},
|
||||
|
||||
skuTree(val) {
|
||||
this.resetSelectedSku(val);
|
||||
}
|
||||
@ -196,19 +198,35 @@ export default sfc({
|
||||
skuEventBus.$on('sku:addCart', this.onAddCart);
|
||||
skuEventBus.$on('sku:buy', this.onBuy);
|
||||
|
||||
this.resetStepper();
|
||||
this.resetSelectedSku(this.skuTree);
|
||||
|
||||
// 组件初始化后的钩子,抛出skuEventBus
|
||||
this.$emit('after-sku-create', skuEventBus);
|
||||
},
|
||||
|
||||
methods: {
|
||||
resetStepper() {
|
||||
const { skuStepper } = this.$refs;
|
||||
const { selectedNum } = this.initialSku;
|
||||
const num = isDef(selectedNum) ? selectedNum : 1;
|
||||
|
||||
if (skuStepper) {
|
||||
skuStepper.setCurrentNum(num);
|
||||
} else {
|
||||
this.selectedNum = num;
|
||||
}
|
||||
},
|
||||
|
||||
resetSelectedSku(skuTree) {
|
||||
this.selectedSku = {};
|
||||
// 重置selectedSku
|
||||
|
||||
// 重置 selectedSku
|
||||
skuTree.forEach(item => {
|
||||
this.selectedSku[item.k_s] = this.initialSku[item.k_s] || UNSELECTED_SKU_VALUE_ID;
|
||||
});
|
||||
// 只有一个sku规格值时默认选中
|
||||
|
||||
// 只有一个 sku 规格值时默认选中
|
||||
skuTree.forEach(item => {
|
||||
const key = item.k_s;
|
||||
const valueId = item.v[0].id;
|
||||
|
@ -176,7 +176,8 @@ export default {
|
||||
closeOnClickOverlay: true,
|
||||
initialSku: {
|
||||
s1: '30349',
|
||||
s2: '1193'
|
||||
s2: '1193',
|
||||
selectedNum: 3
|
||||
},
|
||||
customSkuValidator: () => '请选择xxx',
|
||||
customStepperConfig: {
|
||||
|
@ -191,7 +191,8 @@ sku: {
|
||||
// Key:skuKeyStr
|
||||
// Value:skuValueId
|
||||
s1: '30349',
|
||||
s2: '1193'
|
||||
s2: '1193',
|
||||
selectedNum: 3
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -200,7 +200,9 @@ sku: {
|
||||
// 键:skuKeyStr(sku 组合列表中当前类目对应的 key 值)
|
||||
// 值:skuValueId(规格值 id)
|
||||
s1: '30349',
|
||||
s2: '1193'
|
||||
s2: '1193',
|
||||
// 初始选中数量
|
||||
selectedNum: 3
|
||||
}
|
||||
```
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user