CouponList.mjs 6.9 KB

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