index.js 5.8 KB

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