CouponList.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. couponListProps: () => couponListProps,
  21. default: () => stdin_default
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_utils = require("../utils");
  26. var import_use_refs = require("../composables/use-refs");
  27. var import_tab = require("../tab");
  28. var import_tabs = require("../tabs");
  29. var import_empty = require("../empty");
  30. var import_field = require("../field");
  31. var import_button = require("../button");
  32. var import_coupon = require("../coupon");
  33. var import_use = require("@vant/use");
  34. const [name, bem, t] = (0, import_utils.createNamespace)("coupon-list");
  35. const couponListProps = {
  36. code: (0, import_utils.makeStringProp)(""),
  37. coupons: (0, import_utils.makeArrayProp)(),
  38. currency: (0, import_utils.makeStringProp)("\xA5"),
  39. showCount: import_utils.truthProp,
  40. emptyImage: String,
  41. enabledTitle: String,
  42. disabledTitle: String,
  43. disabledCoupons: (0, import_utils.makeArrayProp)(),
  44. showExchangeBar: import_utils.truthProp,
  45. showCloseButton: import_utils.truthProp,
  46. closeButtonText: String,
  47. inputPlaceholder: String,
  48. exchangeMinLength: (0, import_utils.makeNumberProp)(1),
  49. exchangeButtonText: String,
  50. displayedCouponIndex: (0, import_utils.makeNumberProp)(-1),
  51. exchangeButtonLoading: Boolean,
  52. exchangeButtonDisabled: Boolean,
  53. chosenCoupon: {
  54. type: [Number, Array],
  55. default: -1
  56. }
  57. };
  58. var stdin_default = (0, import_vue.defineComponent)({
  59. name,
  60. props: couponListProps,
  61. emits: ["change", "exchange", "update:code"],
  62. setup(props, {
  63. emit,
  64. slots
  65. }) {
  66. const [couponRefs, setCouponRefs] = (0, import_use_refs.useRefs)();
  67. const root = (0, import_vue.ref)();
  68. const barRef = (0, import_vue.ref)();
  69. const activeTab = (0, import_vue.ref)(0);
  70. const listHeight = (0, import_vue.ref)(0);
  71. const currentCode = (0, import_vue.ref)(props.code);
  72. const buttonDisabled = (0, import_vue.computed)(() => !props.exchangeButtonLoading && (props.exchangeButtonDisabled || !currentCode.value || currentCode.value.length < props.exchangeMinLength));
  73. const updateListHeight = () => {
  74. const TABS_HEIGHT = 44;
  75. const rootHeight = (0, import_use.useRect)(root).height;
  76. const headerHeight = (0, import_use.useRect)(barRef).height + TABS_HEIGHT;
  77. listHeight.value = (rootHeight > headerHeight ? rootHeight : import_utils.windowHeight.value) - headerHeight;
  78. };
  79. const onExchange = () => {
  80. emit("exchange", currentCode.value);
  81. if (!props.code) {
  82. currentCode.value = "";
  83. }
  84. };
  85. const scrollToCoupon = (index) => {
  86. (0, import_vue.nextTick)(() => {
  87. var _a;
  88. return (_a = couponRefs.value[index]) == null ? void 0 : _a.scrollIntoView();
  89. });
  90. };
  91. const renderEmpty = () => (0, import_vue.createVNode)(import_empty.Empty, {
  92. "image": props.emptyImage
  93. }, {
  94. default: () => [(0, import_vue.createVNode)("p", {
  95. "class": bem("empty-tip")
  96. }, [t("noCoupon")])]
  97. });
  98. const renderExchangeBar = () => {
  99. if (props.showExchangeBar) {
  100. return (0, import_vue.createVNode)("div", {
  101. "ref": barRef,
  102. "class": bem("exchange-bar")
  103. }, [(0, import_vue.createVNode)(import_field.Field, {
  104. "modelValue": currentCode.value,
  105. "onUpdate:modelValue": ($event) => currentCode.value = $event,
  106. "clearable": true,
  107. "border": false,
  108. "class": bem("field"),
  109. "placeholder": props.inputPlaceholder || t("placeholder"),
  110. "maxlength": "20"
  111. }, null), (0, import_vue.createVNode)(import_button.Button, {
  112. "plain": true,
  113. "type": "primary",
  114. "class": bem("exchange"),
  115. "text": props.exchangeButtonText || t("exchange"),
  116. "loading": props.exchangeButtonLoading,
  117. "disabled": buttonDisabled.value,
  118. "onClick": onExchange
  119. }, null)]);
  120. }
  121. };
  122. const renderCouponTab = () => {
  123. const {
  124. coupons,
  125. chosenCoupon
  126. } = props;
  127. const count = props.showCount ? ` (${coupons.length})` : "";
  128. const title = (props.enabledTitle || t("enable")) + count;
  129. const updateChosenCoupon = (currentValues = [], value = 0) => {
  130. if (currentValues.includes(value)) {
  131. return currentValues.filter((item) => item !== value);
  132. }
  133. return [...currentValues, value];
  134. };
  135. return (0, import_vue.createVNode)(import_tab.Tab, {
  136. "title": title
  137. }, {
  138. default: () => {
  139. var _a;
  140. return [(0, import_vue.createVNode)("div", {
  141. "class": bem("list", {
  142. "with-bottom": props.showCloseButton
  143. }),
  144. "style": {
  145. height: `${listHeight.value}px`
  146. }
  147. }, [coupons.map((coupon, index) => (0, import_vue.createVNode)(import_coupon.Coupon, {
  148. "key": coupon.id,
  149. "ref": setCouponRefs(index),
  150. "coupon": coupon,
  151. "chosen": Array.isArray(chosenCoupon) ? chosenCoupon.includes(index) : index === chosenCoupon,
  152. "currency": props.currency,
  153. "onClick": () => emit("change", Array.isArray(chosenCoupon) ? updateChosenCoupon(chosenCoupon, index) : index)
  154. }, null)), !coupons.length && renderEmpty(), (_a = slots["list-footer"]) == null ? void 0 : _a.call(slots)])];
  155. }
  156. });
  157. };
  158. const renderDisabledTab = () => {
  159. const {
  160. disabledCoupons
  161. } = props;
  162. const count = props.showCount ? ` (${disabledCoupons.length})` : "";
  163. const title = (props.disabledTitle || t("disabled")) + count;
  164. return (0, import_vue.createVNode)(import_tab.Tab, {
  165. "title": title
  166. }, {
  167. default: () => {
  168. var _a;
  169. return [(0, import_vue.createVNode)("div", {
  170. "class": bem("list", {
  171. "with-bottom": props.showCloseButton
  172. }),
  173. "style": {
  174. height: `${listHeight.value}px`
  175. }
  176. }, [disabledCoupons.map((coupon) => (0, import_vue.createVNode)(import_coupon.Coupon, {
  177. "disabled": true,
  178. "key": coupon.id,
  179. "coupon": coupon,
  180. "currency": props.currency
  181. }, null)), !disabledCoupons.length && renderEmpty(), (_a = slots["disabled-list-footer"]) == null ? void 0 : _a.call(slots)])];
  182. }
  183. });
  184. };
  185. (0, import_vue.watch)(() => props.code, (value) => {
  186. currentCode.value = value;
  187. });
  188. (0, import_vue.watch)(import_utils.windowHeight, updateListHeight);
  189. (0, import_vue.watch)(currentCode, (value) => emit("update:code", value));
  190. (0, import_vue.watch)(() => props.displayedCouponIndex, scrollToCoupon);
  191. (0, import_vue.onMounted)(() => {
  192. updateListHeight();
  193. scrollToCoupon(props.displayedCouponIndex);
  194. });
  195. return () => (0, import_vue.createVNode)("div", {
  196. "ref": root,
  197. "class": bem()
  198. }, [renderExchangeBar(), (0, import_vue.createVNode)(import_tabs.Tabs, {
  199. "active": activeTab.value,
  200. "onUpdate:active": ($event) => activeTab.value = $event,
  201. "class": bem("tab")
  202. }, {
  203. default: () => [renderCouponTab(), renderDisabledTab()]
  204. }), (0, import_vue.createVNode)("div", {
  205. "class": bem("bottom")
  206. }, [slots["list-button"] ? slots["list-button"]() : (0, import_vue.withDirectives)((0, import_vue.createVNode)(import_button.Button, {
  207. "round": true,
  208. "block": true,
  209. "type": "primary",
  210. "class": bem("close"),
  211. "text": props.closeButtonText || t("close"),
  212. "onClick": () => emit("change", Array.isArray(props.chosenCoupon) ? [] : -1)
  213. }, null), [[import_vue.vShow, props.showCloseButton]])])]);
  214. }
  215. });