uni-list-item.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <cell :keep-scroll-position="keepScrollPosition">
  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="{paddingTop:padding.top,paddingLeft:padding.left,paddingRight:padding.right,paddingBottom:padding.bottom}">
  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 :customPrefix="extraIcon.customPrefix" :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. customPrefix: ''
  171. };
  172. }
  173. },
  174. border: {
  175. type: Boolean,
  176. default: true
  177. },
  178. customStyle: {
  179. type: Object,
  180. default () {
  181. return {
  182. padding: '',
  183. backgroundColor: '#FFFFFF'
  184. }
  185. }
  186. },
  187. keepScrollPosition: {
  188. type: Boolean,
  189. default: false
  190. }
  191. },
  192. watch: {
  193. 'customStyle.padding': {
  194. handler(padding) {
  195. if(typeof padding == 'number'){
  196. padding += ''
  197. }
  198. let paddingArr = padding.split(' ')
  199. if (paddingArr.length === 1) {
  200. this.padding = {
  201. "top": padding,
  202. "right": padding,
  203. "bottom": padding,
  204. "left": padding
  205. }
  206. } else if (paddingArr.length === 2) {
  207. this.padding = {
  208. "top": padding[0],
  209. "right": padding[1],
  210. "bottom": padding[0],
  211. "left": padding[1]
  212. }
  213. } else if (paddingArr.length === 4) {
  214. this.padding = {
  215. "top": padding[0],
  216. "right": padding[1],
  217. "bottom": padding[2],
  218. "left": padding[3]
  219. }
  220. }
  221. },
  222. immediate: true
  223. }
  224. },
  225. // inject: ['list'],
  226. data() {
  227. return {
  228. isFirstChild: false,
  229. padding: {
  230. top: "",
  231. right: "",
  232. bottom: "",
  233. left: ""
  234. }
  235. };
  236. },
  237. mounted() {
  238. this.list = this.getForm()
  239. // 判断是否存在 uni-list 组件
  240. if (this.list) {
  241. if (!this.list.firstChildAppend) {
  242. this.list.firstChildAppend = true;
  243. this.isFirstChild = true;
  244. }
  245. }
  246. },
  247. methods: {
  248. /**
  249. * 获取父元素实例
  250. */
  251. getForm(name = 'uniList') {
  252. let parent = this.$parent;
  253. let parentName = parent.$options.name;
  254. while (parentName !== name) {
  255. parent = parent.$parent;
  256. if (!parent) return false
  257. parentName = parent.$options.name;
  258. }
  259. return parent;
  260. },
  261. onClick() {
  262. if (this.to !== '') {
  263. this.openPage();
  264. return;
  265. }
  266. if (this.clickable || this.link) {
  267. this.$emit('click', {
  268. data: {}
  269. });
  270. }
  271. },
  272. onSwitchChange(e) {
  273. this.$emit('switchChange', e.detail);
  274. },
  275. openPage() {
  276. if (['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'].indexOf(this.link) !== -1) {
  277. this.pageApi(this.link);
  278. } else {
  279. this.pageApi('navigateTo');
  280. }
  281. },
  282. pageApi(api) {
  283. let callback = {
  284. url: this.to,
  285. success: res => {
  286. this.$emit('click', {
  287. data: res
  288. });
  289. },
  290. fail: err => {
  291. this.$emit('click', {
  292. data: err
  293. });
  294. }
  295. }
  296. switch (api) {
  297. case 'navigateTo':
  298. uni.navigateTo(callback)
  299. break
  300. case 'redirectTo':
  301. uni.redirectTo(callback)
  302. break
  303. case 'reLaunch':
  304. uni.reLaunch(callback)
  305. break
  306. case 'switchTab':
  307. uni.switchTab(callback)
  308. break
  309. default:
  310. uni.navigateTo(callback)
  311. }
  312. }
  313. }
  314. };
  315. </script>
  316. <style lang="scss">
  317. $uni-font-size-sm:12px;
  318. $uni-font-size-base:14px;
  319. $uni-font-size-lg:16px;
  320. $uni-spacing-col-lg: 12px;
  321. $uni-spacing-row-lg: 15px;
  322. $uni-img-size-sm:20px;
  323. $uni-img-size-base:26px;
  324. $uni-img-size-lg:40px;
  325. $uni-border-color:#e5e5e5;
  326. $uni-bg-color-hover:#f1f1f1;
  327. $uni-text-color-grey:#999;
  328. $list-item-pd: $uni-spacing-col-lg $uni-spacing-row-lg;
  329. .uni-list-item {
  330. /* #ifndef APP-NVUE */
  331. display: flex;
  332. /* #endif */
  333. font-size: $uni-font-size-lg;
  334. position: relative;
  335. justify-content: space-between;
  336. align-items: center;
  337. background-color: #fff;
  338. flex-direction: row;
  339. /* #ifdef H5 */
  340. cursor: pointer;
  341. /* #endif */
  342. }
  343. .uni-list-item--disabled {
  344. opacity: 0.3;
  345. }
  346. .uni-list-item--hover {
  347. background-color: $uni-bg-color-hover !important;
  348. }
  349. .uni-list-item__container {
  350. position: relative;
  351. /* #ifndef APP-NVUE */
  352. display: flex;
  353. /* #endif */
  354. flex-direction: row;
  355. padding: $list-item-pd;
  356. padding-left: $uni-spacing-row-lg;
  357. flex: 1;
  358. overflow: hidden;
  359. // align-items: center;
  360. }
  361. .container--right {
  362. padding-right: 0;
  363. }
  364. // .border--left {
  365. // margin-left: $uni-spacing-row-lg;
  366. // }
  367. .uni-list--border {
  368. position: absolute;
  369. top: 0;
  370. right: 0;
  371. left: 0;
  372. /* #ifdef APP-NVUE */
  373. border-top-color: $uni-border-color;
  374. border-top-style: solid;
  375. border-top-width: 0.5px;
  376. /* #endif */
  377. }
  378. /* #ifndef APP-NVUE */
  379. .uni-list--border:after {
  380. position: absolute;
  381. top: 0;
  382. right: 0;
  383. left: 0;
  384. height: 1px;
  385. content: '';
  386. -webkit-transform: scaleY(0.5);
  387. transform: scaleY(0.5);
  388. background-color: $uni-border-color;
  389. }
  390. /* #endif */
  391. .uni-list-item__content {
  392. /* #ifndef APP-NVUE */
  393. display: flex;
  394. /* #endif */
  395. padding-right: 8px;
  396. flex: 1;
  397. color: #3b4144;
  398. // overflow: hidden;
  399. flex-direction: column;
  400. justify-content: space-between;
  401. overflow: hidden;
  402. }
  403. .uni-list-item__content--center {
  404. justify-content: center;
  405. }
  406. .uni-list-item__content-title {
  407. font-size: $uni-font-size-base;
  408. color: #3b4144;
  409. overflow: hidden;
  410. }
  411. .uni-list-item__content-note {
  412. margin-top: 6rpx;
  413. color: $uni-text-color-grey;
  414. font-size: $uni-font-size-sm;
  415. overflow: hidden;
  416. }
  417. .uni-list-item__extra {
  418. // width: 25%;
  419. /* #ifndef APP-NVUE */
  420. display: flex;
  421. /* #endif */
  422. flex-direction: row;
  423. justify-content: flex-end;
  424. align-items: center;
  425. }
  426. .uni-list-item__header {
  427. /* #ifndef APP-NVUE */
  428. display: flex;
  429. /* #endif */
  430. flex-direction: row;
  431. align-items: center;
  432. }
  433. .uni-list-item__icon {
  434. margin-right: 18rpx;
  435. flex-direction: row;
  436. justify-content: center;
  437. align-items: center;
  438. }
  439. .uni-list-item__icon-img {
  440. /* #ifndef APP-NVUE */
  441. display: block;
  442. /* #endif */
  443. height: $uni-img-size-base;
  444. width: $uni-img-size-base;
  445. margin-right: 10px;
  446. }
  447. .uni-icon-wrapper {
  448. /* #ifndef APP-NVUE */
  449. display: flex;
  450. /* #endif */
  451. align-items: center;
  452. padding: 0 10px;
  453. }
  454. .flex--direction {
  455. flex-direction: column;
  456. /* #ifndef APP-NVUE */
  457. align-items: initial;
  458. /* #endif */
  459. }
  460. .flex--justify {
  461. /* #ifndef APP-NVUE */
  462. justify-content: initial;
  463. /* #endif */
  464. }
  465. .uni-list--lg {
  466. height: $uni-img-size-lg;
  467. width: $uni-img-size-lg;
  468. }
  469. .uni-list--base {
  470. height: $uni-img-size-base;
  471. width: $uni-img-size-base;
  472. }
  473. .uni-list--sm {
  474. height: $uni-img-size-sm;
  475. width: $uni-img-size-sm;
  476. }
  477. .uni-list-item__extra-text {
  478. color: $uni-text-color-grey;
  479. font-size: $uni-font-size-sm;
  480. }
  481. .uni-ellipsis-1 {
  482. /* #ifndef APP-NVUE */
  483. overflow: hidden;
  484. white-space: nowrap;
  485. text-overflow: ellipsis;
  486. /* #endif */
  487. /* #ifdef APP-NVUE */
  488. lines: 1;
  489. text-overflow: ellipsis;
  490. /* #endif */
  491. }
  492. .uni-ellipsis-2 {
  493. /* #ifndef APP-NVUE */
  494. overflow: hidden;
  495. text-overflow: ellipsis;
  496. display: -webkit-box;
  497. -webkit-line-clamp: 2;
  498. -webkit-box-orient: vertical;
  499. /* #endif */
  500. /* #ifdef APP-NVUE */
  501. lines: 2;
  502. text-overflow: ellipsis;
  503. /* #endif */
  504. }
  505. </style>