uni-list-item.vue 10 KB

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