uni-list-item.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <cell>
  4. <!-- #endif -->
  5. <view
  6. :class="{ 'uni-list-item--disabled': disabled }"
  7. :hover-class="(!clickable && !link) || disabled || showSwitch ? '' : 'uni-list-item--hover'"
  8. class="uni-list-item"
  9. @click.stop="onClick"
  10. >
  11. <view v-if="!isFirstChild" class="border--left" :class="{ 'uni-list--border': border }"></view>
  12. <view class="uni-list-item__container" :class="{ 'container--right': showArrow || link, 'flex--direction': direction === 'column' }">
  13. <slot name="header">
  14. <view class="uni-list-item__header">
  15. <view v-if="thumb" class="uni-list-item__icon"><image :src="thumb" class="uni-list-item__icon-img" :class="['uni-list--' + thumbSize]" /></view>
  16. <view v-else-if="showExtraIcon" class="uni-list-item__icon"><uni-icons :color="extraIcon.color" :size="extraIcon.size" :type="extraIcon.type" /></view>
  17. </view>
  18. </slot>
  19. <slot name="body">
  20. <view class="uni-list-item__content" :class="{ 'uni-list-item__content--center': thumb || showExtraIcon || showBadge || showSwitch }">
  21. <text v-if="title" class="uni-list-item__content-title" :class="[ellipsis !== 0 && ellipsis <= 2 ? 'uni-ellipsis-' + ellipsis : '']">{{ title }}</text>
  22. <text v-if="note" class="uni-list-item__content-note">{{ note }}</text>
  23. </view>
  24. </slot>
  25. <slot name="footer">
  26. <view v-if="rightText || showBadge || showSwitch" class="uni-list-item__extra" :class="{ 'flex--justify': direction === 'column' }">
  27. <text v-if="rightText" class="uni-list-item__extra-text">{{ rightText }}</text>
  28. <uni-badge v-if="showBadge" :type="badgeType" :text="badgeText" />
  29. <switch v-if="showSwitch" :disabled="disabled" :checked="switchChecked" @change="onSwitchChange" />
  30. </view>
  31. </slot>
  32. </view>
  33. <uni-icons v-if="showArrow || link" :size="16" class="uni-icon-wrapper" color="#bbb" type="arrowright" />
  34. </view>
  35. <!-- #ifdef APP-NVUE -->
  36. </cell>
  37. <!-- #endif -->
  38. </template>
  39. <script>
  40. import uniIcons from '../uni-icons/uni-icons.vue';
  41. import uniBadge from '../uni-badge/uni-badge.vue';
  42. /**
  43. * ListItem 列表子组件
  44. * @description 列表子组件
  45. * @tutorial https://ext.dcloud.net.cn/plugin?id=24
  46. * @property {String} title 标题
  47. * @property {String} note 描述
  48. * @property {String} thumb 左侧缩略图,若thumb有值,则不会显示扩展图标
  49. * @property {String} thumbSize = [lg|base|sm] 略缩图大小
  50. * @value lg 大图
  51. * @value base 一般
  52. * @value sm 小图
  53. * @property {String} badgeText 数字角标内容
  54. * @property {String} badgeType 数字角标类型,参考[uni-icons](https://ext.dcloud.net.cn/plugin?id=21)
  55. * @property {String} rightText 右侧文字内容
  56. * @property {Boolean} disabled = [true|false] 是否禁用
  57. * @property {Boolean} clickable = [true|false] 是否开启点击反馈
  58. * @property {String} link = [navigateTo|redirectTo|reLaunch|switchTab] 是否展示右侧箭头并开启点击反馈
  59. * @value navigateTo 同 uni.navigateTo()
  60. * @value redirectTo 同 uni.redirectTo()
  61. * @value reLaunch 同 uni.reLaunch()
  62. * @value switchTab 同 uni.switchTab()
  63. * @property {String | PageURIString} to 跳转目标页面
  64. * @property {Boolean} showBadge = [true|false] 是否显示数字角标
  65. * @property {Boolean} showSwitch = [true|false] 是否显示Switch
  66. * @property {Boolean} switchChecked = [true|false] Switch是否被选中
  67. * @property {Boolean} showExtraIcon = [true|false] 左侧是否显示扩展图标
  68. * @property {Object} extraIcon 扩展图标参数,格式为 {color: '#4cd964',size: '22',type: 'spinner'}
  69. * @property {String} direction = [row|column] 排版方向
  70. * @value row 水平排列
  71. * @value column 垂直排列
  72. * @event {Function} click 点击 uniListItem 触发事件
  73. * @event {Function} switchChange 点击切换 Switch 时触发
  74. */
  75. export default {
  76. name: 'UniListItem',
  77. components: {
  78. uniIcons,
  79. uniBadge
  80. },
  81. props: {
  82. direction: {
  83. type: String,
  84. default: 'row'
  85. },
  86. title: {
  87. type: String,
  88. default: ''
  89. },
  90. note: {
  91. type: String,
  92. default: ''
  93. },
  94. ellipsis: {
  95. type: [Number],
  96. default: 0
  97. },
  98. disabled: {
  99. type: [Boolean, String],
  100. default: false
  101. },
  102. clickable: {
  103. type: Boolean,
  104. default: false
  105. },
  106. showArrow: {
  107. type: [Boolean, String],
  108. default: false
  109. },
  110. link: {
  111. type: [Boolean, String],
  112. default: false
  113. },
  114. to: {
  115. type: String,
  116. default: ''
  117. },
  118. showBadge: {
  119. type: [Boolean, String],
  120. default: false
  121. },
  122. showSwitch: {
  123. type: [Boolean, String],
  124. default: false
  125. },
  126. switchChecked: {
  127. type: [Boolean, String],
  128. default: false
  129. },
  130. badgeText: {
  131. type: String,
  132. default: ''
  133. },
  134. badgeType: {
  135. type: String,
  136. default: 'success'
  137. },
  138. rightText: {
  139. type: String,
  140. default: ''
  141. },
  142. thumb: {
  143. type: String,
  144. default: ''
  145. },
  146. thumbSize: {
  147. type: String,
  148. default: 'base'
  149. },
  150. showExtraIcon: {
  151. type: [Boolean, String],
  152. default: false
  153. },
  154. extraIcon: {
  155. type: Object,
  156. default() {
  157. return {
  158. type: 'contact',
  159. color: '#000000',
  160. size: 20
  161. };
  162. }
  163. },
  164. border: {
  165. type: Boolean,
  166. default: true
  167. }
  168. },
  169. inject: ['list'],
  170. data() {
  171. return {
  172. isFirstChild: false
  173. };
  174. },
  175. mounted() {
  176. if (!this.list.firstChildAppend) {
  177. this.list.firstChildAppend = true;
  178. this.isFirstChild = true;
  179. }
  180. },
  181. methods: {
  182. onClick() {
  183. if (this.to !== '') {
  184. this.openPage();
  185. return;
  186. }
  187. if (this.clickable || this.link) {
  188. this.$emit('click', {
  189. data: {}
  190. });
  191. }
  192. },
  193. onSwitchChange(e) {
  194. this.$emit('switchChange', e.detail);
  195. },
  196. openPage() {
  197. if (['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'].indexOf(this.link) !== -1) {
  198. this.pageApi(this.link);
  199. } else {
  200. this.pageApi('navigateTo');
  201. }
  202. },
  203. pageApi(api) {
  204. uni[api]({
  205. url: this.to,
  206. success: res => {
  207. this.$emit('click', {
  208. data: res
  209. });
  210. },
  211. fail: err => {
  212. this.$emit('click', {
  213. data: err
  214. });
  215. console.error(err.errMsg);
  216. }
  217. });
  218. }
  219. }
  220. };
  221. </script>
  222. <style lang="scss">
  223. $list-item-pd: $uni-spacing-col-lg $uni-spacing-row-lg;
  224. .uni-list-item {
  225. /* #ifndef APP-NVUE */
  226. display: flex;
  227. /* #endif */
  228. font-size: $uni-font-size-lg;
  229. position: relative;
  230. justify-content: space-between;
  231. background-color: #fff;
  232. flex-direction: row;
  233. }
  234. .uni-list-item--disabled {
  235. opacity: 0.3;
  236. }
  237. .uni-list-item--hover {
  238. background-color: $uni-bg-color-hover;
  239. }
  240. .uni-list-item__container {
  241. position: relative;
  242. /* #ifndef APP-NVUE */
  243. display: flex;
  244. /* #endif */
  245. flex-direction: row;
  246. padding: $list-item-pd;
  247. padding-left: $uni-spacing-row-lg;
  248. flex: 1;
  249. overflow: hidden;
  250. // align-items: center;
  251. }
  252. .container--right {
  253. padding-right: 0;
  254. }
  255. // .border--left {
  256. // margin-left: $uni-spacing-row-lg;
  257. // }
  258. .uni-list--border {
  259. position: absolute;
  260. top: 0;
  261. right: 0;
  262. left: 0;
  263. /* #ifdef APP-NVUE */
  264. border-top-color: $uni-border-color;
  265. border-top-style: solid;
  266. border-top-width: 0.5px;
  267. /* #endif */
  268. }
  269. /* #ifndef APP-NVUE */
  270. .uni-list--border:after {
  271. position: absolute;
  272. top: 0;
  273. right: 0;
  274. left: 0;
  275. height: 1px;
  276. content: '';
  277. -webkit-transform: scaleY(0.5);
  278. transform: scaleY(0.5);
  279. background-color: $uni-border-color;
  280. }
  281. /* #endif */
  282. .uni-list-item__content {
  283. /* #ifndef APP-NVUE */
  284. display: flex;
  285. /* #endif */
  286. padding-right: 8px;
  287. flex: 1;
  288. color: #3b4144;
  289. // overflow: hidden;
  290. flex-direction: column;
  291. justify-content: space-between;
  292. overflow: hidden;
  293. }
  294. .uni-list-item__content--center {
  295. justify-content: center;
  296. }
  297. .uni-list-item__content-title {
  298. font-size: $uni-font-size-base;
  299. color: #3b4144;
  300. overflow: hidden;
  301. }
  302. .uni-list-item__content-note {
  303. margin-top: 6rpx;
  304. color: $uni-text-color-grey;
  305. font-size: $uni-font-size-sm;
  306. overflow: hidden;
  307. }
  308. .uni-list-item__extra {
  309. // width: 25%;
  310. /* #ifndef APP-NVUE */
  311. display: flex;
  312. /* #endif */
  313. flex-direction: row;
  314. justify-content: flex-end;
  315. align-items: center;
  316. }
  317. .uni-list-item__header {
  318. /* #ifndef APP-NVUE */
  319. display: flex;
  320. /* #endif */
  321. flex-direction: row;
  322. align-items: center;
  323. }
  324. .uni-list-item__icon {
  325. margin-right: 18rpx;
  326. flex-direction: row;
  327. justify-content: center;
  328. align-items: center;
  329. }
  330. .uni-list-item__icon-img {
  331. /* #ifndef APP-NVUE */
  332. display: block;
  333. /* #endif */
  334. height: $uni-img-size-base;
  335. width: $uni-img-size-base;
  336. }
  337. .uni-icon-wrapper {
  338. /* #ifndef APP-NVUE */
  339. display: flex;
  340. /* #endif */
  341. align-items: center;
  342. padding: 0 10px;
  343. }
  344. .flex--direction {
  345. flex-direction: column;
  346. align-items: initial;
  347. }
  348. .flex--justify {
  349. justify-content: initial;
  350. }
  351. .uni-list--lg {
  352. height: $uni-img-size-lg;
  353. width: $uni-img-size-lg;
  354. }
  355. .uni-list--base {
  356. height: $uni-img-size-base;
  357. width: $uni-img-size-base;
  358. }
  359. .uni-list--sm {
  360. height: $uni-img-size-sm;
  361. width: $uni-img-size-sm;
  362. }
  363. .uni-list-item__extra-text {
  364. color: $uni-text-color-grey;
  365. font-size: $uni-font-size-sm;
  366. }
  367. .uni-ellipsis-1 {
  368. /* #ifndef APP-NVUE */
  369. overflow: hidden;
  370. white-space: nowrap;
  371. text-overflow: ellipsis;
  372. /* #endif */
  373. /* #ifdef APP-NVUE */
  374. lines: 1;
  375. /* #endif */
  376. }
  377. .uni-ellipsis-2 {
  378. /* #ifndef APP-NVUE */
  379. overflow: hidden;
  380. text-overflow: ellipsis;
  381. display: -webkit-box;
  382. -webkit-line-clamp: 2;
  383. -webkit-box-orient: vertical;
  384. /* #endif */
  385. /* #ifdef APP-NVUE */
  386. lines: 2;
  387. /* #endif */
  388. }
  389. </style>