uni-list-item.vue 13 KB

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