CouponCell.mjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import { defineComponent, createVNode as _createVNode } from "vue";
  2. import { isDef, truthProp, makeArrayProp, makeStringProp, createNamespace } from "../utils/index.mjs";
  3. import { Cell } from "../cell/index.mjs";
  4. const [name, bem, t] = createNamespace("coupon-cell");
  5. const couponCellProps = {
  6. title: String,
  7. border: truthProp,
  8. editable: truthProp,
  9. coupons: makeArrayProp(),
  10. currency: makeStringProp("\xA5"),
  11. chosenCoupon: {
  12. type: [Number, Array],
  13. default: -1
  14. }
  15. };
  16. const getValue = (coupon) => {
  17. const {
  18. value,
  19. denominations
  20. } = coupon;
  21. if (isDef(value)) {
  22. return value;
  23. }
  24. if (isDef(denominations)) {
  25. return denominations;
  26. }
  27. return 0;
  28. };
  29. function formatValue({
  30. coupons,
  31. chosenCoupon,
  32. currency
  33. }) {
  34. let value = 0;
  35. let isExist = false;
  36. (Array.isArray(chosenCoupon) ? chosenCoupon : [chosenCoupon]).forEach((i) => {
  37. const coupon = coupons[+i];
  38. if (coupon) {
  39. isExist = true;
  40. value += getValue(coupon);
  41. }
  42. });
  43. if (isExist) {
  44. return `-${currency} ${(value / 100).toFixed(2)}`;
  45. }
  46. return coupons.length === 0 ? t("noCoupon") : t("count", coupons.length);
  47. }
  48. var stdin_default = defineComponent({
  49. name,
  50. props: couponCellProps,
  51. setup(props) {
  52. return () => {
  53. const selected = Array.isArray(props.chosenCoupon) ? props.chosenCoupon.length : props.coupons[+props.chosenCoupon];
  54. return _createVNode(Cell, {
  55. "class": bem(),
  56. "value": formatValue(props),
  57. "title": props.title || t("title"),
  58. "border": props.border,
  59. "isLink": props.editable,
  60. "valueClass": bem("value", {
  61. selected
  62. })
  63. }, null);
  64. };
  65. }
  66. });
  67. export {
  68. couponCellProps,
  69. stdin_default as default
  70. };