uni-list-item.vue 12 KB

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