uni-list-item.vue 12 KB

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