index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. exports.__esModule = true;
  4. exports["default"] = void 0;
  5. var _utils = require("../utils");
  6. var _tab = _interopRequireDefault(require("../tab"));
  7. var _tabs = _interopRequireDefault(require("../tabs"));
  8. var _field = _interopRequireDefault(require("../field"));
  9. var _button = _interopRequireDefault(require("../button"));
  10. var _coupon = _interopRequireDefault(require("../coupon"));
  11. var _use = (0, _utils.use)('coupon-list'),
  12. sfc = _use[0],
  13. bem = _use[1],
  14. t = _use[2];
  15. var EMPTY_IMAGE = 'https://img.yzcdn.cn/vant/coupon-empty.png';
  16. var _default = sfc({
  17. model: {
  18. prop: 'code'
  19. },
  20. props: {
  21. code: String,
  22. coupons: Array,
  23. disabledCoupons: Array,
  24. closeButtonText: String,
  25. inputPlaceholder: String,
  26. exchangeButtonText: String,
  27. exchangeButtonLoading: Boolean,
  28. exchangeButtonDisabled: Boolean,
  29. exchangeMinLength: {
  30. type: Number,
  31. "default": 1
  32. },
  33. chosenCoupon: {
  34. type: Number,
  35. "default": -1
  36. },
  37. displayedCouponIndex: {
  38. type: Number,
  39. "default": -1
  40. },
  41. showExchangeBar: {
  42. type: Boolean,
  43. "default": true
  44. },
  45. showCloseButton: {
  46. type: Boolean,
  47. "default": true
  48. },
  49. currency: {
  50. type: String,
  51. "default": '¥'
  52. }
  53. },
  54. data: function data() {
  55. return {
  56. tab: 0,
  57. winHeight: window.innerHeight,
  58. currentCode: this.code || ''
  59. };
  60. },
  61. computed: {
  62. buttonDisabled: function buttonDisabled() {
  63. return !this.exchangeButtonLoading && (this.exchangeButtonDisabled || !this.currentCode || this.currentCode.length < this.exchangeMinLength);
  64. },
  65. title: function title() {
  66. return t('enable') + " (" + this.coupons.length + ")";
  67. },
  68. disabledTitle: function disabledTitle() {
  69. return t('disabled') + " (" + this.disabledCoupons.length + ")";
  70. },
  71. listStyle: function listStyle() {
  72. return {
  73. height: this.winHeight - (this.showExchangeBar ? 140 : 94) + 'px'
  74. };
  75. }
  76. },
  77. watch: {
  78. code: function code(_code) {
  79. this.currentCode = _code;
  80. },
  81. currentCode: function currentCode(code) {
  82. this.$emit('input', code);
  83. },
  84. displayedCouponIndex: function displayedCouponIndex(val) {
  85. this.scrollToShowCoupon(val);
  86. }
  87. },
  88. mounted: function mounted() {
  89. this.scrollToShowCoupon(this.displayedCouponIndex);
  90. },
  91. methods: {
  92. onClickExchangeButton: function onClickExchangeButton() {
  93. this.$emit('exchange', this.currentCode); // auto clear currentCode when not use vModel
  94. if (!this.code) {
  95. this.currentCode = '';
  96. }
  97. },
  98. // scroll to show specific coupon
  99. scrollToShowCoupon: function scrollToShowCoupon(index) {
  100. var _this = this;
  101. if (index === -1) {
  102. return;
  103. }
  104. this.$nextTick(function () {
  105. var _this$$refs = _this.$refs,
  106. card = _this$$refs.card,
  107. list = _this$$refs.list;
  108. /* istanbul ignore next */
  109. if (list && card && card[index]) {
  110. list.scrollTop = card[index].$el.offsetTop - 100;
  111. }
  112. });
  113. },
  114. renderEmpty: function renderEmpty() {
  115. var h = this.$createElement;
  116. return h("div", {
  117. "class": bem('empty')
  118. }, [h("img", {
  119. "attrs": {
  120. "src": EMPTY_IMAGE
  121. }
  122. }), h("p", [t('empty')])]);
  123. },
  124. renderExchangeButton: function renderExchangeButton() {
  125. var h = this.$createElement;
  126. return h(_button["default"], {
  127. "attrs": {
  128. "size": "small",
  129. "type": "danger",
  130. "text": this.exchangeButtonText || t('exchange'),
  131. "loading": this.exchangeButtonLoading,
  132. "disabled": this.buttonDisabled
  133. },
  134. "class": bem('exchange'),
  135. "on": {
  136. "click": this.onClickExchangeButton
  137. }
  138. });
  139. }
  140. },
  141. render: function render(h) {
  142. var _this2 = this;
  143. var ExchangeBar = this.showExchangeBar && h(_field["default"], {
  144. "attrs": {
  145. "clearable": true,
  146. "border": false,
  147. "placeholder": this.inputPlaceholder || t('placeholder'),
  148. "maxlength": "20"
  149. },
  150. "class": bem('field'),
  151. "scopedSlots": {
  152. button: this.renderExchangeButton
  153. },
  154. "model": {
  155. value: _this2.currentCode,
  156. callback: function callback($$v) {
  157. _this2.currentCode = $$v;
  158. }
  159. }
  160. });
  161. var onChange = function onChange(index) {
  162. return function () {
  163. return _this2.$emit('change', index);
  164. };
  165. };
  166. var CouponTab = h(_tab["default"], {
  167. "attrs": {
  168. "title": this.title
  169. }
  170. }, [h("div", {
  171. "class": bem('list'),
  172. "style": this.listStyle
  173. }, [this.coupons.map(function (coupon, index) {
  174. return h(_coupon["default"], {
  175. "ref": "card",
  176. "key": coupon.id,
  177. "attrs": {
  178. "coupon": coupon,
  179. "currency": _this2.currency,
  180. "chosen": index === _this2.chosenCoupon
  181. },
  182. "nativeOn": {
  183. "click": onChange(index)
  184. }
  185. });
  186. }), !this.coupons.length && this.renderEmpty()])]);
  187. var DisabledCouponTab = h(_tab["default"], {
  188. "attrs": {
  189. "title": this.disabledTitle
  190. }
  191. }, [h("div", {
  192. "class": bem('list'),
  193. "style": this.listStyle
  194. }, [this.disabledCoupons.map(function (coupon) {
  195. return h(_coupon["default"], {
  196. "attrs": {
  197. "disabled": true,
  198. "coupon": coupon,
  199. "currency": _this2.currency
  200. },
  201. "key": coupon.id
  202. });
  203. }), !this.disabledCoupons.length && this.renderEmpty()])]);
  204. return h("div", {
  205. "class": bem()
  206. }, [ExchangeBar, h(_tabs["default"], {
  207. "class": bem('tab'),
  208. "attrs": {
  209. "line-width": 120
  210. },
  211. "model": {
  212. value: _this2.tab,
  213. callback: function callback($$v) {
  214. _this2.tab = $$v;
  215. }
  216. }
  217. }, [CouponTab, DisabledCouponTab]), h(_button["default"], {
  218. "directives": [{
  219. name: "show",
  220. value: this.showCloseButton
  221. }],
  222. "attrs": {
  223. "size": "large",
  224. "text": this.closeButtonText || t('close')
  225. },
  226. "class": bem('close'),
  227. "on": {
  228. "click": onChange(-1)
  229. }
  230. })]);
  231. }
  232. });
  233. exports["default"] = _default;