index.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { use } from '../utils';
  2. import { inherit } from '../utils/functional';
  3. import Cell from '../cell';
  4. // Types
  5. import { CreateElement, RenderContext } from 'vue/types';
  6. import { DefaultSlots } from '../utils/use/sfc';
  7. import { Coupon } from '../coupon/shared';
  8. export type CouponCellProps = {
  9. title?: string;
  10. border: boolean;
  11. coupons: Coupon[];
  12. currency: string;
  13. editable: boolean;
  14. chosenCoupon: number;
  15. };
  16. const [sfc, bem, t] = use('coupon-cell');
  17. function formatValue(props: CouponCellProps) {
  18. const { coupons, chosenCoupon, currency } = props;
  19. const coupon = coupons[chosenCoupon];
  20. if (coupon) {
  21. const value = coupon.denominations || coupon.value;
  22. return `-${currency}${(value / 100).toFixed(2)}`;
  23. }
  24. return coupons.length === 0 ? t('tips') : t('count', coupons.length);
  25. }
  26. function CouponCell(
  27. h: CreateElement,
  28. props: CouponCellProps,
  29. slots: DefaultSlots,
  30. ctx: RenderContext<CouponCellProps>
  31. ) {
  32. const valueClass = props.coupons[props.chosenCoupon]
  33. ? 'van-coupon-cell--selected'
  34. : '';
  35. const value = formatValue(props);
  36. return (
  37. <Cell
  38. class={bem()}
  39. value={value}
  40. title={props.title || t('title')}
  41. border={props.border}
  42. isLink={props.editable}
  43. valueClass={valueClass}
  44. {...inherit(ctx, true)}
  45. />
  46. );
  47. }
  48. CouponCell.model = {
  49. prop: 'chosenCoupon'
  50. };
  51. CouponCell.props = {
  52. title: String,
  53. coupons: Array,
  54. currency: {
  55. type: String,
  56. default: '¥'
  57. },
  58. border: {
  59. type: Boolean,
  60. default: true
  61. },
  62. editable: {
  63. type: Boolean,
  64. default: true
  65. },
  66. chosenCoupon: {
  67. type: Number,
  68. default: -1
  69. }
  70. };
  71. export default sfc<CouponCellProps>(CouponCell);