CouponList.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. default: () => stdin_default
  21. });
  22. module.exports = __toCommonJS(stdin_exports);
  23. var import_vue = require("vue");
  24. var import_vue2 = 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. chosenCoupon: (0, import_utils.makeNumberProp)(-1),
  42. enabledTitle: String,
  43. disabledTitle: String,
  44. disabledCoupons: (0, import_utils.makeArrayProp)(),
  45. showExchangeBar: import_utils.truthProp,
  46. showCloseButton: import_utils.truthProp,
  47. closeButtonText: String,
  48. inputPlaceholder: String,
  49. exchangeMinLength: (0, import_utils.makeNumberProp)(1),
  50. exchangeButtonText: String,
  51. displayedCouponIndex: (0, import_utils.makeNumberProp)(-1),
  52. exchangeButtonLoading: Boolean,
  53. exchangeButtonDisabled: Boolean
  54. };
  55. var stdin_default = (0, import_vue2.defineComponent)({
  56. name,
  57. props: couponListProps,
  58. emits: ["change", "exchange", "update:code"],
  59. setup(props, {
  60. emit,
  61. slots
  62. }) {
  63. const [couponRefs, setCouponRefs] = (0, import_use_refs.useRefs)();
  64. const root = (0, import_vue2.ref)();
  65. const barRef = (0, import_vue2.ref)();
  66. const activeTab = (0, import_vue2.ref)(0);
  67. const listHeight = (0, import_vue2.ref)(0);
  68. const currentCode = (0, import_vue2.ref)(props.code);
  69. const buttonDisabled = (0, import_vue2.computed)(() => !props.exchangeButtonLoading && (props.exchangeButtonDisabled || !currentCode.value || currentCode.value.length < props.exchangeMinLength));
  70. const updateListHeight = () => {
  71. const TABS_HEIGHT = 44;
  72. const rootHeight = (0, import_use.useRect)(root).height;
  73. const headerHeight = (0, import_use.useRect)(barRef).height + TABS_HEIGHT;
  74. listHeight.value = (rootHeight > headerHeight ? rootHeight : import_utils.windowHeight.value) - headerHeight;
  75. };
  76. const onExchange = () => {
  77. emit("exchange", currentCode.value);
  78. if (!props.code) {
  79. currentCode.value = "";
  80. }
  81. };
  82. const scrollToCoupon = (index) => {
  83. (0, import_vue2.nextTick)(() => {
  84. var _a;
  85. return (_a = couponRefs.value[index]) == null ? void 0 : _a.scrollIntoView();
  86. });
  87. };
  88. const renderEmpty = () => (0, import_vue.createVNode)(import_empty.Empty, {
  89. "image": props.emptyImage
  90. }, {
  91. default: () => [(0, import_vue.createVNode)("p", {
  92. "class": bem("empty-tip")
  93. }, [t("noCoupon")])]
  94. });
  95. const renderExchangeBar = () => {
  96. if (props.showExchangeBar) {
  97. return (0, import_vue.createVNode)("div", {
  98. "ref": barRef,
  99. "class": bem("exchange-bar")
  100. }, [(0, import_vue.createVNode)(import_field.Field, {
  101. "modelValue": currentCode.value,
  102. "onUpdate:modelValue": ($event) => currentCode.value = $event,
  103. "clearable": true,
  104. "border": false,
  105. "class": bem("field"),
  106. "placeholder": props.inputPlaceholder || t("placeholder"),
  107. "maxlength": "20"
  108. }, null), (0, import_vue.createVNode)(import_button.Button, {
  109. "plain": true,
  110. "type": "danger",
  111. "class": bem("exchange"),
  112. "text": props.exchangeButtonText || t("exchange"),
  113. "loading": props.exchangeButtonLoading,
  114. "disabled": buttonDisabled.value,
  115. "onClick": onExchange
  116. }, null)]);
  117. }
  118. };
  119. const renderCouponTab = () => {
  120. const {
  121. coupons
  122. } = props;
  123. const count = props.showCount ? ` (${coupons.length})` : "";
  124. const title = (props.enabledTitle || t("enable")) + count;
  125. return (0, import_vue.createVNode)(import_tab.Tab, {
  126. "title": title
  127. }, {
  128. default: () => {
  129. var _a;
  130. return [(0, import_vue.createVNode)("div", {
  131. "class": bem("list", {
  132. "with-bottom": props.showCloseButton
  133. }),
  134. "style": {
  135. height: `${listHeight.value}px`
  136. }
  137. }, [coupons.map((coupon, index) => (0, import_vue.createVNode)(import_coupon.Coupon, {
  138. "key": coupon.id,
  139. "ref": setCouponRefs(index),
  140. "coupon": coupon,
  141. "chosen": index === props.chosenCoupon,
  142. "currency": props.currency,
  143. "onClick": () => emit("change", index)
  144. }, null)), !coupons.length && renderEmpty(), (_a = slots["list-footer"]) == null ? void 0 : _a.call(slots)])];
  145. }
  146. });
  147. };
  148. const renderDisabledTab = () => {
  149. const {
  150. disabledCoupons
  151. } = props;
  152. const count = props.showCount ? ` (${disabledCoupons.length})` : "";
  153. const title = (props.disabledTitle || t("disabled")) + count;
  154. return (0, import_vue.createVNode)(import_tab.Tab, {
  155. "title": title
  156. }, {
  157. default: () => {
  158. var _a;
  159. return [(0, import_vue.createVNode)("div", {
  160. "class": bem("list", {
  161. "with-bottom": props.showCloseButton
  162. }),
  163. "style": {
  164. height: `${listHeight.value}px`
  165. }
  166. }, [disabledCoupons.map((coupon) => (0, import_vue.createVNode)(import_coupon.Coupon, {
  167. "disabled": true,
  168. "key": coupon.id,
  169. "coupon": coupon,
  170. "currency": props.currency
  171. }, null)), !disabledCoupons.length && renderEmpty(), (_a = slots["disabled-list-footer"]) == null ? void 0 : _a.call(slots)])];
  172. }
  173. });
  174. };
  175. (0, import_vue2.watch)(() => props.code, (value) => {
  176. currentCode.value = value;
  177. });
  178. (0, import_vue2.watch)(import_utils.windowHeight, updateListHeight);
  179. (0, import_vue2.watch)(currentCode, (value) => emit("update:code", value));
  180. (0, import_vue2.watch)(() => props.displayedCouponIndex, scrollToCoupon);
  181. (0, import_vue2.onMounted)(() => {
  182. updateListHeight();
  183. scrollToCoupon(props.displayedCouponIndex);
  184. });
  185. return () => (0, import_vue.createVNode)("div", {
  186. "ref": root,
  187. "class": bem()
  188. }, [renderExchangeBar(), (0, import_vue.createVNode)(import_tabs.Tabs, {
  189. "active": activeTab.value,
  190. "onUpdate:active": ($event) => activeTab.value = $event,
  191. "class": bem("tab")
  192. }, {
  193. default: () => [renderCouponTab(), renderDisabledTab()]
  194. }), (0, import_vue.createVNode)("div", {
  195. "class": bem("bottom")
  196. }, [(0, import_vue.withDirectives)((0, import_vue.createVNode)(import_button.Button, {
  197. "round": true,
  198. "block": true,
  199. "type": "danger",
  200. "class": bem("close"),
  201. "text": props.closeButtonText || t("close"),
  202. "onClick": () => emit("change", -1)
  203. }, null), [[import_vue.vShow, props.showCloseButton]])])]);
  204. }
  205. });