mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
[improvement] CouponCell: functional (#2759)
This commit is contained in:
parent
f6ab03fa7e
commit
2ad525c45d
@ -1,63 +1,63 @@
|
||||
import { use } from '../utils';
|
||||
import { inherit } from '../utils/functional';
|
||||
import Cell from '../cell';
|
||||
|
||||
const [sfc, bem, t] = use('coupon-cell');
|
||||
|
||||
export default sfc({
|
||||
model: {
|
||||
prop: 'chosenCoupon'
|
||||
},
|
||||
function formatValue(props) {
|
||||
const { coupons, chosenCoupon, currency } = props;
|
||||
const coupon = coupons[chosenCoupon];
|
||||
|
||||
props: {
|
||||
title: String,
|
||||
coupons: Array,
|
||||
currency: {
|
||||
type: String,
|
||||
default: '¥'
|
||||
},
|
||||
border: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
editable: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
chosenCoupon: {
|
||||
type: Number,
|
||||
default: -1
|
||||
}
|
||||
},
|
||||
|
||||
computed: {
|
||||
value() {
|
||||
const { coupons } = this;
|
||||
const coupon = coupons[this.chosenCoupon];
|
||||
if (coupon) {
|
||||
const value = coupon.denominations || coupon.value;
|
||||
return `-${this.currency}${(value / 100).toFixed(2)}`;
|
||||
}
|
||||
return coupons.length === 0 ? t('tips') : t('count', coupons.length);
|
||||
},
|
||||
|
||||
valueClass() {
|
||||
return this.coupons[this.chosenCoupon] ? 'van-coupon-cell--selected' : '';
|
||||
}
|
||||
},
|
||||
|
||||
render(h) {
|
||||
return (
|
||||
<Cell
|
||||
class={bem()}
|
||||
title={this.title || t('title')}
|
||||
value={this.value}
|
||||
border={this.border}
|
||||
isLink={this.editable}
|
||||
valueClass={this.valueClass}
|
||||
onClick={() => {
|
||||
this.$emit('click');
|
||||
}}
|
||||
/>
|
||||
);
|
||||
if (coupon) {
|
||||
const value = coupon.denominations || coupon.value;
|
||||
return `-${currency}${(value / 100).toFixed(2)}`;
|
||||
}
|
||||
});
|
||||
|
||||
return coupons.length === 0 ? t('tips') : t('count', coupons.length);
|
||||
}
|
||||
|
||||
function CouponCell(h, props, slots, ctx) {
|
||||
const valueClass = props[props.chosenCoupon]
|
||||
? 'van-coupon-cell--selected'
|
||||
: '';
|
||||
const value = formatValue(props);
|
||||
|
||||
return (
|
||||
<Cell
|
||||
class={bem()}
|
||||
value={value}
|
||||
title={props.title || t('title')}
|
||||
border={props.border}
|
||||
isLink={props.editable}
|
||||
valueClass={valueClass}
|
||||
{...inherit(ctx, true)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
CouponCell.model = {
|
||||
prop: 'chosenCoupon'
|
||||
};
|
||||
|
||||
CouponCell.props = {
|
||||
title: String,
|
||||
coupons: Array,
|
||||
currency: {
|
||||
type: String,
|
||||
default: '¥'
|
||||
},
|
||||
border: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
editable: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
chosenCoupon: {
|
||||
type: Number,
|
||||
default: -1
|
||||
}
|
||||
};
|
||||
|
||||
export default sfc(CouponCell);
|
||||
|
@ -1,6 +1,6 @@
|
||||
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||
|
||||
exports[`coupon cell 1`] = `
|
||||
exports[`render coupon cell 1`] = `
|
||||
<div class="van-cell van-cell--clickable van-coupon-cell">
|
||||
<div class="van-cell__title"><span>优惠券</span></div>
|
||||
<div class="van-cell__value"><span>使用优惠</span></div><i class="van-icon van-icon-arrow van-cell__right-icon">
|
||||
@ -8,10 +8,10 @@ exports[`coupon cell 1`] = `
|
||||
</div>
|
||||
`;
|
||||
|
||||
exports[`coupon cell 2`] = `
|
||||
exports[`render coupon cell with coupon 1`] = `
|
||||
<div class="van-cell van-cell--clickable van-coupon-cell">
|
||||
<div class="van-cell__title"><span>优惠券</span></div>
|
||||
<div class="van-cell__value van-coupon-cell--selected"><span>-¥1.00</span></div><i class="van-icon van-icon-arrow van-cell__right-icon">
|
||||
<div class="van-cell__value"><span>-¥1.00</span></div><i class="van-icon van-icon-arrow van-cell__right-icon">
|
||||
<!----></i>
|
||||
</div>
|
||||
`;
|
||||
|
@ -109,21 +109,27 @@ test('exchange coupon', () => {
|
||||
expect(wrapper.emitted('input')[2][0]).toBe('2');
|
||||
});
|
||||
|
||||
test('coupon cell', () => {
|
||||
test('render coupon cell', () => {
|
||||
const onClick = jest.fn();
|
||||
const wrapper = mount(CouponCell, {
|
||||
listeners: {
|
||||
click: onClick
|
||||
context: {
|
||||
on: {
|
||||
click: onClick
|
||||
}
|
||||
}
|
||||
});
|
||||
expect(wrapper).toMatchSnapshot();
|
||||
|
||||
wrapper.setProps({
|
||||
coupons: [{ value: 100 }],
|
||||
chosenCoupon: 0
|
||||
});
|
||||
expect(wrapper).toMatchSnapshot();
|
||||
|
||||
wrapper.trigger('click');
|
||||
expect(onClick.mock.calls.length).toEqual(1);
|
||||
});
|
||||
|
||||
test('render coupon cell with coupon', () => {
|
||||
const wrapper = mount(CouponCell, {
|
||||
propsData: {
|
||||
coupons: [{ value: 100 }],
|
||||
chosenCoupon: 0
|
||||
}
|
||||
});
|
||||
expect(wrapper).toMatchSnapshot();
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user